jquery json未返回值
jquery json not returning value
我使用jquery使用serialize提交表单,除了输入多个框外,一切都很好。如果我输入1个框,消息框将显示在#BA_addbox中。然而,如果我使用,作为分隔符输入了多个框,那么在firebug中不会显示任何消息,也不会显示任何json选项卡。只是正确的html。我哪里的代码出了问题。
我已经创建了一个数组,并使用foreach和爆炸来分隔值,但没有返回多个值。感谢
更新:正在php脚本中收集变量,如下所示:
php代码
$dept = mysql_real_escape_string($_POST['customerdept']);
$company = mysql_real_escape_string($_POST['BA_customer']);
$address = mysql_real_escape_string($_POST['customeraddress']);
$service = mysql_real_escape_string($_POST['BA_service']);
$box = mysql_real_escape_string($_POST['BA_box']);
$date = DateTime::createFromFormat('d/m/Y', $_POST['BA_destdate']);
$destdate = $date - > format('Y-m-d');
$authorised = mysql_real_escape_string($_POST['BA_authorised']);
$submit = mysql_real_escape_string($_POST['submit']);
$array = explode(",", $_POST['BA_box']);
if (isset($_POST['submit'])) {
foreach ($array as $box) {
//$sql = "INSERT INTO `act` (service, activity, company, address, department, user, destroydate, date, item, new) VALUES ('$service', '$activity', '$company', '$address', '$dept', '$authorised', '$destdate', NOW(), '$box', 1)";
//$result = runSQL($sql) or die(mysql_error());
$form=array('dept'=>$dept,
'company'=>$company,
'address'=>$address,
'service'=>$service,
'box'=>$box,
'destroydate'=>$destdate,
'authorised'=>$authorised,
'submit'=>$submit);
$result=json_encode($form);
echo $result;
}
}
jquery代码
submitHandler: function() {
if ($("#BA_boxform").valid() === true) {
var data = $("#BA_boxform").serialize();
$.post('/domain/admin/requests/boxes/boxesadd.php', data, function(msg) {
$("#BA_addbox").html("You have entered box(es): " + "<b>" + msg.box + "</b><br /> You may now close this window.");
$("#BA_boxform").get(0).reset();
}, 'json');
} else
{
return;
}
},
success: function(msg) {
//$("#BA_addbox").html("You have entered a box");
//$("#BA_boxform").get(0).reset();
}
Per@nnnnnn:
如果有多个框,则生成的json看起来像下面的结构。这是无效的json,因此无法可靠地解析。
{
...
}{
...
}
要解决此问题,必须将数组添加到另一个数组中,然后对父数组进行编码。
$box = mysql_real_escape_string($_POST['BA_box']);
$array = explode(",", $_POST['BA_box']);
$output = Array();
if (isset($_POST['submit'])) {
foreach ($array as $box) {
//$sql = "INSERT INTO `act` (service, activity, company, address, department, user, destroydate, date, item, new) VALUES ('$service', '$activity', '$company', '$address', '$dept', '$authorised', '$destdate', NOW(), '$box', 1)";
//$result = runSQL($sql) or die(mysql_error());
$form=array('dept'=>$dept,
'company'=>$company,
'address'=>$address,
'service'=>$service,
'box'=>$box,
'destroydate'=>$destdate,
'authorised'=>$authorised,
'submit'=>$submit);
//Add to a parent array instead
$output[] = $form;
}
//encode the entire array
$result = json_encode( $output );
echo $result;
}
这将产生以下结构,对于包含解析json的变量data
,每个框都可以通过data[0]
、data[1]
等检索。
[
{
...
},
{
...
}
]
首先修复php以返回有效的json数组。
if (isset($_POST['submit'])) {
foreach ($array as $box) {
//$sql = "INSERT INTO `act` (service, activity, company, address, department, user, destroydate, date, item, new) VALUES ('$service', '$activity', '$company', '$address', '$dept', '$authorised', '$destdate', NOW(), '$box', 1)";
//$result = runSQL($sql) or die(mysql_error());
$form=array('dept'=>$dept,
'company'=>$company,
'address'=>$address,
'service'=>$service,
'box'=>$box,
'destroydate'=>$destdate,
'authorised'=>$authorised,
'submit'=>$submit);
$result[]=$form;
}
echo json_encode( $result );
}
然后post回调中的msg参数应该是一个结果数组,所以不能只执行msg.box
来获得框列表。我建议这样做:
boxes = jQuery.map(msg,function(item){
return item.box;
}).join(',');
从数组中的每个项提取box属性,并将它们连接到逗号分隔的列表中。然后你可以这样显示这个列表:
$("#BA_addbox").html("You have entered box(es): " + "<b>" + boxes +
"</b><br /> You may now close this window.");
考虑到这些变化,你的代码对我来说是有效的。如果你有其他问题,我建议你发布更多的html,尤其是表单结构。您的表单可能没有提交正确的值以从服务器获得有效响应。
相关文章:
- 如何从php返回的JSON中检索值
- 使用Javascript数组或JSON来存储AJAX请求的返回值,以备将来使用
- 更改返回的JSON数据的值
- JSON获取地理位置在HTML中未返回值
- 在调用中使用返回的json值不起作用
- 在JSON返回值中循环
- 在 java 脚本中从 JSON 加载内容之前返回值
- 无法根据返回的 Json 值向输入字段添加文本
- 如何获取 JSON 对象的返回值作为变量
- 从 Meteor 中的 Stripe API 返回的 JSON 字典中的访问值
- 从控制器 MVC 返回的 JSON 列表中获取值
- 在嵌套的 JavaScript JSON 对象中返回值
- 使用XMLHttpRequest解析JSON时返回值为空
- 如何在jquery中的弹出窗口中访问Json返回值
- 返回标题JSON的所有值
- jquery json未返回值
- 在 javascript 中解析 json 数组时检查返回值
- 在Angular/Restangular中对返回的JSON值进行空检查不起作用
- JSON对象根据位置返回值
- 在HTML中使用返回的JSON编码AJAX PHP响应<作为值