Jquery AJAX 发布到 PHP
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4105211/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jquery AJAX post to PHP
提问by benhowdle89
OK i've got my json string built but i'm not sure what to do next??
好的,我已经建立了我的 json 字符串,但我不确定下一步该怎么做??
$('#submit').live('click',function(){
var dataString = '[';
$('#items tr').not(':first').each(function(){
var index = $('#items tr').index(this);
var supp_short_code=$(this).closest('tr').find('.supp_short_code').text();
var project_ref=$(this).closest('tr').find('.project_ref').text();
var om_part_no=$(this).closest('tr').find('.om_part_no').text();
var description=$(this).closest('tr').find('.description').text();
var cost_of_items=$(this).closest('tr').find('.cost_of_items').text();
var cost_total=$(this).closest('tr').find('.cost_total').text();
dataString += '{"row":"' + index + '", "supp_short_code":"' + supp_short_code + '", "project_ref":"' + project_ref + '", "om_part_no":"' + om_part_no + '", "description":"' + description + '", "cost_of_items":"' + cost_of_items + '", "cost_total_td":"' + cost_total + '"}';
});
dataString += ']';
$.ajax
({
type: "POST",
url: "order.php",
data: dataString,
cache: false,
success: function()
{
alert("Order Submitted");
}
});
});
In my php file i was attempting to write the dataString to a text file so i could see its coming through ok but nothing was in the text file!? Am i doing something wrong client side or PHP side, my php code:
在我的 php 文件中,我试图将 dataString 写入文本文件,以便我可以看到它通过正常但文本文件中没有任何内容!?我在客户端或 PHP 端做错了什么,我的 php 代码:
<?php
$stringData = $_POST['dataString'];
$myFile = "testFile.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, $stringData);
fclose($fh);
?>
回答by jerjer
This should do it:
这应该这样做:
...
$.ajax({
type: "POST",
url: "order.php",
data: { 'dataString': dataString },
cache: false,
success: function()
{
alert("Order Submitted");
}
});
You may try to verify:
您可以尝试验证:
<?php
$stringData = $_POST['dataString'];
echo $stringData;
?>
回答by Phil
Why don't you try constructing your data like this
你为什么不尝试像这样构建你的数据
var postData = {};
$('#items tr').not(':first').each(function(index, value) {
var keyPrefix = 'data[' + index + ']';
postData[keyPrefix + '[supp_short_code]'] = $(this).closest('tr').find('.supp_short_code').text();
postData[keyPrefix + '[project_ref]'] = $(this).closest('tr').find('.project_ref').text();
// and so on
});
Then in your AJAX call
然后在你的 AJAX 调用中
data: postData,
Now your PHP script can process the data as a multi-dimensional array
现在您的 PHP 脚本可以将数据作为多维数组进行处理
<?php
if (isset($_POST['data']) && is_array($_POST['data'])) {
foreach ($_POST['data'] as $row => $data) {
echo $data['supp_short_code'];
echo $data['project_ref'];
// and so on
}
}
回答by SalientKnight
First convert the json object to a string in js like this:
首先将json对象转换为js中的字符串,如下所示:
var json_string=JSON.stringify(json_object);
Then, pass it to PHP as a string and then in php decode it, like this :
然后,将它作为字符串传递给 PHP,然后在 php 中对其进行解码,如下所示:
<?php
$map = json_decode($_POST['json_string']);
?>
I hope this helps anyone just finding this thread...
我希望这可以帮助任何人找到这个线程......
回答by Steve Whitfield
The problem will be that you are attempting to access a POST variable called "dataString" that does not exist. Just because you set the "data" property to the contents of a variable called "dataString" does not mean that your post variable will be called "dataString".
问题是您正在尝试访问不存在的名为“dataString”的 POST 变量。仅仅因为您将“data”属性设置为名为“dataString”的变量的内容并不意味着您的 post 变量将被称为“dataString”。
You could try this:
你可以试试这个:
data: { "dataString": dataString },
This passes an object to the jQuery function that has a property called "dataString" and the value of your actual data string. jQuery will take all the properties from this object (in this case just one) and set them as post variables on the HTTP request that it will eventually send to your PHP application. This allows you to access the data via the $_POST["dataString"] call.
这会将一个对象传递给具有名为“dataString”的属性和实际数据字符串值的 jQuery 函数。jQuery 将从这个对象中获取所有属性(在本例中只有一个),并将它们设置为 HTTP 请求上的 post 变量,它最终将发送到您的 PHP 应用程序。这允许您通过 $_POST["dataString"] 调用访问数据。
Steve
史蒂夫
回答by T.Todua
I have problems when use:
我在使用时遇到问题:
url: "/folder/form.php",
And i have to use:
我必须使用:
url: "folder/form.php",,