How to keep checkbox array checked after form submission - php

I have the following codes to show dynamic checkboxes.
while($result = mysqli_fetch_array($query)){
$oaName = $result['oaName'];
echo '<input class="checkBoxes" type="checkbox" name="checkBoxArray[]" value="'.$oaName.'" style="float:left;"'; ?> <?php if(isset($_POST['checkBoxArray'])) echo "checked='checked'"; ?> <?php echo '>'; ?>
}
I want to retain status of only those checkboxes that I checked as checked after form submission. But with the above codes, all checkboxes are showing checked after form submission. Does anyone know what am i doing wrong here?
Edit 1
checkBoxArray[] are checkboxes names which are getting from database

After a few trials, I am able to get it done by the following way. The key here is the use of in_array
while($result = mysqli_fetch_array($query)){
$oaName = $result['oaName'];
echo '<div class = "checkbox-group" required ><input class="checkBoxes" type="checkbox" name="checkBoxArray[]" value="'.$oaName.'" style="float:left;"'; ?> <?php if(in_array($oaName,$_POST['checkBoxArray'])) echo "checked='checked'"; ?> <?php echo '></div>
}

Related

set_checkbox for multiple select checkbox

how to set checkbox value if form validation fails in codeigniter. I have multiple select checkbox whose values populated from other dropdown selection. I want if form validation fails it should give me selected values. In my case it disappers. Any help will be appreciated.
my code:
<tbody class="location_id">
<?
$checked='';
if(isset($data1['loc_id']))
{
if($data1['loc_id']!='')
{
$checked='checked';
}
}
if(isset($data1['loc_id'])){
?>
<tr><td><input type="checkbox" name="loc_id[]" value="<?php if(isset($data1['loc_id'])){ echo $data1['loc_id'];} ?>" <?php echo set_checkbox('loc_id[]', $data1['loc_id']); ?> <?php echo $checked;?>></td><td><?php if(isset($data1['loc_id'])){echo get_name('location_tbl','loc_id',$data1['loc_id'],'loc_name');}?></td></tr>
<?php
}
?>
</tbody>
hope you understand.
In my controller I have set
$this->form_validation->set_rules('loc_id', '', 'trim|xss_clean');
sorry for grammar.
<input type="checkbox" style= "position: initial;" name="hobby[]" value="Sport"<?php if(strpos($hobby, 'Sport') !== false) echo "checked='checked'"; ?> >Sport
model:-
$hobby =implode(",", $this->input->post('hobby'));

How to persist check box as checked using php

I have a program where I am populating the check box, once I click on the submit button, I am inserting the data into DB and I want to retain the checkbox state as checked for the values that I have inserted, so that when a user logs in again he can see the items he has checked the previous time. This is my code
<?php
$accQry = "select * from brands";
$result1 = mysql_query($accQry, $db1->conn);
$i = 1;
while ($row = mysql_fetch_assoc($result1)) {
?>
<label> <input type="checkbox" value="<?php echo $row['brand_id']; ?>" name="chk1[]" ><?php echo $row['brand_name']; ?></label>
<?php $i++;
}
?>
This is the code to populate and I don't know how to go about it further.
Checkbox has attribute checked, which should be set when you want to check it - how checkbox works

Input checkbox when i edit user no change

When i want to edit my user on inactive is it no working, but when i editing my users to inactive to active, it is working.
This is my PHP code: https://pastebin.com/iBaDxH2u
<input class="form-control" type="checkbox" name="actif" id="actif" value="<?php echo $userinfo['actif']; ?>" <?php if ($userinfo['actif'] == "1") { echo "checked"; } ?>>
<input type="hidden" name="actif" value="1" />
I dont solve the problem...
Thx
This sounds like it has something to so with how checkboxes work. When you submit the form, if the checkbox is ticked then the page you submit the form to will receive [actif] => on. If you submit with the checkbox unticked then the page will not receive [actif] => off, it will receive an empty array []. actif will not be set. Something like this might make it more obvious what is going on.
<?php
if (isset($_GET['actif']) && $_GET['actif']=="on")
{
echo ("The box was ticked");
$ticked = 'checked';
}
else
{
echo ("The box was not ticked");
$ticked = '';
}
echo ("<pre>");
print_r($_GET);
?>
<form>
Actif <input type='checkbox' name='actif' id='actif' <?=$ticked?>>
<button type='submit'>Click me</button>
</form>

keeping multiple checkboxes checked in foreach loop in php

I have multiple checkboxes within a foreach loop and I need to keep the selected checkboxes checked after the form submission. The code is as below. Please help.
<? $i=0;
while ($row=mysql_fetch_array($result,MYSQL_ASSOC))
{
foreach($row=$val)
{
$id="chkbox".$i;
?>
<input type="checkbox" name="chkbx" onclick ="func()" id="<?echo $id;">? value="<?echo $val \n;?>" <? echo "$val";?>
Now where and how to include the checked property of the boxes..
You don't need foreach loop here
This can be done for checking multiple checkbox checked
<?php
$i=0;
while ($row=mysql_fetch_array($result,MYSQL_ASSOC))
{
$checked = "";
if($row['database_column_name']=$val){
$checked = "checked";
}
echo '
<input type="checkbox" name="chkbx" onclick ="func()" id="'.$id.'" value="'.$val.'" '.$checked.'>'.
$val
.'
';
}
?>
Works for me.

Retrieve POST checkbox data from form if they exist

Hello knowledgeable people. I am having trouble retrieving checkbox data from form. I have a site in which user can add checkboxes themselves, so I am writing them out like this:
<table style="padding:10px;">
<?php
$query_boolean = $DB->prepare("SELECT * FROM moduls WHERE type='boolean'") or die(mysql_error());
$query_boolean->execute();
while (($row = $query_boolean->fetch()) != false)
{
?>
<tr>
<td>
<?php echo $row->name ?>:
</td>
<td>
<?php
$s = "";
$s .= sprintf('<input type="checkbox" class="textbox" name="boolean_%s" value="yes">%s', $row->id, Yes);
$s .= sprintf('<input type="checkbox" class="textbox" name="boolean_%s" value="no">%s', $row->id, No);
echo $s;
?>
</td>
</tr>
<?php
}
?>
</table>
Now I have an advanced search in which I have to chech through every checkbox to see what has been selected (ether none, Yes, No, or both). How can I get the info from every checkbox in variables? Thank you so much!
To get POST data from checkboxes they must have attribute
checked="checked"
EDIT:
If you have 2 checkbox as this..
<input type="checkbox" checked="checked" class="textbox" name="boolean_yes" value="yes">
<input type="checkbox" class="textbox" name="boolean_no" value="no">
When you submit your form the checkbox with attribute checked will be sent as POST and the one without checked attribute will not be sent..
if(isset($_POST['search'])) {
$all_checked = array();
foreach($_POST as $key=>$value){
if(strpos($key, "boolean_") > -1){
$all_checked[$key] = $value;
}
}
var_dump($all_checked);
}
This way you will get inside $all_checked array all marked boxes.. All others checboxes are not marked!
if you want to get checkbox value then use checkbox name as array
<input type="checkbox" name="email1[]" value="">
an get it on another page by
<?php
$var2 = $_POST['email1'];
$v=implode(",",$var2);
echo $v;
?>
try it

Categories