简体   繁体   中英

JavaScript variable when sending data via jQuery $.post() function

I have a problem when I want to send input data via jQuery's $.post . I have this input:

<input id="mydata" value='<?php echo $data; ?>' >

and I want to send its data via jQuery's $.post() function.

This $.post function runs, but it does't send anything:

var the_data = $('#mydata').val();
//alert(the_data); has correct data!!
$.post("http://php/server", the_data , function(data){
    alert(data);
}, "html");

But this one sends data correctly:

$.post("http://php/server", <?php echo $data; ?> , function(data){}, "html");

I should use input for saving data and method 2 isn't suitable for me.

The data needs to be an object.

var the_data = {
    mydata: $('#mydata').val()
};

You can access this in your PHP with $_POST['mydata'] .

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