I have this piece of code in my PHP code:
while ($row = mysqli_fetch_assoc($result))
{
extract($row);
echo "<tr>";
echo "<td bgcolor='#FFFFFF'><input id='bookArray[]' name='bookArray[]' type='checkbox' value='$book_id' />$book_id</td>";
echo "<td bgcolor='#FFFFFF'>$threat_name</td>";
echo "</tr>";
}
In HTML page, I want to use jQuery serialize() method to sent array of selected books in bookArray[]. In my JavaScript,
var selectedbooks = $("book_form").serialize();
alert (selectedbooks);
In the alert message box, i did not get any value (empty string).
Previously when i am using Prototype, it worked nicely.
Thanks Tomalak.
In the PHP file, I am using the following code now:
**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
Inside saveBookList.php to process the serialize form, i got this error "Invalid argument supplied for foreach()".
Inside saveBookList.php,
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?
If you have to post that array only and not other input fields, this is a clean and quick solution:
Explaination:
The selector ^= selects all elements with a name attribute value starting with 'bookArray', the open square bracket '[' makes sure the element is the array we want to serialize and not just a variable starting with the string 'bookArray'.