首页 文章

从json返回多个值

提问于
浏览
0

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

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

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>
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 回答

  • 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>
    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

  • 3

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

    var html = "You have entered box(es): <b>"
    $.each(msg, function(index, element) { 
        if(index > 0)
             html += " ";
        html += element.box;
    });
    html += "</b>
    You may now close this window."

相关问题