jqueryajax发布到PHP
好的,我已经构建了json字符串,但我不确定下一步该做什么jqueryajax发布到PHP,php,jquery,ajax,json,post,Php,Jquery,Ajax,Json,Post,好的,我已经构建了json字符串,但我不确定下一步该做什么 $('#submit').live('click',function(){ var dataString = '['; $('#items tr').not(':first').each(function(){ var index = $('#items tr').index(this);
$('#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");
}
});
});
在我的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);
?>
这应该可以做到:
...
$.ajax({
type: "POST",
url: "order.php",
data: { 'dataString': dataString },
cache: false,
success: function()
{
alert("Order Submitted");
}
});
您可以尝试验证:
<?php
$stringData = $_POST['dataString'];
echo $stringData;
?>
问题在于您试图访问一个不存在的名为“dataString”的POST变量。仅仅因为您将“data”属性设置为名为“dataString”的变量的内容,并不意味着您的post变量将被称为“dataString”
你可以试试这个:
data: { "dataString": dataString },
这将向jQuery函数传递一个对象,该函数具有一个名为“dataString”的属性和实际数据字符串的值。jQuery将从这个对象获取所有属性(在本例中只有一个),并将它们设置为HTTP请求上的post变量,最终发送给PHP应用程序。这允许您通过$\u POST[“dataString”]调用访问数据
史蒂夫你为什么不试着这样构建你的数据呢
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
});
然后在AJAX调用中
data: postData,
现在,您的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
}
}
首先将json对象转换为js中的字符串,如下所示:
var json_string=JSON.stringify(json_object);
然后,将其作为字符串传递给PHP,然后在PHP中对其进行解码,如下所示:
<?php
$map = json_decode($_POST['json_string']);
?>
我希望这有助于任何人找到此线程…我在使用时遇到问题:
url: "/folder/form.php",
我必须使用:
url: "folder/form.php",,
使用firebug检查而不是写入文件。是的,我可以在firebug中看到它,但我只是想确认PHP是否能够正常运行,我认为我需要在PHP端使用json_解码,对吗?例如,对于每一行,做类似的事情……如果你想解析字符串中的数据,那么你应该使用json_解码