Storing Form Text Fields as Array within $_POST variable
I'm creating an HTML form using jQuery that has certain text boxes grouped together under a single question (e.g., "List each URL that this request applies to.") For that question (and others), there are 3 text boxes below it along with a button to add additional text boxes if necessary. When I pass this form data to be processed by PHP, how can I have all these text box values be grouped together as a single array variable within the $_POST array开发者_运维技巧 variable? I tried giving all the text boxes the same name attribute followed by brackets, but that didn't seem to work (e.g., <input name='myarray[]' type='text' />
). Any suggestions?
EDIT: Here are the specifics on the error I'm getting:
I'm using this in PHP
$myarray ='';
foreach ($_POST['myarray'] as $value) {
$myarray .= $value . '\n';
}
The error I get is: "Warning: Invalid argument supplied for foreach()"
foreach($_POST["myarray"] as $myarray)
{
echo $myarray . "";
}
This is quite simple. I suppose you are using $_POST['myarray[]'] (notice brackets[]) which is incorrect. This example should work perfectly fine.
精彩评论