Welcome Guest, Not a member yet? Register   Sign In
Getting jQuery $.ajax to work.
#1

[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({
type: "POST",
url: "http://localhost/projects/wcas/index.php/vehicles/viewvehicle/<?php echo site_url('vehicles/get_serial_numbers/'); ?>",
dataType: "json",
success: function(msg){
     alert( "Data Saved: " + msg );
}
});

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
#2

[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.




Theme © iAndrew 2016 - Forum software by © MyBB