Getting jQuery $.ajax to work. |
[eluser]Mr Lazy[/eluser]
Hi, I am having trouble setting up a AJAX call using CI and jQuery. I think the problem is getting the URL right to the controller. I place all my jQuery code in .js files that are included within the HEAD tag, so no PHP will get executed. After a bit of research I found this post: AJAX for CodeIgniter sample code So I thought if I put the jQuery code in the .js file, the PHP will get executed and I will get a URI to my controller and everything will fall into place. Not quite.... I got this: Code: $.ajax({ Uurrghgh... OK, so is it possible to have my jQuery code in a .js file or must it be in the view.php file? Or is there a better way of doing this? Many Thanks, Stephen
[eluser]gon[/eluser]
You can pass variables from PHP to Javascript, by generating an inline script in the view, containing global variable definitions for values you need. In this case you would generate the URL in PHP, and pass it into a variable to the view. This way you can load normal scripts from the public folder. Use the global var in the JS script instead of writing it on the fly. |
Welcome Guest, Not a member yet? Register Sign In |