jQuery的AJAX后到PHP [英] Jquery AJAX post to PHP
本文介绍了jQuery的AJAX后到PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
好吧,我有我的JSON字符串内置,但我不知道下一步该怎么做?
$('#提交)。住(点击,函数(){
变种dataString ='[';
$('TR#项目)不是(':第一')。每个(函数(){
。VAR指数= $('#项目TR')指数(本);
。VAR supp_short_ code = $(本).closest(TR)找到('。supp_short_ code')文本();
VAR project_ref = $(本).closest(TR)找到('project_ref。)文本()。
。VAR om_part_no = $(本).closest(TR)找到('。om_part_no)文本();
Var描述= $(本).closest(TR)找到(说明。)文本()。
。VAR cost_of_items = $(本).closest(TR)找到('。cost_of_items)文本();
。VAR cost_total = $(本).closest(TR)找到('。cost_total)文本();
dataString + ='{行:'+指数+',supp_short_ code:'+ supp_short_ code +',project_ref:'+ project_ref +', om_part_no:+ om_part_no +',描述:+原文+',cost_of_items:+ cost_of_items +',cost_total_td:+ cost_total +'}';
});
dataString + =']';
$就
({
键入:POST,
网址:order.php
数据:dataString,
缓存:假的,
成功:函数()
{
警报(订单提交);
}
});
});
在我的PHP文件,我试图写dataString到一个文本文件,这样我就可以看到它的未来通过确定,但什么也没有在文本文件中!?我做得不对客户端或PHP端,我的PHP code:
< PHP
$ StringData是= $ _ POST ['dataString'];
$ MYFILE =TESTFILE.TXT;
$ FH =的fopen($ MYFILE,W)或死亡(无法打开文件);
FWRITE($跳频,$ StringData是);
fclose函数($ FH);
?>
解决方案
你为什么不尝试构建你的数据是这样
VAR POSTDATA = {};
$('#项目TR')不是(':第一')。每个(函数(指数值){
VAR键preFIX ='数据['+指数+];
POSTDATA [键preFIX +'[supp_short_ code]'] = $(本).closest(TR)找到('supp_short_ code')文本()。
POSTDATA [键preFIX +'[project_ref]'] = $(本).closest(TR)找到('project_ref。)文本()。
// 等等
});
然后在你的AJAX调用
数据:POSTDATA,
现在你的PHP脚本可以处理的数据作为多维数组
< PHP
如果(使用isset($ _ POST ['数据'])及和放大器; is_array($ _ POST ['数据'])){
的foreach($ _ POST ['数据']为$行=> $数据){
回声$数据['supp_short_ code'];
回声$数据['project_ref'];
// 等等
}
}
OK i've got my json string built but i'm not sure what to do next??
$('#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
$stringData = $_POST['dataString'];
$myFile = "testFile.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, $stringData);
fclose($fh);
?>
解决方案
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
data: postData,
Now your PHP script can process the data as a multi-dimensional array
<?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
}
}
这篇关于jQuery的AJAX后到PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文