3

我正在使用 jquery 使用序列化提交表单,一切都很好,除非我输入超过 1 个框。如果我输入 1 个框,则 msg.box 出现在 #BA_addbox 中。但是,如果我使用 , 作为分隔符输入超过 1 个框,则不会显示 msg,并且 firebug 中不会出现 json 选项卡。只是正确的html。我的代码哪里出错了。

我创建了一个数组并使用 foreach 和explode 来分隔值,但没有返回多个值。谢谢

更新:在 php 脚本中正在收集 vars,如下所示:

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代码

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

2 回答 2

1

First fix the php to return a valid json 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);
    $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. 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. 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. It's possible your form isn't submitted the correct values to get a valid response from the server.

于 2013-07-06T13:47:07.543 回答
1

每@nnnnnn:

如果有多个框,则生成的 json 看起来像下面的结构。这是无效的 json,因此无法可靠地解析。

{
  ...
}{
  ...
}

要解决此问题,您必须将数组添加到另一个数组,然后对父数组进行编码。

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

这将产生以下结构,对于data包含已解析 json 的变量,可以通过 等检索每个data[0]data[1]

[
  {
    ...
  },
  {
    ...
  }
]
于 2013-07-06T13:50:15.243 回答