将多个数组的ajax数据发布到php

时间:2022-09-19 21:35:13

I've successfully posted a single serialized array, but I can't figure out how to post more than one array in an AJAX post. Here is my code:

我已成功发布了一个序列化数组,但我无法弄清楚如何在AJAX帖子中发布多个数组。这是我的代码:

HTML

HTML

<td><input type="text" name='item_name[]' id="item_name" class="form-control" autocomplete="off"></td>
<td><input type="number" name='quantity[]' id="quantity" class="form-control" autocomplete="off"></td>

jquery

jQuery的

$("#create_order").click(function(){  

    var item_name = $('[name="item_name[]"]').serialize();
    var quantity = $('[name="quantity[]"]').serialize();

    $.ajax({

        url: "includes/ajax_new_order.php",
        data: {item_name:item_name, quantity:quantity},

        type: "POST",

        success:function(data){ 

            $("#editModal").modal('hide'); 

            $('#create_order').html('<span class="glyphicon glyphicon-ok" aria-hidden="true"></span> Save');
        }
    });
});

PHP

PHP

<?php require("../init.php");

    $item_name = $_POST['item_name'];
    $quantity = $_POST['quantity'];

    foreach (array_combine($item_name, $quantity) as $key1 => $key2) {

      $query = $database->query("INSERT INTO order_tb(item,quantity) VALUES('$key1','$key2') ");
      if ($query) {
        echo "<p>Success</p>";
      }
      else { 
        echo "<p>Failed</p>"; 
      } 
    }
?>

One array works fine, however when I try to add a second array quantity to the data: field, it doesn't work.

一个数组工作正常,但是当我尝试向data:字段添加第二个数组数量时,它不起作用。

1 个解决方案

#1


-1  

You can use map function instead of serialize function. Here is a sample code. Just replaced your jquery code by this bellow sample code.

您可以使用map函数而不是serialize函数。这是一个示例代码。刚用这个下面的示例代码替换了你的jquery代码。

$("#create_order").click(function(){  

    //var item_name = $('[name="item_name[]"]').serialize();
    //var quantity = $('[name="quantity[]"]').serialize();

    var item_name =$('[name="item_name[]"]').map(function(){return $(this).val();}).get();
    var quantity = $('[name="quantity[]"]').map(function(){return $(this).val();}).get();


    $.ajax({

        url: "includes/ajax_new_order.php",
        data: {item_name:item_name, quantity:quantity},

        type: "POST",

        success:function(data){ 

            $("#editModal").modal('hide'); 

            $('#create_order').html('<span class="glyphicon glyphicon-ok" aria-hidden="true"></span> Save');
        }
    });
});

#1


-1  

You can use map function instead of serialize function. Here is a sample code. Just replaced your jquery code by this bellow sample code.

您可以使用map函数而不是serialize函数。这是一个示例代码。刚用这个下面的示例代码替换了你的jquery代码。

$("#create_order").click(function(){  

    //var item_name = $('[name="item_name[]"]').serialize();
    //var quantity = $('[name="quantity[]"]').serialize();

    var item_name =$('[name="item_name[]"]').map(function(){return $(this).val();}).get();
    var quantity = $('[name="quantity[]"]').map(function(){return $(this).val();}).get();


    $.ajax({

        url: "includes/ajax_new_order.php",
        data: {item_name:item_name, quantity:quantity},

        type: "POST",

        success:function(data){ 

            $("#editModal").modal('hide'); 

            $('#create_order').html('<span class="glyphicon glyphicon-ok" aria-hidden="true"></span> Save');
        }
    });
});