How To Pass Value From Javascript To Php File
Solution 1:
Take a look at the jQuery api documentation, in particular to the jQuery.ajax() function. You'll find documentation and many useful examples. This is a piece of code from that page that does exactly what you've requested:
$.ajax({
type: "POST",
url: "some.php",
data: "name=John&location=Boston",
success: function(msg){
alert( "Data Saved: " + msg );
}
});
(Of course you can also pass the data by simply using GET params and appending them to the url)
Solution 2:
well, if you only want to send that data to the server and you don't want to retrieve anything else and don't know or don't want to use ajax, a more simpler solution would be to attach the desired url on a hidden dom object. such as :
<img src="http://my-host.com/my-script.php?id="+id style="display:none;"/>
witch can be added to the dom in multiple ways.
If you want to use ajax, I recommend using a library such as jQuery, wich is optimized for cross-browser web apps. With jQuery, the syntax is very simple :
$.get(
"http://my-host.com/my-script.php",
{"id" : id},
function(data){
// do something with the response
}
);
You could use "post" instead of "get" depending on your needs. I recommend you take a peak at jQuery documentation.
Post a Comment for "How To Pass Value From Javascript To Php File"