Multiselect Checkbox Validation in PHP - php

I am having a dynamic checkbox which is working on foreach. I want to add a validation for that. If I have added 'required' in my input type, it is asking to select all the checkboxes, Is there any other option to validate at least one checkbox is checked.
My Form
<?php
foreach($category_list as $list) {
if(!empty($prop_cat_check)){
$checked = null;
if(in_array($list->cat_id,$prop_cat_check)){
$checked = 'checked';
}
}
?>
<input type="checkbox" name="cat_id[]" id="<?php echo $list->cat_id;?>"
<?php echo $checked;?> value="<?php echo $list->cat_id;?>" >
<?php echo $list->cat_title;
}
} ?>

try this code
<?php
$k==0;
foreach($category_list as $list) {
if(!empty($prop_cat_check)){
$checked = null;
if(in_array($list->cat_id,$prop_cat_check)){
$checked = 'checked';
}
}
?>
<input type="checkbox" name="cat_id[]" id="<?php echo $list->cat_id;?>"
<?php echo $checked;?> value="<?php echo $list->cat_id;?>" <?php if($k==0) echo 'required';?>>
<?php echo $list->cat_title;
$k++;
}
} ?>

Related

For loop display radio buttons with first one checked

I have a for loop that displays radio buttons and I want the first one to display as checked. But when I put a if statement inside the for loop for this the page nevers loads. Any ideas?
$mains = array(0=>'Beef Steak', 1=>'Chicken Breast', 2=>'Pork Chops');
$mainscount = count($mains);
<?php for ($mainNO = 0; $mainNO < $mainscount; $mainNO++) { ?>
<label for="mains<?php echo $mainNO ?>" class="radiobutton"><?php echo $mains[$mainNO]; ?></label>
<input type="radio" name="mains" id="mains<?php echo $mainNO; ?>" value="<?php echo $mainNO; ?>"
<?php if($mainNO = 0){ echo 'checked="checked"'; } ?>/>
<?php } ?>
<?php for ($mainNO = 0; $mainNO < $mainscount; $mainNO++) { ?>
<label for="mains<?php echo $mainNO ?>" class="radiobutton"><?php echo $mains[$mainNO]; ?></label>
<input type="radio" name="mains" id="mains<?php echo $mainNO; ?>" value="<?php echo $mainNO; ?>"
<?php if ($mainNO == 0) {
echo ' checked="checked" ';
} ?>/>
<?php } ?>
you use = where you should use ==
u are using assingment operator in comparision statement
<?php for ($mainNO = 0; $mainNO < $mainscount; $mainNO++) { ?>
<label for="mains<?php echo $mainNO ?>" class="radiobutton"><?php echo $mains[$mainNO]; ?></label>
<input type="radio" name="mains" id="mains<?php echo $mainNO; ?>" value="<?php echo $mainNO; ?>"
<?php if ($mainNO == 0) {
echo " checked";
} ?>/>
and in HTML5 you can use checked only
<input type="checkbox" checked>
// Note: You used single = in if condition that is wrong, it will create indefinite loop . Tested code.
$mains = array(0=>'Beef Steak', 1=>'Chicken Breast', 2=>'Pork Chops');
$mainscount = count($mains);
for ($mainNO = 0; $mainNO < $mainscount; $mainNO++) {
// Checked if value is 0
if($mainNO == 0){ $checked = 'checked="checked"'; }else { $checked =''; };
echo "<label for='mains".$mainNO."' class='radiobutton'>".$mains[$mainNO]."</label>";
echo "<input type='radio' name='mains' id='mains".$mainNO."' value='".$mainNO."' $checked />";
}

Adding checked variable from database with fields from database as well

I have a form which has a list of checkboxes to be filled in on insert to database as well as when editing. When editing I am trying to populate the fields with a list of fields from the database which is in the $groups variable and the checked value being checked against the group_id from groups with the group_id from the $user. It is working except it only fills out one checkbox and some users belong to multiple groups. Any ideas, or more efficient ways to do this.
Heres my code so far
<?php foreach($groups as $group) : ?>
<?php foreach ($user['groups'] as $uG) {
if ($uG['group_id'] == $group['id']) {
$checked = "checked";
} else {
$checked = '';
}
}?>
<div class="checkbox">
<label for="group_id-<?php echo $group['id']; ?>">
<input <?php echo $checked; ?> type="checkbox" name="group_id[]" id="group_id-<?php echo $group['id']; ?>" value="<?php echo $group['id']; ?>">
<?php echo $group['name']; ?>
</label>
</div>
<? endforeach; ?>
This problem is that you are searching all the $users['group'] array and not stopping when you find a match, so unless last $uG['group_id'] matches $group['id'] you continue and therefore clear $checked after potentially setting it.
So just add a break when you find a match.
Also, officially the correct way of setting the checked status is checked="checked" although most modern browsers are not that pedantic, its possibly better to stick to the HTML spec
<?php foreach($groups as $group) : ?>
<?php
$checked = '';
foreach ($user['groups'] as $uG) {
if ($uG['group_id'] == $group['id']) {
$checked = 'checked="checked"';
break;
}
}?>
<div class="checkbox">
<label for="group_id-<?php echo $group['id']; ?>">
<input <?php echo $checked; ?> type="checkbox" name="group_id[]" id="group_id-<?php echo $group['id']; ?>" value="<?php echo $group['id']; ?>">
<?php echo $group['name']; ?>
</label>
</div>
<? endforeach; ?>
you could also use a ternary operator and in_array() to do this all in one simple statement
<?php
foreach($groups as $group) :
$checked = in_array( $group['id'], $user['groups'] ) ? 'checked="checked"' : '';
?>
<div class="checkbox">
<label for="group_id-<?php echo $group['id']; ?>">
<input <?php echo $checked; ?> type="checkbox" name="group_id[]" id="group_id-<?php echo $group['id']; ?>" value="<?php echo $group['id']; ?>">
<?php echo $group['name']; ?>
</label>
</div>
<? endforeach; ?>
Define $checked = ''; first then loop and change the value if match found
<?php foreach($groups as $group) : ?>
<?php
$checked = '';
foreach ($user['groups'] as $uG) {
if ($uG['group_id'] == $group['id']) {
$checked = "checked";
}
}?>
<div class="checkbox">
<label for="group_id-<?php echo $group['id']; ?>">
<input <?php echo $checked; ?> type="checkbox" name="group_id[]" id="group_id-<?php echo $group['id']; ?>" value="<?php echo $group['id']; ?>">
<?php echo $group['name']; ?>
</label>
</div>
<? endforeach; ?>

How to show checkboxes as checked when values are set in the database in codeigniter

I use foreach to show data came from database.
This is for Plan_data
<?php foreach ($veddingPlanData as $row) { ?>
<input box with value containing "echo row->value_name" >
<?php } ?>
This is for task data as check box [here I want to show the checkbox list in which the task is given to plan_id they appear as cheeked checkbox and remaining list with not checked status)
<?php foreach ($veddingPlanTaskMappingData as $row) { ?>
<input type="checkbox" name="task_id[]" value="<?php echo $row->task_id ;?>" checked><?php echo $row->task_name?><br>
<?php } ?>
Here I show the whole task list in check box.
<?php foreach ($allVedingTasks as $row) { ?>
<input type="checkbox" name="task_id" value="<?php echo $row->task_id ;?>" ><?php echo $row->task_name?><br>
<?php } ?>
I want to foreach the task_name list with selected some task as there mapped plan_id.
Finally I found It
<?php
$arrSelVedTask = array();
foreach ($veddingPlanTaskMappingData as $row) {
$arrSelVedTask[$row->task_id] = '';
}
?>
<div class="form-group">
<lable for="task_id" class="control-label col-sm-12">Plan Task LIST:</lable>
<div class="col-sm-10">
<div class="checkbox" style="margin-left:40px;">
<?php foreach ($allVedingTasks as $row) {
if(isset($arrSelVedTask[$row->task_id])) {
?><input type="checkbox" name="task_id[]" value="<?php echo $row->task_id ;?>" checked ><?php echo $row->task_name; ?><br><?php
}
else{
?><input type="checkbox" name="task_id[]" value="<?php echo $row->task_id ;?>" ><?php echo $row->task_name; ?><br><?php
}
}
?>
Simply add a check:
<?php
foreach ($veddingPlanTaskMappingData as $row) {
$checked = ($row->task_id == YOUR_CONDITION) ? 'checked="checked"' : '';
?>
<input type="checkbox" name="task_id[]" value="<?php echo $row->task_id ;?>" <?php echo $checked?>><?php echo $row->task_name?><br>
<?php } ?>
Use set_checkbox Of CI
<input type="checkbox" name="task_id" value="<?php echo $row->task_id ;?>" <?php echo set_checkbox('task_id', $row->task_id); ?> ><?php echo $row->task_name?>
at first in your model fetch the value of checkbox which is in your database then call that function in controller and pass the value to view page. then use if statement with every checkbox item to check whether the value is same or not. and write echo "checked";command if the condition match.
First of all we will take all the categories than we are taking an Id and using it for showing the checked list from the database. First we select the table values based on Id than we will fetch the record. We are having the data in the database column using , comma like 1,2,3,5,9. Than we save it in array and at last we just show that array to the page.
$table="select * from categorytable";
$this->result=mysqli_query($this->con,$table);
$this->count=mysqli_num_rows($this->result);
if($this->count < 1)
{
return "<div class='form-group'>".$messages->getResponseMessage("No category is added.", "Warning")."</div>";
}
else
{
$alert='';
$select="select Category from table where Id='$id'";
$result= mysqli_query($this->con, $select);
$amrow= mysqli_fetch_array($result);
$getAminity=$amrow["Category"];
$getArray= explode(",",$getAminity);
while($row= mysqli_fetch_array($this->result))
{
if(in_array($row[Id],$getArray)){
$alert.="<div class=col-md-2><label class='checkbox-inline'><input type=checkbox id='catval[]' checked name='catval[]' value='$row[Id]'/> $row[Name]</label></div>";
}
else
{
$alert.="<div class=col-md-2><label class='checkbox-inline'><input type=checkbox id='catval[]' name='catval[]' value='$row[Id]'/> $row[Name]</label></div>";
}
}

how to add check bo0x in selection box with multiple selection box

I used selection box with multiple selection now the problem is that it is selectable with ctrl+click of mouse. It is work properly but not that much prefrble to me and lookes like simple selection box and user cant get that its multiple selector not single.so thats why i want it with check box so user easly get it is multiple selector.please give apropriate solution thanks in advanced...
<select class="selopt" id="selPreLoc" name="SelPreLoc[]" multiple="multiple" size=5>
<option label="No Preference">No Preference</option>
<?php
//<option value=-1 selected>No Preference</option>
while ($rec = mysql_fetch_array($GetCityRecord)) {
if ($rec['City_Id'] == 30 || $rec['City_Id'] == 34 || $rec['City_Id'] == 35) {
$others[$rec['City_Id']] = $rec['City'];
continue;
}
?>
<option value="<?php $rec['City_Id']; ?>" <?php echo (isset($_POST['SelPreLoc']) && in_array($rec['City_Id'], $_POST['SelPreLoc'])) ? 'selected' : '' ?>>
<?php echo $rec['City']; ?>
</option>
<?php
}
foreach ($others as $ind => $val) {
?>
<option value="<?php echo $ind; ?>" <?php echo (isset($_POST['SelPreLoc']) && in_array($ind, $_POST['SelPreLoc'])) ? 'selected' : '' ?>>
<?php echo $val; ?>
</option>
<?php }
?>
</select>
<label class="formtxt" valign="bottom">Use Ctrl + Click to multi-select.</label></td>
I got many other solution with using div or other.
but i just want it with select option only is it posible if yes how .andi can fetch the result in mysql and i want that result with comma seprator in mysql.
My code is not tested, since i do not have the data, but based on your logic, you can use it like this:
$theOthers = array(30, 34, 35);
while ($rec = mysql_fetch_array($GetCityRecord)) {
if (in_array($rec['City_Id'], $theOthers)) {
$others[$rec['City_Id']] = $rec['City'];
continue;
}
$checked = '';
if (isset($_POST['SelPreLoc']) && in_array($rec['City_Id'], $_POST['SelPreLoc'])) {
$checked = 'checked="checked"';
}
?>
<input type="checkbox" name="city[]" value="<?php $rec['City_Id']; ?>" <?php echo $checked; ?> /> <?php echo $rec['City']; ?> <br />
<?php
}
foreach ($others as $ind => $val) {
$checked = '';
if (isset($_POST['SelPreLoc']) && in_array($ind, $_POST['SelPreLoc'])) {
$checked = 'checked="checked"';
}
?>
<input type="checkbox" name="city[]" value="<?php echo $ind; ?>" <?php echo $checked; ?> /> <?php echo $val; ?> <br />
<?php
}
NOTE: I improved your code a littlebit with the $theOthers array, i think it's more readable.
When form is submitted, let's var_dump($_POST["city"]);

How do I make values stay in chekbox?

How do I make the values stay in checkboxes?
my problem is when I submit the form the values do not stay (in the form).
Below is my code :
Mca<input type="checkbox" name="qual[]" id="Mca" value="Mca"
<?php if($qual == "Mca") { echo ' checked="checked"' ; } ?>>
Mtech<input type="checkbox" name="qual[]" id="Mtech" value="Mtech"
<?php if($qual == "Mtech") { echo "checked"; } ?>>
Btech<input type="checkbox" name="qual[]" id="Btech" value="Btech"
<?php if($qual == "Btech") { echo "checked"; } ?>>
Try this because $qual is an array.
/* Your $qual should be */ <?php $qual = $GET['qual']; ?>
<?php if($qual == "Mca")
must be changed to :
<?php if(in_array("Mca",$qual)
Try using better code for your form:
<?php
$checkboxes = array('Mca', 'Mtech', 'Btech');
foreach($checkboxes as $k => $v){
echo '<input '.($v==$qual[$k]? 'checked="checked" ': '').'type="checkbox" name="qual[]" id="'.$v.'" value="'.$v.'">';
}
?>

Categories