PHP keep checkbox checked after submitting form
change
<input type="checkbox" name="txtCheck" value="<?php echo $_POST['txtCheck'];?>" /><br />
to
<input type="checkbox" name="txtCheck" value="your value" <?php if(isset($_POST['txtCheck'])) echo "checked='checked'"; ?> /><br />
this will keep checkbox checked..
How to keep checkboxes checked after form submit?
Figured it out. Changed the input boxes to:
<input type="checkbox" name="checkArr[]" value="$dynamic_val" <?php (in_array($dynamic_val, $checkArr) ? ' checked' : '');?>>
keep checkbox checked after submitting form
<input type="checkbox" name="small" class="checkbox" <?=(isset($_POST['small'])?' checked':'')?> /> Small
<input type="checkbox" name="medium" class="checkbox" <?=(isset($_POST['medium'])?' checked':'')?> > Medium<br>
Keep checkbox checked after form submit
Rather than hard-coding your "checked" attribute into each of your <input type="checkbox">
fields, you should be using PHP to determine if the GET
variable associated with each field was passed to the script. In that case, you want to append the checked
attribute to your <input>
field. You can accomplish this by using a ternary operator right inside the field, as such:
<input type='checkbox' name='cities[]' value='CHI' <?= (in_array('CHI', $places)) ? 'checked' : ''; ?> >CHICAGO
<input type='checkbox' name='cities[]' value='DET' <?= (in_array('DET', $places)) ? 'checked' : ''; ?> >DETROIT
<input type='checkbox' name='cities[]' value='LA' <?= (in_array('LA', $places)) ? 'checked' : ''; ?> >LAS ANGELES
<input type='checkbox' name='cities[]' value='NYC' <?= (in_array('NYC', $places)) ? 'checked' : ''; ?> >NEW YORK
<input type='checkbox' name='cities[]' value='DALLAS' <?= (in_array('DALLAS', $places)) ? 'checked' : ''; ?> >DALLAS
<input type='checkbox' name='cities[]' value='SPR' <?= (in_array('SPR', $places)) ? 'checked' : ''; ?> >SPRINGFIELD
<input type='checkbox' name='cities[]' value='PHI' <?= (in_array('PHI', $places)) ? 'checked' : ''; ?> >PHILIDELPHIA
How can I keep the checkbox checked after I submit a form using PHP?
<input type='checkbox' name='$post_Value' value='post_valuebValue' checked='checked'>$post_valuebValue</input>
I would do something like this please forgive any syntax errors in doing this on my phone
<input type='checkbox' name='$post_Value' value='post_valuebValue' <?pho if ($postvalue = checked){echo 'checked='checked'}?>'>$post_valuebValue</input>";
Echo checked html into field to keep it checked
Checkbox Php : Keep checked only the checked checkbox after submitting Get form
The problem is because of this line,
<?php if (isset($_GET['category'])) echo "checked='checked'"; ?>> ...
^^^^^^^^^^^^^^^^^^^^^^^^^^
Upon form submission $_GET['category']
will be set, hence this condition isset($_GET['category'])
will hold true for all the checkboxes. And that's why all the checkboxes are checked irrespective of which one you checked earlier. So your foreach
loop should be like this:
<?php foreach ($categories as $cat) { ?>
<li>
<input id="category" name="category[]" type="checkbox" value="<?= $cat->term_id; ?>"
<?php if (isset($_GET['category']) && in_array($cat->term_id, $_GET['category'])) { echo "checked='checked'"; } ?>><?= $cat->name ?></input>
</li>
<?php } ?>
Related Topics
Best Methods to Clean Up a Hacked Site with No Clean Version Available
Alter Table in Magento Setup Script Without Using SQL
How to Execute Stored Procedure from Laravel
PHP Conditionals, Brackets Needed
Is My Understanding of PHP Sessions Correct
PHP Pdo::Bindparam() Data Types.. How Does It Work
How to Access PHP Session Variables from Jquery Function in a .Js File
How to Parse a Date String in PHP
How to Connect to MySQL Database in PHP Using MySQLi Extension
How to "Validate" Human Names in Cakephp
PHP File Upload, How to Restrict File Upload Type
Calculate Skip Value for Given Record for Sorted Paging
Sqlstate[42S22]: Column Not Found: 1054 Unknown Column - Laravel
A Conditional Element Inside an Array(...) Construct
Get Size of Post-Request in PHP