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

后端 未结 14 666
庸人自扰
庸人自扰 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:43

    Thanks Tomalak.

    In the PHP file, I am using the following code now:

    while ($row = mysqli_fetch_assoc($result)) {
        extract($row);
        echo "<tr>";
            echo "<td bgcolor='#FFFFFF'><input id='$book_id' name='bookArray[]' type='checkbox' value='$book_id' />$book_id</td>";
            echo "<td bgcolor='#FFFFFF'>$book_name</td>";
        echo "</tr>";
    } // while
    

    **The book_id is unique.

    Using tvanfosson solution, now i am able to get array of input value

    var selectedBooks = $('form#book_form').serialize(); alert (selectedBooks);

    From the alert message box, I get ==> bookArray%5B%5D=T2.05&bookArray%5B%5D=T2.81

    Now, when I sent the serialize input value to PHP file using JQuery

    var selectedBooks   = $('form#book_form').serialize();
    alert (selectedBooks);
    
    var url = 'saveBookList.php';
    
    // Send to server using JQuery
    $.post(url, {bookArray: selectedBooks}, function(responseData) {
        $("#save-result").text(responseData);
    });
    

    Inside saveBookList.php to process the serialize form, i got this error "Invalid argument supplied for foreach()".

    Inside saveBookList.php,

    // If you have selected from list box.
    if(isset($_POST['bookArray'])) {
    
        // Get array or bookID selected by user
        $selectedBookId = $_POST['bookArray'];
        echo $selectedBookId;
    
        foreach($selectedBookId as $selectListItem) {
            echo "You sent this -->" . $selectListItem . "\n";
        }
    }
    

    The PHP code above works fine if i am sending using Prototype.

    For Prototype when i do echo $selectedBookId;

    I got Array.

    For JQuery, when i do echo $selectedBookId;

    I got ==> bookArray%5B%5D=T4.11&bookArray%5B%5D=T4.38

    My question, does jQuery support array value for post method?

    0 讨论(0)
  • 2020-12-16 15:48
    var data = $(form).serialize();
    $.post('post.php', '&'+data);
    
    0 讨论(0)
  • 2020-12-16 15:49

    Prototype / Scriptaculous serialize functionality for jQuery:

    <script>
    function toObjectMap( queryString )
    {
        return '{\'' + queryString.replace(/=/g, '\':\'').replace(/&/g, '\',\'') + '\'}';
    }
    </script>
    ...
    <div id="result"></div>
    ...
    <form onSubmit="$('#result').load( ajaxURL, toObjectMap( $('#formId').serialize() ) );" ...>
    ...
    
    0 讨论(0)
  • 2020-12-16 15:50
    var arTags = new Array();
    
    jQuery.map( $("input[name='tags[]']") , function(obj,index)
    {
       arTags .push($(obj).val());
    });
    
    var obj = {'new_tags'           : $("#interest").val() ,
               'allready_tags[]'  : arTags };
    
    var post_data = jQuery.param(obj,true);
    
    $.ajax({
          type :  'POST',
          url  :  'some_url',
          data :  post_data,
          dataType : "html",
          success: function(htmlResponse)
          {
    
          }
    });
    
    0 讨论(0)
  • 2020-12-16 15:51

    You may need to change your PHP as @Tomalak suggests, but you will also need to change your javascript. To reference a named element use the #name selector:

    var selectedbooks = $('form#book_form').serialize();;
    
    0 讨论(0)
  • 2020-12-16 15:53

    easy: serialize().replace(/%5B%5D/g, '[]')

    0 讨论(0)
提交回复
热议问题