Welcome Guest, Not a member yet? Register   Sign In
JQuery, AJAX help.
#1

[eluser]rschla104[/eluser]
Hello,

I'm converting a previous project to CI. The old pages were only php files where the logic and view resided in the same page. I've come across a section where $.ajax is being used to retrieve a JSON object(which is just an array of strings) and then run a callback function to parse the strings and get them ready to be placed into a dropdown as selectable options.

What would be the best practice for porting this functionality to CI and maintaining the MVC pattern? Would it belong in the model or the controller?
#2

[eluser]grisha[/eluser]
All the jQuery code is loaded or contained in the view file. The AJAX request sends the POST/GET data to a controller's method (via proper URL). The controller does his job with the model returning (echoing) the output (json_encode) or perhaps loading some views to avoid using HTML inside PHP classes. jQuery gets the JSON and you're at home Smile
#3

[eluser]rschla104[/eluser]
Thanks. I'm able to get the data in the view, but I'm having trouble passing it back to the function in the controller.

I've got data and in the javascript callback function, I'm trying

Code:
var url =  "add/process/" + data;
[removed] = url;

as I read that anything additional after the function segment of the uri will be passed to that function. The data I want to pass is an array of strings. When I do this, it is throwing a 403 says forbidden, I don't have access to add/process/(all of the strings from the array, separated by columns).

Any ideas on how to get my array of strings into the controller?
#4

[eluser]grisha[/eluser]
I'd suggest:
Code:
var url = "<?= base_url(); ?>add/process";

Sending the data back to the controller via POST or GET is much better idea. The array of strings can contain some disallowed chars, so CI might act not as you expected.




Theme © iAndrew 2016 - Forum software by © MyBB