CodeIgniter Forums
passing data from js to controller - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forum-22.html)
+--- Thread: passing data from js to controller (/thread-39230.html)



passing data from js to controller - El Forum - 03-04-2011

[eluser]Giorgio Nordo[/eluser]
I need to pass a data (just a numeric value) from a javascript function contained in a view to a controller.
I tryed to generate a cookie inside the javascript and to restore it inside the constructor of my controller by means of $this->input->cookie.
It seems that the value is not immediately available to the above CI method and in order to obtain the desired effect I have to manually refresh the page.
Obviously this is bad :-(

Some idea?


passing data from js to controller - El Forum - 03-05-2011

[eluser]steelaz[/eluser]
This forum is for CI add-ons libraries etc., next time please post in "Help, is this a bug?" forum.

Cookies are read by the browser only when page is being loaded, so if you send a cookie using JavaScript, you will be able to access it from CI on the next page load only.

If you need to call controller method from your view, without refreshing the page, you should use AJAX. Using jQuery this call would look something like this:

Code:
$.post("http://www.example.com/controller/method/", { my_data: "1" } );



passing data from js to controller - El Forum - 03-05-2011

[eluser]Giorgio Nordo[/eluser]
thank you and sorry for having used the wrong section (maybe a forum administrator chan move there this thread).
Giorgio