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.
As for using the data from my POST or retrieving it,
In my controller do I need to use
$value = /Input::json('data');
or
Input::post('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
Input::get('data');
Sorry if the questions might be simple. Thanks for any help.
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.
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);
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?
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?