简体   繁体   中英

How to pass value from javascript to php file

I am getting value via JQuery as something like this

var query= popURL.split('?');  
var dim= query[1].split('&'); 
var popWidth = dim[0].split('=')[1]; //Gets the first query string value 
var id = dim[1].split('=')[1];

Now i want to send or get this "id" value into same php file. I think we can only get value on other php page using get method. I want to get value on same php file.

I think we can do with this using AJAX right?

If yes please give me some links or code how to get? I am newbie to ajax .

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)

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM