Reputation: 151
I am currently creating a form with checkboxes in it. At the minute i have it working that it is only adding the last selected checkbox to the database table. I need it to add all the selected values to the table. I think i may need to use implode but i am unsure where to put it. Any help would be greatly appreciated.
actionplan.php
<h1>My Action Plan</h1>
<?php
if (isset($_GET['success']) && empty($_GET['success'])) {
} else {
if (empty($_POST) === false && empty($errors) === true) {
$actionplan = array(
'studentid' => $user_data['studentid'],
'interests_hobbies' => $_POST['interests_hobbies'],
);
actionplan($actionplan);
header ('Location: actionplan.php');
exit();
} else if(empty($errors) === false){
echo output_errors($errors);
}
?>
<form action="" method="post">
<li>
<p class="p4">
What are your interests and hobbies?*
<center>
<table border="0">
<tr>
<td><p class="p6"><input type="checkbox" name="interests_hobbies" value="Animal Care"/>Animal Care</td>
<td><p class="p6"><input type="checkbox" name="interests_hobbies" value="Computer Games"/>Computer Games </td>
<td><p class="p6"><input type="checkbox" name="interests_hobbies" value="Gardening"/>Gardening</td>
<td><p class="p6"><input type="checkbox" name="interests_hobbies" value="Internet Browsing"/>Internet Browsing</td>
<td><p class="p6"><input type="checkbox" name="interests_hobbies" value="Sculpting"/>Sculpting</td>
</tr>
<tr>
<td><p class="p6"><input type="checkbox" name="interests_hobbies" value="Bird Watching "/>Bird Watching</td>
<td><p class="p6"><input type="checkbox" name="interests_hobbies" value="Fishing"/>Fishing</td>
<td><p class="p6"><input type="checkbox" name="interests_hobbies" value="Golfing"/>Golfing </td>
<td><p class="p6"><input type="checkbox" name="interests_hobbies" value="Painting"/>Painting </td>
<td><p class="p6"><input type="checkbox" name="interests_hobbies" value="Social Networking"/>Social Networking</td>
</tr>
<tr>
<td><p class="p6"><input type="checkbox" name="interests_hobbies" value="Bowling"/>Bowling</td>
<td><p class="p6"><input type="checkbox" name="interests_hobbies" value="Food Tasting"/>Food Tasting </td>
<td><p class="p6"><input type="checkbox" name="interests_hobbies" value="Gymnastics"/>Gymnastics </td>
<td><p class="p6"><input type="checkbox" name="interests_hobbies" value="Reading"/>Reading </td>
<td><p class="p6"><input type="checkbox" name="interests_hobbies" value="Watching Movies"/>Watching Movies</td>
</tr>
<tr>
<td><p class="p6"><input type="checkbox" name="interests_hobbies" value="Camping"/>Camping</td>
<td><p class="p6"><input type="checkbox" name="interests_hobbies" value="Football"/>Football </td>
<td><p class="p6"><input type="checkbox" name="interests_hobbies" value="Interior Design"/>Interior Design </td>
<td><p class="p6"><input type="checkbox" name="interests_hobbies" value="Surfing"/>Surfing </td>
<td><p class="p6"><input type="checkbox" name="interests_hobbies" value="Yoga"/>Yoga</td>
</tr>
</table>
<li>
<input type="submit" value="Submit">
</li>
</ul>
</form>
users.php
function actionplan($actionplan) {
array_walk($actionplan, 'array_sanitize');
$fields = '`' . implode('`, `', array_keys($actionplan)) . '`';
$data = '\'' . implode('\', \'', $actionplan) . '\'';
mysql_query("INSERT INTO `actionplan` ($fields) VALUES ($data)");
}
Upvotes: 1
Views: 450
Reputation: 2109
You need to include braces in PHP:
<input type="checkbox" name="interests_hobbies[]"...
This will give you an array in $_POST $_POST['interests_hobbies']
. Remember that you only get a POST value if the checkbox is checked. So you could potentially have a null value for $_POST['interests_hobbies'] if no checkboxes are checked, so make sure your code handles that.
Update: If user.php is the script handling your POST, then your form action needs to be:
<form action="users.php" method="post">
Then at the top of users.php, add this code to dump the POST to the screen:
echo "<pre>";var_dump($_POST['interests_hobbies']);echo "</pre>"
(You don't need the tags, but I like them when I use var_dump inside HTML.)
Once you have verified that you are getting the right POST values, you can proceed from there to output or store the values.
Upvotes: 1