I have a checkbox that is always checked no matter if I checked the box or not.
Here is how the checkbox is currently setup:
<input class="classname" id="check" value="1" <?php echo (($temp['test']) ? 'checked' : ''); ?> name="check" type="checkbox" />
Here is how the check box value is being captured:
$check = $_POST['check'] ?? 0;
I also tried the following code as well:
$check = (isset($_POST['check']) == '1' ? '1' : '0');
I tried the hidden field approach as well and it didn't work. Any ideas on why the checkbox is showing marked even though I removed the check.
I think Drussy might be onto something, where does $_POST['test'] come from?
Try this code:
$checked = isset($_POST['check']) ? 'checked' : '';
<input class="classname" id="check" value="1" <?php echo $checked; ?> name="check" type="checkbox" />
Related
i cant seem to make the checkbox appear as checked if this statement turns out to be true. the array passed is working fine and everything is in palce but i dont seem to find a way where i can make the checkbox checked if the value of BSS is there
<?php if(isset($_GET['cng']) && array_search("BSS", $scharr))
{
echo "checked=''";
};
?>/>
BSS
</label>
try below code:
<label for="one">
<input type="checkbox" name="school[]" value="BSS" <?php echo isset( $_GET['cng'] ) && in_array("BSS", $scharr) ? 'checked' : ''; }; ?> />
BSS </label>
<input type="checkbox" <?php if(isset($_GET['cng']) && isset($scharr['BSS'])) echo "checked"?> />
Try this code
<input type="checkbox" <?php if(isset($_GET['cng']) && array_search("BSS", $scharr)) { echo "checked";}>
how can I auto check the checkbox based on the value from the database? I have the following code:
<input type="checkbox" name="CarModel" value="Yes" ($row[CarModel]==Audi? 'checked' : '') >
it should be like this
<input type="checkbox" name="CarModel" value="Yes" <?php echo ($row['CarModel'] == 'Audi' ) ? 'checked' : NULL ; ?> >
I have a form with several check boxes populated from a db
CODE:
// populating Checkboxes from db
echo '<div>
<label for="'.$n['eName'].'">'.$n['eName'].'</label>
<input type="checkbox" name="skills[]" id="'.$n['eName'].'" value="'.$n['id'].'" '.(isset($_POST[$n['eName']]) ? 'checked="checked"' : '') .' />
</div>';
The problem is when the user select some of these check-boxes and submit the form and get error, the form can not remember his choices and he has to re-select them again.
what can I do to go over this issue?
Thanks in advance
This is not correct:
isset($_POST[$n['eName']]
You should look in the $_POST['skills'] array.
According to your implementation of $n['eName'] the following code might work:
echo '<div>
<label for="'.$n['eName'].'">'.$n['eName'].'</label>
<input type="checkbox" name="skills['.$n['eName'].']" id="'.$n['eName'].'" value="'.$n['id'].'" '.(isset($_POST['skills'][$n['eName']]) ? 'checked="checked"' : '') .' />
</div>';
ps: Be warned that any quotation in $n['eName'] would most likely break your code - one should take measures for these cases.
Edit:
<form method=post>
<?php
$n['id']='someid';
$n['eName'] = 'test';
echo '<div>
<label for="'.$n['eName'].'">'.$n['eName'].'</label>
<input type="checkbox" name="skills['.$n['eName'].']" id="'.$n['eName'].'" value="'.$n['id'].'" '.(isset($_POST['skills'][$n['eName']]) ? 'checked="checked"' : '') .' />
</div>';
?>
<input type=submit >
</form>
i have a form in three steps, from step 1 to step two there is a checkbox, i would like to keep the checkbox value in a session and also to show if it's checked or unchecked even if the user goes to step 3 of the form then comes back to step two,
right now on form submit i have all the $_POST vars in $_SESSION vars but i cant make it work with checkboxes, this is what i got right now:
<input type="checkbox" value="<?php echo isset($_POST['afficher_ddn'])? "1":"0"; ?>"
style="margin-left: 20px;" name="afficher_ddn" id="afficher_ddn"
<?php echo $_SESSION['afficher_ddn']=="1" ? "checked" : ""; ?> />
but this doesnt work.
<input type="checkbox" value="1" style="margin-left: 20px;" name="afficher_ddn" id="afficher_ddn"
<?php echo isset($_SESSION['afficher_ddn']) && $_SESSION['afficher_ddn'] == "1" ? 'checked="checked"' : ''; ?> />
For one, you don't change the value of the checkbox. If it is not checked it won't pass to the post vars. See here: Does <input type="checkbox" /> only post data if it's checked?
Now, the current standard for checked is checked="checked" I have no idea where or why this standard became one at the firm I work for, but it's what we do so I relay that here.
If the code provided here does not work for you, I would var_dump($_SESSION) to make sure it's set as well as check the actual HTML in something like firebug (firefox) or developer tools in chrome. Sometimes goofiness happens and checked="checked" is actually set in the html but doesn't display in browser. In those times I usually yank out some hair and clear caches. then it usually clears up.
Your code is fine.Possibly there is some problem in passing the the value in session as i am checking the code manually it is working perfectly:
use print_r($_SESSION) for testing and post the output;If possible provide all the forms:
Try Below Code:
<input type="checkbox" value="1"
style="margin-left: 20px;" name="afficher_ddn" id="ddn"
<?php echo $_SESSION['afficher_ddn']=="1" ? "checked" : ""; ?> />
try this example separetly
Make a page test.php as:
<?php
session_start();
?>
<form methos='post' method='post' action='test1.php'>
<input type="checkbox" value="1"
style="margin-left: 20px;" name="afficher_ddn" id="ddn"
<?php echo $_SESSION['afficher_ddn']=="1" ? "checked" : ""; ?> /><br>
Name:<input type='text' name='f1' value='<?php echo $_SESSION['f1'] ?>' ><br>
Select:<select name='s1'>
<option value="" <?php echo $_SESSION['s1']=="" ? "selected" : ""; ?>></option>
<option value="1" <?php echo $_SESSION['s1']=="1" ? "selected" : ""; ?>>1</option>
<option value="2" <?php echo $_SESSION['s1']=="1" ? "selected" : ""; ?>>2</option>
</select><br>
Test:
<textarea name='tex1'><?php echo $_SESSION['tex1']; ?></textarea>
<input type='submit' value='submit'>
</form>
Make a second page test1.php as:
<?php
session_start();
print_r($_POST);
echo $_SESSION['afficher_ddn']=$_POST['afficher_ddn'];
echo $_SESSION['f1']=$_POST['f1'];
echo $_SESSION['s1']=$_POST['s1'];
echo $_SESSION['tex1']=$_POST['tex1'];
?>
<form methos='post' action='third.php'>
<a href='test.php'>Step1</a>
</form>
and Check
HI
i'm using a php page and i need to keep the value of and check box and radio button (checked or not checked) after post page.
how could i make it?
thanks
First get the radio button value.
$radiobuttonvalue = $_POST['radiobuttoname']
Then for each radio button with the same name, do this
<input type="radio" name="radiobuttonname" value="value" id="radiobuttonname" <?php if($radiobuttonvalue == "value") { echo 'checked="checked"';} ?>
You need something like:-
<?php
$postCheckboxName = '';
if (isset($_POST['checkbox_name']) || 'any_value' == $_POST['checkbox_name']) {
$postCheckboxName = ' checked="checked"';
}
?>
<input type="checkbox" name="checkbox_name" value="any_value"<?php echo $postCheckboxName;?> />
<?php
$postRadioName = '';
if (isset($_POST['radio_name']) || 'any_other_value' == $_POST['radio_name']) {
$postRadioName = ' checked="checked"';
}
?>
<input type="checkbox" name="radio_name" value="any_other_value"<?php echo $postRadioName;?> />
This code should get you going. I'm basically checking whether the POST value of either the checkbox / radio element is set or not & whether the corresponding element's value matches with my respective element's value or not.
Hope it helps.
Something like this:
<?php if (isset($_POST['checkbox_name']))?>
<input type="checkbox" checked="checked" value="<?php echo $_POST['checkbox_name'];?>" />
<?php} ?>
<?php if (isset($_POST['radio_name']))?>
<input type="radio" checked="checked" value="<?php echo $_POST['radio_name'];?>" />
<?php} ?>
What happens is that you check if the input variables are in the $_POST and if so you add checked="checked" to the input fields to make them checked.
This worked for me, and is self explanatory
sample code usage:
<div class="form-group">
<label class="radio-inline">
<input type="radio" name="time" value="lunch" <?php if (isset($_POST[ 'time']) && $_POST[ 'time']=='lunch' ){echo ' checked="checked"';}?>>Lunch</label>
<label class="radio-inline">
<input type="radio" name="time" value="dinner" <?php if (isset($_POST[ 'time']) && $_POST[ 'time']=='dinner' ){echo ' checked="checked"';}?>>Dinner</label>
</div>