How to send serialize form data using JQuery if the input element is an array

后端 未结 14 687
庸人自扰
庸人自扰 2020-12-16 15:16

I have this piece of code in my PHP code:

while ($row = mysqli_fetch_assoc($result))
{
    extract($row);
    echo \"\";
    echo \"

        
14条回答
  •  死守一世寂寞
    2020-12-16 15:55

    Problem solved! Here is what i did.

    Inside PHP file to create rows of dynamic checkboxes,

      while ($row = mysqli_fetch_assoc($result))
      {
       extract($row);
       echo "";
       echo "$book_id";
       echo "$book_name";
       echo "";
      } // while
    

    I do not use JQuery $.post method anymore. I changed my code to the following

        var my_query_str = '';
    
    $("input[@type='checkbox'][@name='books']").each(
      function()
      {
        if(this.checked)
        {
               my_query_str += "&bookArray[]=" + this.value;
        }
      });
    
    
    $.ajax(
    {
        type: "POST",
        url: "saveBookList.php",
        data: "dummy_data=a" + my_query_str,
        success:
            function(responseData)
            {
                $("#save-result").empty().append(responseData);
            },
        error:
            function()
            {
                $("#save-result").append("An error occured during processing");
            }
    });
    

    Now, inside saveBookList.php page, value of $_POST['bookArray'] is an Array. Yey! Yey!

    I do hope and wish the $.post method of JQuery can support array data type as Prototype does. :)

提交回复
热议问题