Reputation: 1
I have a form with text and number input fields and then there are also some checkboxes. I can get the text into the database from the input fields, but I can not get it to work with checkboxes, when I have more than 1. Here is the code of 2 out of the 4 I have in my form.
<dl class="inputCheckbox">
<dt><label for="interests">Nyt/brugt:</label></dt>
<dd>
<input type="checkbox" name="newUsed[]" value="Nyt" />
<label for="new" class="opt">Nyt</label>
<input type="checkbox" name="newUsed[]" value="Brugt" />
<label for="used" class="opt">Brugt</label>
<input type="checkbox" name="newUsed[]" value="Nyt/Brugt" />
<label for="both" class="opt">Nyt/Brugt</label>
</dd>
</dl>
<dl class="inputCheckbox">
<dt><label for="interests">Diverse:</label></dt>
<dd>
<input type="checkbox" name="various[]" value="El" />
<label for="electricity" class="opt">El</label>
<input type="checkbox" name="various[]" value="Vand" />
<label for="water" class="opt">Vand</label>
<input type="checkbox" name="various[]" value="El/Vand" />
<label for="both" class="opt">El/Vand</label>
</dd>
</dl>
I hope that there are some who can help me with some PHP code :)
Here's the code of how I got the other things like text and number from the label into the database.
<?php
session_start();
include('connection.php');
$cvr=$_POST['cvr'];
$cvr=$_POST['companyName'];
$fName=$_POST['fName'];
$lName=$_POST['lName'];
$streetName=$_POST['streetName'];
$streetNumber=$_POST['streetNumber'];
$zip=$_POST['zip'];
$city=$_POST['city'];
$phone=$_POST['phone'];
$eMail=$_POST['eMail'];
$repeatEmail=$_POST['repeatEmail'];
$locationNumber=$_POST['locationNumber'];
$size=$_POST['size'];
$products=$_POST['products'];
$comments=$_POST['comments'];
$newUsed=$_POST['newUsed'];
$various=$_POST['various'];
$sleep=$_POST['sleep'];
$reOrder=$_POST['reOrder'];
mysql_query("INSERT INTO creatUser(cvr, fName, lName, streetName, streetNumber, zip, city, phone, eMail, repeatEmail, locationNumber, size, products, comments)
VALUES('$cvr', '$fName', '$lName', '$streetName', '$streetNumber', '$zip', '$city', '$phone', '$eMail', '$repeatEmail', '$locationNumber', '$size', '$products', '$comments')");
header("location: creatUser.php?remarks=success");
mysql_close($con);
?>
Upvotes: 0
Views: 1442
Reputation: 1932
You will have to serialize the checkbox inputs in some way. Usually explode()
and implode()
is used for this. Using your code:
$newUsedSerialized = implode(",", $_POST["newUsed"]); # Sanitize $_POST["newUsed"] first!
Then, get your inputs back into an array with
$newUsedArray = explode(",", $queryResult["newUsed"]);
And so you can use $newUsedArray
in your PHP code for generating the forms:
$inputField = "";
$inputField .= '<input type="checkbox" name="newUsed[]" checked="' . (in_array("Nyt", $nytUsedArray) ? "checked" : "") . '/>';
(In this case, consider the use of numerical values
instead - it saves space.)
Upvotes: 0
Reputation: 2673
You can save checkboxes in a comma separated format.
In you PHP script you can do
$checkboxData = implode(",", $_POST['various']);
When repopulating your data you can do in your view
<?php $checkboxValues = explode(",", $checkboxData); ?>
<input type="checkbox" name="various[]" value="El" <?php if(in_array("EL", $checkboxValues)) echo 'selected="selected";?>/>
Upvotes: 2
Reputation: 22711
Can you try,
$various=$_POST['various'];
$VariousData ='';
foreach($various as $k=>$val){
$VariousData .=$val." ";
}
You can use to store $VariousData
into your table
Upvotes: 0