Forum Moderators: coopster
Hopefully you will find this an easy one. I am giving users a selection of checkboxes in a form that they can select. I want to collect the information from the responses to store in a database.
Here is some code which seperates out the reasons given and lists them to the screen:
if(isset($_POST['reasons'])) {
foreach(($_POST['reasons']) as $reasongiven => $Arrayitem) {
echo "$Arrayitem <br>";
}
Can anyone please tell me how to also set a new variable to each item that is being echoed to the screen?
I want to echo the item, but also set a variable to contain this information so that I can then pass the variables into a database and send them via email.
Thank you for helping
Just because<input type='checkbox' name='reasons[]' value='just because' /><br />
No reason<input type='checkbox' name='reasons[]' value='none' />
echo '<pre>';
[url=http://us3.php.net/print_r]print_r[/url]($_POST['reasons']);
echo '</pre>';
eelixduppy is right and you could get the whole array into the DB directly from the $_POST array.
The most obvious way (to me) is to use the implode() function to create an SQL friendly list of comma separated values under 1 new variable name.
So something like : -
if(isset($_POST['reasons'])) {
$imploded = implode(",",$_POST['reasons'])
}
this would create a variable called $imploded with a value of 'value1, value2, value3, value4' which could then be used as a part of an SQL statement - of course, you still need to construct the rest of the SQL statement to make use of $imploded ;-)
Hope this helps, Rolf
For that, you can refer to this thread on Basics of Submitting and Emailing Forms with PHP [webmasterworld.com] taken from our Library [webmasterworld.com]. Good luck ;)