Echo checked if checkbox is checked - php

if this is my checkbox
<input type="checkbox" name="Filter[]" value="Steak" id="Filter"/>
and if checkbox is checked var_export returns me
["Filter"]=> array(1) { [0]=> string(7) "Steak"
how do I echo "checked=checked" if checkbox is checked?

What you need is in_array(), this will check whether that value exists in the array, if your array contains the value, the function will return true and you can simply echo out the checked attribute
if (in_array('YOUR_VALUE_HERE', $arr)) {
echo 'checked="checked"';
}
You can also make a function passing value and array as parameter and returning the value from the function.

$checked = in_array('Steak',$_POST['Filter']) ? ' checked="checked"' : '';
echo '<input type="checkbox" name="Filter[]" value="Steak" id="Filter"'.$checked.'/>';

<?php // Check if the box was sent.
$checked = "";
$status = (isset($_REQUEST['status']));
if ($status == 'checked' )
{
$status = 1;
$checked = 'checked="checked"';
}
else
{
$status = 0;
}
echo $status;
echo <<<END
<form action="" method="post">
<input type="checkbox" name="status" $checked /> Testbox<br />
<input type="submit" onclick="return showDiv();"/>
</form>
END;
?>

I think this might solve your problem
$checked = in_array('Steak',$_POST['Filter']) ? ' checked="checked"' : '';
echo '';

Related

Retain Checked the checkbox inside While

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>

Check if checkbox is checked

I have user titles, which are stored in database in this way. 1,2,6,10 and etc.
I want to check if the user already has this titles and if he does, to check the check box.
<?php
$user_titles = explode(',', $user['titles']);
//foreach($user_titles as $uTitles){
//echo $uTitles;
//}
?>
<input type="checkbox" name="title[]" value="1">Test1<br/>
<input type="checkbox" name="title[]" value="2">Test2<br/>
<input type="checkbox" name="title[]" value="3">Test3<br/>
You need to add the checked attribute to the checked ones.
<input type="checkbox" name="title[]" value="1" <?php if(in_array(1, $user_titles) echo 'checked="checked"'; ?>>Test1<br/>
Move the titles to new array and iterate trough them. Inside the loop, check if the value is in the $user_titles array and add 'checked' to the input tag.
<?php
$titles = [
1 => "Test1",
2 => "Test2",
3 => "Test3",
];
foreach ($titles as $value => $title) {
$checked = in_array($value, $user_titles) ? 'checked' : '';
echo '<input type="checkbox" name="title[]" value="' . $value . '" ' . $checked . '>' . $title . '<br/>';
}
?>
If i understand correctly you have the values stored as a string delimited by a comma.
Use an iteration method, for example:
<?php $checked = ''; ?>
<?php for($i = 1; $i <= count($possible_checkbox); $i++ ) {
if(in_array($i, $user_titles ) ) { $checked = 'checked'; } else {$checked = '' }
?>
<input type="checkbox" value="<?php echo $i; ?>" name="title[]" <?php echo $checked; ?> />
<?php } ?>
Hope it helps.

Checking radio button on submit - PHP

I need to check a radio button input on submit.
If none of the radio buttons are checked, $err1_diet gets set to true and the red class needs to be added.
And I also need to know which radio button was checked since this isn't the only question in the form.
if( !isset($_POST['diet']) ){
$err1_diet = true;
}elseif($_POST['diet'] == 1){
$diet = true;
}else{
$diet = false;
$yes = true;
}
<p class="<?php echo (($err1_diet == true) ? "red" : "" ); ?>">• Are you on a diet?<?php var_dump($err1_diet); ?></p>
<input type="radio" name="diet" value="1" <?php echo (($diet) ? 'checked="true"' : "" ); ?> /> Yes
<input type="radio" name="diet" value="0" <?php echo (($diet) ? '' : 'checked="true"' ); ?> /> No
Actually no ... it's checking one of the radio buttons before the form is submitted. That's the problem
You can solve your problem using $_SERVER['REQUEST_METHOD']=='POST' for example:
if( $_SERVER['REQUEST_METHOD']=='POST' and !isset($_POST['diet']) ){
$err1_diet = true;
}elseif($_POST['diet'] == 1){
$diet = true;
}else{
$diet = false;
$yes = true;
}
<p class="<?php echo (($err1_diet == true) ? "red" : "" ); ?>">• Are you on a diet?<?php var_dump($err1_diet); ?></p>
<input type="radio" name="diet" value="1" <?php echo (($diet) ? 'checked="true"' : "" ); ?> /> Yes
<input type="radio" name="diet" value="0" <?php echo (($diet) ? '' : 'checked="true"' ); ?> /> No

PHP get checkbox value in checked/unchecked [duplicate]

This question already has answers here:
Get checked and unchecked checkboxes value
(3 answers)
Closed 8 years ago.
<?php
// $groups is fetch_array from mysql
foreach($groups as $group) {
if ($group['delete_user'] === 'Y') {
$checked = "checked=\"checked\";
}
else {
$checked = '';
}
?>
<input type = "checkbox" name="delete_user[<?php echo $group['id']; ?>]" <?php echo $checked; ?>>
<?php
}
?>
Will output:
<form action="test.php" method="post">
<input type="checkbox" name="delete_user[1]">
<input type="checkbox" name="delete_user[2]">
<input type="checkbox" name="delete_user[3]">
<input type="checkbox" name="delete_user[4]" checked="checked">
<input type="submit" name="save_action" value="Save">
</form>
And when I check inputs as wanted, then this will process input.
<?php
if(isset($_POST['save_action']) {
if (empty($_POST['delete_user'])) {
$_POST['delete_user'] = array();
}
foreach($_POST['delete_user'] as $del) {
is_checked($del); //#todo
}
}
?>
I am looking for way to check if check-box is checked and return proper value ( Y or N ). In this point I declared is_checked() function for this purpose.
I would do it like this:
<form action="test.php" method="post">
<input type="checkbox" name="delete_user[]" value="1">
<input type="checkbox" name="delete_user[]" value="2">
<input type="checkbox" name="delete_user[]" value="3">
<input type="checkbox" name="delete_user[]" value="4" checked="checked">
<input type="submit" name="save_action" value="Save">
</form>
Now in PHP you can loop over the $_POST['delete_user'] and it will contain the values of the selected items.
foreach($_POST['delete_user'] as $item)
{
// code to delete $item
}
As the comments already mentioned, only the checked checkboxes will be set
So you either have to take the index as ID or add a val to each input element.
The latter would probably be cleaner (in my opinion)
<?php
// $groups is fetch_array from mysql
foreach($groups as $group) {
if ($group['delete_user'] === 'Y') {
$checked = "checked=\"checked\";
}
else {
$checked = '';
}
?>
<input type = "checkbox" name="delete_user[]" <?php echo $checked; ?> value="<?php echo $group['id'];?>">
<?php
}
?>
And the code which does something with the inputs:
<?php
if(isset($_POST['save_action']) {
if (empty($_POST['delete_user'])) {
$_POST['delete_user'] = array();
}
foreach($_POST['delete_user'] as $del) {
// do something with $del - the id
}
}
?>
Use the checkbox name just as an array and then give the id as value. See the below code.
<?php
// $groups is fetch_array from mysql
foreach($groups as $group) {
if ($group['delete_user'] === 'Y') {
$checked = "checked=\"checked\";
}
else {
$checked = '';
}
?>
<input type = "checkbox" name="delete_user[]" value="<?php echo $group['id']; ?>" <?php echo $checked; ?>>
<?php
}
?>
Then on the action page place this code
<?php
foreach($_POST['delete_user'] as $del) {
is_checked($del); //#todo
}
}
?>
This will work fine.

How to make checkboxes dynamic (checked option)

I have a code:
while ($row = mysql_fetch_assoc($query_model)) {
echo ' <label class="checkbox">
<input name="model[]" value="'.$row["Model"].'" type="checkbox" checked>'.$row["Model"].'
</label>
';
}
I want to make checked option dynamic.
At first I will have all checkboxes checked, but when I will uncheck some checkboxes and click submit, I want to see, after page reloads, whole list with checked and unchecked fields (which I have unchecked formerly). Is it possible to put this option to while statement?
Normally I would do something like that:
if ($model == 'My model') {
echo ' checked';
} else {
echo '';
}
but in this case I don't know how to implement it.
Put the value of all the checked checkboxes in a array, let it be $available.
Now loop over the query result some what like this -
while ($row = mysql_fetch_assoc($query_model)) {
$checked = '';
if(in_array($row['Model'], $available){
$checked = 'checked';
}
echo '<label class="checkbox">
<input name="model[]" value="'.$row["Model"].'" type="checkbox" '.$checked.'>'.$row["Model"].'
</label>';
}
Yes you can do that like as follows :
while ($row = mysql_fetch_assoc($query_model)) {
if($model == 'My model') { // your condition
$checked=' checked';
} else {
$checked='';
}
echo '<label class="checkbox">';
echo '
<input name="model[]" value="'.$row["Model"].'" type="checkbox"'. $checked .'>'.$row["Model"];
echo '</label>';
}
You can try like,
if(isset($_POST['model']))
{
while ($row = mysql_fetch_assoc($query_model))
{
if(in_array($_POST,$row['Model'])
$checked="checked='checked'";
else
$checked="";
echo '<label class="checkbox"><input name="model[]" value="'.$row["Model"].'" type="checkbox" '.$checked.'>'.$row["Model"].'</label>';
}
}
else{
while ($row = mysql_fetch_assoc($query_model))
{
echo '<label class="checkbox"><input name="model[]" value="'.$row["Model"].'" type="checkbox" checked>'.$row["Model"].'</label>';
}
}

Categories