简体   繁体   English

jquery json没有返回值

[英]jquery json not returning value

I am using jquery to submit a form using serialize and all is well except if I input more than 1 box. 我使用jquery使用序列化提交表单,一切都很好,除非我输入超过1个框。 If I input 1 box, the msg.box appears in #BA_addbox. 如果我输入1个框,则msg.box出现在#BA_addbox中。 If however I enter more than 1 box using , as delimiter, then no msg is shown and no json tab appears in firebug. 但是,如果我使用as作为分隔符输入多个框,则不显示消息,并且在firebug中不显示json选项卡。 just the html which is correct. 只是正确的HTML。 Where have I gone wrong with code. 代码在哪里出错了?

I have created an array and using foreach with explode to seperate the values but no multiple value being returned. 我创建了一个数组并使用foreach with explode来分隔值,但没有返回多个值。 Thanks 谢谢

UPDATE: vars are being collected in the php script like thus: 更新: vars正在PHP脚本中收集,如下所示:

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

First fix the php to return a valid json array. 首先修复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 );
}

Then the msg parameter in the post callback should be an array of results, so you can't just do msg.box to get the list of boxes. 然后post回调中的msg参数应该是结果数组,因此你不能只使用msg.box来获取框的列表。 I would suggest something like this: 我会建议这样的事情:

boxes = jQuery.map(msg,function(item){
  return item.box;
}).join(',');

That extracts the box property from each item in the array and joins them into a comma separated list. 它从数组中的每个项目中提取box属性,并将它们连接到逗号分隔列表中。 You can then display that list like this: 然后,您可以像这样显示该列表:

$("#BA_addbox").html("You have entered box(es): " + "<b>" + boxes + 
  "</b><br /> You may now close this window.");

Given these changes, your code works for me. 鉴于这些变化,您的代码适合我。 If you're having other problems, I suggest you post more of your html - in particular your form structure. 如果您遇到其他问题,我建议您发布更多的html - 特别是您的表单结构。 It's possible your form isn't submitted the correct values to get a valid response from the server. 您的表单可能未提交正确的值以从服务器获得有效响应。

Per @nnnnnn: Per @nnnnnn:

Your resulting json looks like the structure below if there is more than one box. 如果有多个框,则生成的json看起来像下面的结构。 This is invalid json, and therefore can not be reliably parsed. 这是无效的json,因此无法可靠地解析。

{
  ...
}{
  ...
}

To fix this, you have to add the arrays to another array, then encode the parent array. 要解决此问题,您必须将数组添加到另一个数组,然后对父数组进行编码。

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

This will result in the following structure and for a variable data that contains the parsed json each box can be retrieved via data[0] , data[1] etc. 这将导致以下结构,对于包含已解析的json的可变data ,可以通过data[0]data[1]等检索每个框。

[
  {
    ...
  },
  {
    ...
  }
]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM