How to pass value from javascript to php file

后端 未结 2 1306
我在风中等你
我在风中等你 2021-01-28 14:47

I am getting value via JQuery as something like this

var query= popURL.split(\'?\');  
var dim= query[1].split(\'&\'); 
var popWidth = dim[0].sp         


        
相关标签:
2条回答
  • 2021-01-28 14:59

    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)

    0 讨论(0)
  • 2021-01-28 15:12

    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.

    0 讨论(0)
提交回复
热议问题