Php 从json返回多个值

Php 从json返回多个值,php,javascript,jquery,Php,Javascript,Jquery,目前,我正在使用一个脚本,该脚本以msg[0].box msg[1].box等格式从json返回一个值。如果用户输入(比如)2项,这是可以的。但是,如果用户输入的内容不止这些呢。我的一位朋友建议我查看jquery的每个函数以重新运行多个值,但鉴于我对jquery还相当陌生,如果有人能帮助我使用代码实现这一点,我将不胜感激 我也很困惑,为什么在返回值时firebug中没有json选项卡,只有resosse和html。这正常吗?请注意,输入值时使用,作为分隔符 php代码 $dept = m

目前,我正在使用一个脚本,该脚本以msg[0].box msg[1].box等格式从json返回一个值。如果用户输入(比如)2项,这是可以的。但是,如果用户输入的内容不止这些呢。我的一位朋友建议我查看jquery的每个函数以重新运行多个值,但鉴于我对jquery还相当陌生,如果有人能帮助我使用代码实现这一点,我将不胜感激

我也很困惑,为什么在返回值时firebug中没有json选项卡,只有resosse和html。这正常吗?请注意,输入值时使用,作为分隔符

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']);
    $activity = 'New Intake';
    $submit = mysql_real_escape_string($_POST['submit']);
    $boxerrortext = 'You must enter a box for intake';

    $array = explode(',', $_POST['BA_box']);

        if (isset($_POST['submit']))   {
          foreach ($array as $box) {


          $form=array('dept'=>$dept,
                 'company'=>$company,
                 'address'=>$address,
                 'service'=>$service,
                 'box'=>$box,
                 'destroydate'=>$destdate,
                 'authorised'=>$authorised,
                 'submit'=>$submit);

          $result[]=$form;

   }  

          echo json_encode( $result );
   }
jquery代码

submitHandler: function()   {
                if ($("#BA_boxform").valid() === true)  { 
                var data = $("#BA_boxform").serialize();
                $.post('/sample/admin/requests/boxes/boxesadd.php', data, function(msg) {
                $("#BA_addbox").html("You have entered box(es): " + "<b>" + msg[0].box + "  " + msg[1].box + "</b><br /> You may now close this window.");
                //console.log(msg[0].box);
                $("#BA_boxform").get(0).reset();
                }, 'json');

         } else

         { 
           return; 
         }
        },
        success:    function(msg)   {
                //$("#BA_addbox").html("You have entered a box");
                //$("#BA_boxform").get(0).reset();
        }
submitHandler:function(){
if($(“#BA#u boxform”).valid()==true){
var data=$(“#BA#u boxform”).serialize();
$.post('/sample/admin/requests/box/boxesadd.php',数据,函数(msg){
$(“#BA#u addbox”).html(“您已输入框:”+“”+msg[0]。框+“”+msg[1]。框+”
您现在可以关闭此窗口。”); //console.log(消息[0].box); $(“#BA#u boxform”).get(0.reset(); }“json”); }否则 { 返回; } }, 成功:功能(msg){ //$(“#BA#u addbox”).html(“您已经输入了一个框”); //$(“#BA#u boxform”).get(0.reset(); }
关于如何使用
$。每个
功能

var html = "You have entered box(es): <b>"
$.each(msg, function(index, element) { 
    if(index > 0)
         html += " ";
    html += element.box;
});
html += "</b><br /> You may now close this window."
var html=“您已输入框:”
$.each(msg,函数(索引,元素){
如果(索引>0)
html+=“”;
html+=element.box;
});
html+=“
您现在可以关闭此窗口。”
关于如何使用
$。每个
功能

var html = "You have entered box(es): <b>"
$.each(msg, function(index, element) { 
    if(index > 0)
         html += " ";
    html += element.box;
});
html += "</b><br /> You may now close this window."
var html=“您已输入框:”
$.each(msg,函数(索引,元素){
如果(索引>0)
html+=“”;
html+=element.box;
});
html+=“
您现在可以关闭此窗口。”
您的结果将根据您的输出作为javascript中的数组进行计算。请看参考资料:

为了演示的目的,我刚刚对您的方法的输出进行了两个以上的虚拟测试。因此,php脚本的结果可能如下所示:

 [{
    "dept": "dept",
    "company": "conmpany",
    "address": "address",
    "service": "service",
    "box": "box1",
    "destroydate": "destroydate",
    "authorised": "authorised",
    "submit": "submit"
}, {
    "dept": "dept",
    "company": "conmpany",
    "address": "address",
    "service": "service",
    "box": "box2",
    "destroydate": "destroydate",
    "authorised": "authorised",
    "submit": "submit"
}, {
    "dept": "dept",
    "company": "conmpany",
    "address": "address",
    "service": "service",
    "box": "box3",
    "destroydate": "destroydate",
    "authorised": "authorised",
    "submit": "submit"
}]
$.post('/sample/admin/requests/boxes/boxesadd.php', data, function(msg) {
                var messageOutput = '';
                for (var i = 0; i<msg.length; i++){
                    messageOutput += msg[i].box+'  ';     
                }
                $("#BA_addbox").html("You have entered box(es): " + "<b>" + messageOutput + "</b><br /> You may now close this window.");
                //console.log(msg[0].box);
                $("#BA_boxform").get(0).reset();
            }, 'json');
在javascript方面,这是作为对象数组处理的。由于是一个标准数组,
循环的
足以检索值,因此您的方法可能如下所示:

 [{
    "dept": "dept",
    "company": "conmpany",
    "address": "address",
    "service": "service",
    "box": "box1",
    "destroydate": "destroydate",
    "authorised": "authorised",
    "submit": "submit"
}, {
    "dept": "dept",
    "company": "conmpany",
    "address": "address",
    "service": "service",
    "box": "box2",
    "destroydate": "destroydate",
    "authorised": "authorised",
    "submit": "submit"
}, {
    "dept": "dept",
    "company": "conmpany",
    "address": "address",
    "service": "service",
    "box": "box3",
    "destroydate": "destroydate",
    "authorised": "authorised",
    "submit": "submit"
}]
$.post('/sample/admin/requests/boxes/boxesadd.php', data, function(msg) {
                var messageOutput = '';
                for (var i = 0; i<msg.length; i++){
                    messageOutput += msg[i].box+'  ';     
                }
                $("#BA_addbox").html("You have entered box(es): " + "<b>" + messageOutput + "</b><br /> You may now close this window.");
                //console.log(msg[0].box);
                $("#BA_boxform").get(0).reset();
            }, 'json');
$.post('/sample/admin/requests/box/boxesadd.php',数据,函数(msg){
var messageOutput='';

对于(var i=0;i您的结果将作为javascript中的一个数组从您的输出中进行计算。请查看参考:

出于演示目的,我刚刚在两个以上的框中对方法的输出进行了虚拟测试。因此,php脚本的结果可能如下所示:

 [{
    "dept": "dept",
    "company": "conmpany",
    "address": "address",
    "service": "service",
    "box": "box1",
    "destroydate": "destroydate",
    "authorised": "authorised",
    "submit": "submit"
}, {
    "dept": "dept",
    "company": "conmpany",
    "address": "address",
    "service": "service",
    "box": "box2",
    "destroydate": "destroydate",
    "authorised": "authorised",
    "submit": "submit"
}, {
    "dept": "dept",
    "company": "conmpany",
    "address": "address",
    "service": "service",
    "box": "box3",
    "destroydate": "destroydate",
    "authorised": "authorised",
    "submit": "submit"
}]
$.post('/sample/admin/requests/boxes/boxesadd.php', data, function(msg) {
                var messageOutput = '';
                for (var i = 0; i<msg.length; i++){
                    messageOutput += msg[i].box+'  ';     
                }
                $("#BA_addbox").html("You have entered box(es): " + "<b>" + messageOutput + "</b><br /> You may now close this window.");
                //console.log(msg[0].box);
                $("#BA_boxform").get(0).reset();
            }, 'json');
在javascript方面,这是作为对象数组处理的。由于这是一个标准数组,因此
循环的
将足以检索值,因此您的方法可能如下所示:

 [{
    "dept": "dept",
    "company": "conmpany",
    "address": "address",
    "service": "service",
    "box": "box1",
    "destroydate": "destroydate",
    "authorised": "authorised",
    "submit": "submit"
}, {
    "dept": "dept",
    "company": "conmpany",
    "address": "address",
    "service": "service",
    "box": "box2",
    "destroydate": "destroydate",
    "authorised": "authorised",
    "submit": "submit"
}, {
    "dept": "dept",
    "company": "conmpany",
    "address": "address",
    "service": "service",
    "box": "box3",
    "destroydate": "destroydate",
    "authorised": "authorised",
    "submit": "submit"
}]
$.post('/sample/admin/requests/boxes/boxesadd.php', data, function(msg) {
                var messageOutput = '';
                for (var i = 0; i<msg.length; i++){
                    messageOutput += msg[i].box+'  ';     
                }
                $("#BA_addbox").html("You have entered box(es): " + "<b>" + messageOutput + "</b><br /> You may now close this window.");
                //console.log(msg[0].box);
                $("#BA_boxform").get(0).reset();
            }, 'json');
$.post('/sample/admin/requests/box/boxesadd.php',数据,函数(msg){
var messageOutput='';

对于(var i=0;没有JSON选项卡,原因响应与非JSON响应类型标题一起提供(例如,
text/html
而不是
application/JSON
)。在我的例子中,json选项卡仅针对json对象而不是json数组显示,它似乎与头类型无关。我不发送任何头。没有json选项卡,因为“响应”与非json响应类型头一起提供(例如,
text/html
,而不是
application/json
)。在我的例子中,json选项卡仅针对json对象而不是json数组显示,它似乎与标题类型无关。我不发送任何标题。我将在何处放置div以显示代码中的消息:#BA_addbox。许多感谢您的示例。如果我在此处放置:var html=$(“#BA_addbox”).html(“您已输入框):”)然后只显示消息,没有项目。你能纠正我哪里出错了吗?是否应该使用if函数?非常感谢你的帮助。我将发布一个与我的问题相关的新主题。thanks@user1532468在我的代码中,我创建了一个名为
html
的变量,并在这里放置了一个字符串,然后连接了一些其他变量在代码变量
html
中包含一个jQuery对象,该对象包含对id为BA#u addbox的DOM元素的引用,因为“
.html(“…”)
只是将传递的文本放在DOM元素中并返回一个jQuery对象,所以您可以执行类似于
$('.#BA#u addbox').html('string').addClass('myClazz').css的操作('color'、'red');
我应该将div放在哪里以显示代码中的消息:#BA_addbox。很多感谢您的示例。如果我放在这里:var html=$(“#BA_addbox”).html(“您已经输入了框):”)然后只显示消息,没有项目。你能纠正我哪里出错了吗?是否应该使用if函数?非常感谢你的帮助。我将发布一个与我的问题相关的新主题。thanks@user1532468在我的代码中,我创建了一个名为
html
的变量,并在这里放置了一个字符串,然后连接了一些其他变量在代码变量
html
中包含一个jQuery对象,该对象包含对id为BA#u addbox的DOM元素的引用,因为“
.html(“…”)
只是将传递的文本放在DOM元素中并返回一个jQuery对象,所以您可以执行类似于
$('.#BA#u addbox').html('string').addClass('myClazz').css的操作(“颜色”、“红色”);