0

目前,我正在使用一个脚本,它以 msg[0].box msg[1].box 等格式从 json 返回一个值。如果用户输入说 2 个项目,那没关系。但是,如果用户输入的内容不止这些怎么办。我的一个朋友建议我查看 jquery 每个函数以重新调整多个值,但鉴于我对 jquery 还很陌生,如果有人可以帮助我使用我的代码实现这一点,我将不胜感激。

我也很困惑为什么当返回值时firebug中没有json选项卡,只有reposnse和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();
        }
4

2 回答 2

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."
于 2013-07-08T09:30:24.457 回答
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/

firebug 上的 json 选项卡

于 2013-07-08T10:25:49.187 回答