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.'">';
}
?>
Related
I have code:
<?php
if ($user['x'] == 1) { $x_checked = ' checked'; } else { $x_checked = ''; }
if ($user['y'] == 1) { $y_checked = ' checked'; } else { $y_checked = ''; }
if ($user['a'] == 1) { $a_checked = ' checked'; } else { $a_checked = ''; }
if ($user['b'] == 1) { $b_checked = ' checked'; } else { $b_checked = ''; }
if ($user['c'] == 1) { $c_checked = ' checked'; } else { $c_checked = ''; }
[...]
?>
<input name="a" type="checkbox"<?php echo $a_checked; ?> />
<input name="b" type="checkbox"<?php echo $b_checked; ?> />
<input name="c" type="checkbox"<?php echo $c_checked; ?> />
[...]
and i have too long code (others same lines). How shortcode to this?
Just check in the input HTML:
<input name="a" type="checkbox" <?php echo ($user['a'] == 1) ? 'checked' : '' ?> />
<input name="b" type="checkbox" <?php echo ($user['b'] == 1) ? 'checked' : '' ?> />
<input name="c" type="checkbox" <?php echo ($user['c'] == 1) ? 'checked' : '' ?> />
If the values can only be 0 or 1 (or maybe more than 1 if you want that checked) then it is shorter:
<?php echo $user['c'] ? 'checked' : '' ?>
If you're going to have a $user element for each checkbox then loop it:
<?php foreach($user as $key => $val) { ?>
<input name="<?php echo $key ?>" type="checkbox" <?php echo $val ? 'checked' : '' ?> />
<?php } ?>
From your comment it appears you may be echoing, if so then just:
foreach($user as $key => $val) {
$checked = $val ? 'checked' : '';
echo '<input name="'.$key.'" type="checkbox" '.$checked.'/>';
}
Welcome to Stackoverflow!
Foreach loops and arrays are in this case your best friends, this is how I usually do it.
<?php
$input_name = array('a', 'b', 'c', 'd');
input_data = '';
foreach ($input_name as $value) {
if ($user[$value] == 1) {
$input_data .= '<input name="'.$value.'" type="checkbox" checked>';
} else {
$input_data .= '<input name="'.$value.'" type="checkbox">';
}
}
?>
Echo the results in the HTML part:
<?=$input_data?>
<?php
$fields = [
'a',
'b',
'etc'
];
foreach ($fields as $field){
if($user[$field] == 1){
$checked = 'checked';
}else{
$checked = '';
}
print('<input name="'.$field.'" type="checkbox" '.$checked.' />');
}
?>
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++;
}
} ?>
this code is inside while after I submit the form instead of retaining what I checked it checked all after submitting.
I just want to happen after submitting the only check box i check is checked.
What should i do ?
<input type="checkbox" title ="<?php echo $sym ?>"<?php if(isset($_POST['poscon'])) echo "checked='checked'"; ?> name="poscon[]" value ="<?php echo $pc?>"><?php echo $pc?>
refer to in_array
<?php
if(isset($_GET["poscon"])) {
$_SESSION["poscon"] = $_GET["poscon"];
$dr=$_SESSION['poscon'];
if(isset($_POST['submit'])) {
if(!empty($_GET['poscon']))
$_SESSION['poscon'] = $_POST['poscon'];
$part=$_GET["poscon"];
}
$poscon=mysqli_real_escape_string($link,$_GET['poscon']);
$p = mysqli_query($link,"select distinct PossibleCondition,Symptoms from healthguide where Subpart like '%$poscon%' and PossibleCondition REGEXP '^[N-Z].*$' Order by PossibleCondition ");
while($r=mysqli_fetch_array($p)) {
$pc=$r["PossibleCondition"];
$sym=$r["Symptoms"];
if(isset($_POST) && isset($_POST['poscon']) && in_array($pc,$_POST['poscon']))
$strIsChecked='checked="checked"';
else
$strIsChecked=null;
echo '<tr>';
echo '<td><input type="checkbox" '.$strIsChecked.' title ="'.$sym.'" name="poscon[]" value ="'.$pc.'"></td>';
echo '<td>'.$pc.'</td>';
echo '</tr>';
}
}
?>
$_POST['poscon'] is a array. Run my script and see how it works.
<?php
/**
* File test.php
*/
// Store checked Values in Array $arrChecked
$arrChecked=array();
if(isset($_POST) && $_POST['poscon']) {
// Debug: Show all POST Vars
echo "<pre>"; print_r($_POST); echo "</pre>";
foreach($_POST['poscon'] AS $checkboxValue) {
// fill array with checked values
// e.g. arrChecked['foo'] = true;
$arrChecked[$checkboxValue] = true;
}
}
/**
* Note for HTML-Block:
* shorthand php if/else used
* http://stackoverflow.com/questions/4567292/overview-of-php-shorthand
*/
?>
<form action="test.php" method="post">
<input type="checkbox" name="poscon[]" value="foo" <?php echo (isset($arrChecked) && $arrChecked['foo'] ? 'checked="checked"' : '');?>> foo
<input type="checkbox" name="poscon[]" value="bar" <?php echo (isset($arrChecked) && $arrChecked['bar'] ? 'checked="checked"' : '');?>> bar
<input type="submit" value="go">
</form>
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"]);
I'm trying to check a check box, if the value for that field is 1 in the database.
I have:
<?php
$selectedSPK=$_POST['SPKSelect'];
$assigned = $_POST['Sales_Exec'];
$date = $_POST['DateSelect'];
if ($selectedSPK) {
$Priorityquery = "SELECT Priority FROM Data WHERE SPKCustNo = '$selectedSPK' ";
$Priorityresult = mysql_query($Priorityquery);
$row = mysql_fetch_array($Priorityresult);
$checked = $Priorityresult['Priority'];
}
?>
<input name="PriorityCheckBox" type="checkbox" value="1"
<?php if ($checked == 1) echo ' checked'; ?> />
but not getting any joy, any ideas?
Try this:
You were not using the row returned by the query...
<?php
$selectedSPK=$_POST['SPKSelect'];
$assigned = $_POST['Sales_Exec'];
$date = $_POST['DateSelect'];
if ($selectedSPK)
{
$Priorityquery = "SELECT Priority FROM Data WHERE SPKCustNo = '$selectedSPK' ";
$Priorityresult = mysql_query($Priorityquery);
$row = mysql_fetch_array($Priorityresult);
//$checked = $Priorityresult['Priority']; // <------ this is where you went wrong...
$checked = $row['Priority']; // <------ this will fix where u went wrong!
}
?>
<input name="PriorityCheckBox" type="checkbox" value="1" <?php if ($checked == 1){echo ' checked'; }?>
You should use
<?php if ($checked == 1){echo "checked='checked'"; }
and also
$checked = $Priorityresult['Priority'];
to
$checked = $row['Priority'];
I think you have one mistake... Try this
<input name="PriorityCheckBox" type="checkbox" value="1" <?php if ($row['Priority'] == 1) echo ' checked'; ?> />
Try it this way
<input name="PriorityCheckBox" type="checkbox" value="1" <?php if ($checked == 1) echo "checked='checked'"; ?> />
change
<?php if ($checked == 1) echo ' checked'; ?>
to
<?php if ($checked == 1) echo ' checked="checked"'; ?>
and $checked = $Priorityresult['Priority']; to $checked = $row['Priority'];
It should be checked="checked"
<?php if ($checked == 1) echo "checked='checked'"; ?>