Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
  • Hi there, I'm a bit unsure how to use AJAX send data from my view to my controller.

    This is my code for AJAX. I just want to try to send a simple string to my controller so that I can output that back to my view as a test to make sure everything is working correctly.


        var i = 'hi',
        data = JSON.stringify(i);
            url: "/shelf/index",
            type: "POST",

            success: function(data, status, xhr) {
            console.log("data ="+data);
            console.log("status ="+status);
            console.log("xhr ="+xhr);

        error: function(xhr, status, error) {
        }, complete: function(xhr, status) {



    The key to sending it to the controller is

  • I got it working url had to be 'shelf/index' and not '/shelf/index'.

    I also found out that I need to add
    dataType: 'json',

    to my AJAX but after doing that I would get an error instead of success of the AJAX call.

  • As for using the data from my POST or retrieving it,

    In my controller do I need to use

    $value = /Input::json('data');




    I need to make it so that the information that I send via the post is put
    into a variable that will be passed to my model to run an SQL query,
    get those results, and then output those results to the view.
    When I am returning the results with the view will I need to use AJAX to
    receive the data, right? If so how would I go about that? Could I just
    change the above code from POST to GET?

    Also when do I use


    Sorry if the questions might be simple.  Thanks for any help.
  • Input::post() reads posted data in www-form-data format (which you would also find in $_POST). If you post json, you need to use Input::json().
  • I see. That would be put into my action_index function correct?

    The main thing that I want to be able to do is for example take info from a search box in my view and send that data to my controller when a search button is pressed (without reloading the page). I then want to lets say add 'bye' to the data that was sent to the controller and hand it back to the view to be displayed (without reloading the page). However, I'm I am widely unsure of how AJAX and FuelPHP are interacting. Could you possible give me a basic example of how one might do this? I've scoured the internet for ways of doing it but I've come up pretty short. Maybe I haven't been looking in the right places. I can understand if it is a hassle.However, I would greatly appreciate any help you could give. Thank you.

    So far I have,

    <button type="button" id="add" > byes </button>

    <textarea name="text1" id="text1" cols="3" rows="1"></textarea>
    <textarea name="text2" id="text2" cols="3" rows="1"></textarea>


          $('#add').click(function() {
        var val1 = $('#text1').val();
        var val2 = $('#text2').val();
            type: 'POST',
            url: 'shelf/test',
            data: { text1: val1, text2: val2 },
            success: function(response) {
            error: function(xhr, status, error) {
            console.log("post error");
        }, complete: function(xhr, status) {

        $.get("shelf/test", function(data, response){
          alert("Data: " + data + "\nStatus: " + status);

    My controller:

    class Controller_Shelf extends Controller

    public function action_test(){
            if (Input::is_ajax()) { 
                echo "The type you posted is " .Input::post('text1');
                echo "The type you posted is " .Input::post('text2');

    Only "The type you posted is" shows up in my alert, so I'm assuming my GET is working...
  • Ajax is transparent for Fuel, it's just another HTTP request.

    I would personally not mix the two request types in a single action, at a minimum have distinct actions for ajax requests. You can do that by extending Controller_Hybrid instead of Controller.

    We always use separate controllers for interactive stuff (i.e. HTML) and ajax stuff, also because you may want to deal with authentication differently. In that case, extend Controller_Rest, it will automatically format the json response for you, if you return the data in an array.

    Your code won't work, you can't echo stuff when you request application/json data, your ajax code won't understand it.
  • I changed my controller from Controller to Controller_Hybrid. I had two different actions. My action_ index which is for data manipulation and output and action_test which is for my ajax request.

    So I would want to have a separate controller for users logging out when pressing a logout button or for example going to the next page? Right now I have only my main controller which manipulates my data from my model and outputs it to the screen. I only have one page for my entire project. It gets data for a user to confirm, they click a button to confirm data, and then it brings up the next set of data for the user to confirm. So the only pages I should have are the data confirmation page and the login screen.

    Also I got my Ajax to work when I did this

    Controller, action_test

    $val = $_POST['shouhinName'];

            $model = new Model_Shelf();
            // category data
            $jan = $model->jan($val);

            header('Content-Type: application/json; charset=utf-8');
            echo json_encode($jan);


    $(function() {

          $('#search').click(function() {
                var val = $('#shouhinName').val();
                type: "POST",
                url: "shelf/test",
                data: {
                      shouhinName : val
                success: function(j_data){
                error: function(xhr, status, error) {
            console.log("post error");
        }, complete: function(xhr, status) {


  • If you're using Controller_Hybrid or Controller_Rest, is should be:

        $val = $_POST['shouhinName'];

        $model = new Model_Shelf();

            'jan' => $model->jan($val);

    You're using a framework, no need for manual output generation.
  • If I do what you wrote I get

    ParseError [ Error ]:
    syntax error, unexpected ';', expecting ')'

    So I removed the ; after jan($val) and got


    Not sure why this would happen though.
    It works fine for what I had.
    The code you posted and what I wrote are pretty much doing the same thing by
    handing the output to ajax, right?
  • Sorry, made a typo.

    And I think


    would have been better, looking again at your code.

    Your code would fail as soon as the framework decides it needs to write a header, or other output. Which is why it is better to let the framework handle it. 

    It's no point using a development framework if you don't use it's features, in that case you're better off just writing plain PHP?

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion