从json返回多个值

时间:2013-07-08 09:24:55

标签: php javascript jquery

目前,我正在使用一个脚本,该脚本以msg [0] .box msg [1] .box等格式从json返回一个值。如果用户输入了说2项,那就没问题了。但是,如果用户输入的内容超过了这一点,我的一个朋友建议我查看jquery每个函数来重新调用多个值,但看到我对jquery相当新,我会很高兴有人可以帮助我使用我的代码实现这一点。

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

php code

    $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();
        }

2 个答案:

答案 0 :(得分:3)

关于如何使用$.each功能......

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."

答案 1 :(得分:2)

您的结果将在输出中以javascript的形式计算为数组。看一下参考文献:http://www.json.org/

出于演示目的,我只是虚拟测试了超过2个方框的方法输出。因此,您的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"
}]

在javascript方面,它被处理为一个对象数组。由于是标准数组,for循环足以检索值,因此您的方法可能如下所示:

$.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');

当然,您可以使用$.each功能,但在需要铲子时并不总是使用推土机的解决方案。

至于firebug中的json输出已经从版本1.4a11开始:http://www.softwareishard.com/blog/firebug/json-explorer-for-firebug/

json tab on firebug