简体   繁体   English

通过jQuery $ .post()函数发送数据时的JavaScript变量

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

I have a problem when I want to send input data via jQuery's $.post . 我想通过jQuery的$.post发送输入数据时遇到问题。 I have this input: 我有这个输入:

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

and I want to send its data via jQuery's $.post() function. 我想通过jQuery的$.post()函数发送其数据。

This $.post function runs, but it does't send anything: $.post函数运行,但不发送任何内容:

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. 我应该使用输入来保存数据,而方法2不适合我。

The data needs to be an object. 数据必须是一个对象。

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

You can access this in your PHP with $_POST['mydata'] . 您可以使用$_POST['mydata']在您的PHP中访问它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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