PHP Keep Checkbox Checked After Submitting Form

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



Leave a reply



Submit