I want to store multiple checkbox value in database ,and my checkbox name is differnt,how can save ??.I have three column like Called them,Called you,toured, how i can store each value in data base.
HTML Code
<input type="checkbox" name="Called_them[]" value="1">1<br>
<input type="checkbox" name="Called_them[]" value="2">2<br>
<input type="checkbox" name="Called_you[]" value="1">1<br>
<input type="checkbox" name="Called_you[]" value="2">2<br>
PHP Code:-
if(isset($_POST['submits']) )
{
$checked = $_POST['alled_them'];
$checked1 = isset($_POST['alled_them']);
for($i=0; $i < count($checked); $i++){
$tellfriend=new MemberEmails;
$tellfriend->called_them = isset($checked[$i])==0?'':$checked[i];
$tellfriend->save();
}
if(isset($_POST["submit"])) {
foreach($_POST['called_them'] as $called_them) {
//do things
}
foreach($_POST['called_you'] as $called_you) {
// do things
}
}
$array_called_them = $_POST['called_them'];
$array_called_them = $_POST['called_you];
You now have both checkbox groups as arrays. if you want the value from the array just use foreach on each of the arrays.
Related
I created a contact form for the inquiry of customers, category and subcategory I was create using the checkboxes, it is the format of accordion
feature and subfeature are the checkboxes, I am using the insert query for the storing of the database, but I can't insert approximate feature with subfeatures.
three tables are here such as 'iv_mail',iv_mail_features','iv_mail_subfeatures`.
shown below the code
$data=array('name'=>$this->input->post('sendname'),
'lastname'=>$this->input->post('sendlname'),
'mobile'=>$this->input->post('sendphone'),
'location'=>$this->input->post('sendlocation'));
$this->db->insert('iv_mail',$data);
$insert_id=$this->db->insert_id();
foreach($this->input->post('features') as $feature){
$data=array('mail_id'=>$insert_id,
'featurename'=> $feature);
$this->db->insert('iv_mail_features',$data);
$sub_id=$this->db->insert_id();
foreach($this->input->post('sub_feature') as $subfeature){
$data=array('feature_id'=>$sub_id,
'subfeaturename'=> $subfeature);
$this->db->insert('iv_mail_subfeatures',$data);
}
}
here i used insert_id, anyone can help me
You must do name all checkbox as an array like this.
<input type="checkbox" name="checkbox[]" value="add"><hr>
<input type="checkbox" name="checkbox[]" value="edit"><hr>
<input type="checkbox" name="checkbox[]" value="update"><hr>
<input type="checkbox" name="checkbox[]" value="delete">
then in your function must be like this
$inputnum = count($this->input->post('checkbox'));
for ($i=0;$i < $inputnum; $i++) {
echo $this->input->post('checkbox')[$i].'<hr>';
}
You can change the code as you want.
also, you can add all features as json in one column like this
$inputnum = count($this->input->post('checkbox'));
for ($i=0;$i < $inputnum; $i++) {
$checkbox[] = $this->input->post('checkbox')[$i].'<hr>';
}
json_encode($checkbox);
There are multiple checkbox about 50+ for user access, checkbox are like follows;
<input name="PREFIX_1" type="checkbox">
<input name="PREFIX_2" type="checkbox">
<input name="PREFIX_3" type="checkbox">
...........
I have multiple checkbox with its respective values which I loop through to add/update the values in the database, which is like follows:
foreach ($_POST as $field => $value ) {
if ( preg_match('/^PREFIX_/', $field) ) {
$access = isset($value)?'1':'0';
$file = substr($field, 4);
$this->db->query('UPDATE_DESIRED_TABLE');
}
}
The problem is when I add/update the values in the database by using loop foreach ($_POST as $field => $value ) and checking the prefix, I only get values of checkbox that are checked and the values of data that are not checked are not in the $_POST data.
And, I don't get any values which are not checked, which is not even the name of that checkbox. In other words only those values are set which are checked.
Now I have to change the value of all the data in the database by comparing both the data in the database and from the post data.
How can I solve this?
You would need to send hidden inputs along with the checkboxes like this:
<form action="" method="post">
<input type="hidden" id="PREFIX_1_" name="PREFIX_1" value="0">
<input type="checkbox" id="PREFIX_1" name="PREFIX_1" value="1" />
<input type="hidden" id="PREFIX_2_" name="PREFIX_2" value="0">
<input type="checkbox" id="PREFIX_2" name="PREFIX_2" value="1" />
<input type="hidden" id="PREFIX_3_" name="PREFIX_3" value="0">
<input type="checkbox" id="PREFIX_3" name="PREFIX_3" value="1" />
<input type="submit" name="sub" value="Go" />
</form>
However you cant use the isset anymore with this, change PHP like this:
if(isset($_POST['sub']))
{
foreach ($_POST as $field => $value )
{
if ( preg_match('/^PREFIX_/', $field) )
{
$access = $value;
$file = substr($field, 4);
$this->db->query('UPDATE_DESIRED_TABLE');
}
}
}
Considering you know the number of checkbox, stored in example in $checkBoxCount, you might use this kind of loop :
for ($i = 1; $i <= $checkBoxCount; $i++)
{
$access = ((isset($_POST["PREFIX_" . $i])) ? ('1') : ('0')));
//manage DB
}
I am storing content into a database table. One table column is called attrbutes and has a list of values such as (ex: 1, 3, 5) based on the checkboxes that were checked.
<form>
<input type="checkbox" name="attribute" value="1">Attr 1<br>
<input type="checkbox" name="attribute" value="2">Attr 2<br>
<input type="checkbox" name="attribute" value="3">Attr 3<br>
<input type="checkbox" name="attribute" value="4">Attr 4<br>
<input type="checkbox" name="attribute" value="5">Attr 5<br>
<form>
Couple of questions on how to integrate checkboxes with PHP...
1) How do I check to see if at least 1 checkbox is checked on form submit?
2) How do I turn the checked checkboxes into a list like 1, 3, 5 if checkboxes 1, 3, and 5 are selected.
3) As a reverse to #2, on page load I need to figure out how to check each checkbox that's value is listed in the database column. if 1, 3, 5 is listed in table column, I need checkboxes 1 3 and 5 checked on page load.
I know how to code the basic queries for inserting, updating, and removing etc...but I've never worked with checkboxes and storing values from checkboxes using php before.
Change you html:
<input type="checkbox" name="attribute[]" value="1">Attr 1<br>
<input type="checkbox" name="attribute[]" value="2">Attr 2<br>
<input type="checkbox" name="attribute[]" value="3">Attr 3<br>
<input type="checkbox" name="attribute[]" value="4">Attr 4<br>
<input type="checkbox" name="attribute[]" value="5">Attr 5<br>
1)
$checkedAttr = $_POST['attribute'];
if(count($checkedAttr) > 0)
echo "At least one checkbox is selected";
2)
$checkboxList = implode(',', $checkedAttr);
3)
$checkedAttr = explode(',', $yourQueryResultStringContainingTheCheckedList);
<input type="checkbox" name="attribute[]" value="1" <?php if(in_array('1', $checkedAttr)) echo 'checked=\"checked\"'; ?>Attr 1<br>
...
1, 2) You can treat form elements as arrays with name="attribute[]" then loop through the posted values in your php as an array.
For example:
<?php
$attributes = $_POST['attribute'];
if(empty($attributes)) {
echo "No attributes selected";
} else {
// echo whole array
print_r($attributes);
// loop through array
foreach($attributes as $attribute) {
echo $attribute." ";
}
// create list as one whole string
$list = implode(',', $attributes);
}
?>
3) When you are building the form (using php) you can check each value in a loop. Note that I also made your labels proper labels so they will also activate the checkbox if clicked.
<?php
// need some code to get db values into array
$attributes = array(1,3,5); // your list
// loop through the amount of checkboxes you want
for($i=1; $i <= 5; $i++) {
if(in_array($i, $attributs) { // check for a match with current checkbox
$checked = " checked";
} else {
$checked = "";
}
echo'<input type="checkbox" name="attribute[]" id="attribute'.$i.'" value="'.$i.'"'.$checked.'><label for="attribute'.$i.'">Attr 1</label><br>'
}
?>
Is it possible to populate a checkboxlist from the database values? If so, please suggest how to do this.
$query2 = "select * from Products where CategoryID = '$CategoryName' ";
mysql_query($query2) or die(mysql_error());
$array = array();
while($row = mysql_fetch_assoc($query2)){
$array[] = $row;}
foreach($array as $val)
{
if($val =='the checkbox value')
<form>
<input type="checkbox" name="vehicle" value="Bike" /> I have a bike<br />
<input type="checkbox" name="vehicle" value="Car" /> I have a car
</form>
These are the few steps you have to do.
1.Fetch the values from database.
2. Covert the values in array as they are stored as string by (,) separated.
3. Then
foreach($values as $val)
{
if($val =='Bike')
{
$bikeflag = '1';
}
if($val =='Car')
{
$carflag = '1';
}
}
<form>
<input type="checkbox" name="vehicle" value="Bike" <?php if(isset($bikeflag ) =='1'){ ?>checked = checked <?php } ?>/> I have a bike<br />
<input type="checkbox" name="vehicle" value="Car" <?php if(isset($carflag) =='1'){ ?>checked = checked <?php } ?>/> I have a car
</form>
Hopefully this will help you.
Get values from database with the appropriate SQL query
Loop through results echoing out a checkbox element
Caveats
Make sure each one has a unique name unless you want them to be an array. Then use array syntax for their name (e.g. name[])
Make sure to give each one a unique value
Yes, its very much possible:
Fetch the records to be populated with the structure:
Id and value
Provide datasource of the checkedbox list as the fetched structured list as mentioned above.
Now define the value field of the checkboxlist as "Id" and text as "value"
The key code is:
while ($row= mysql_fetch_array($result, MYSQL_ASSOC))
{ $id=$row[id];
$html=<<<html
<tr><td>
<input style="float:left" type="checkbox" name="mycheckbox[]" value="$id">
<span style="float:left">$row[content]</span>
<span style="color:black;float:right">$row[submitter]</span></td></tr>
html;
echo $html;
}
There are many checkboxes.
How to receive these checkbox values in another PHP file?
You know, you need to name these checkboxes so a PHP file can receive these values on the other side. But how to name these checkboxes? If I name 1,2, 3,...,how can I associate them with $row[id]?
You need to give them names - you can either do it like this:
<input style="float:left" type="checkbox" id="$id" name="$id" value="true">
or like this to get an array:
<input style="float:left" type="checkbox" id="$id" name="myBoxes[$id]" value="true">
You can then check isset($_POST[$id]) or isset($_POST['myBoxes'][$id])
Name your checkboxes so you can easily identify them.
eg
$CheckBoxHTML = "<input type='checkbox' name='check_$row[id]' value='YES'>Check This";
then in your recieving php file you can find all checkboxes by
foreach ($_POST as $key => $value)
{
if (strpos($key,'check_') !== false)
{
list($tmp, $ID) = split('_', $key);
$CheckedValues[] = $ID;
}
}
That will pull out all your checked ids.