user2510039
user2510039

Reputation: 167

keep checkbox checked after submitting form

Currently I am able to echo the rows from table according to the checked checkbox name attribute using isset in php .

However, I would like the checkboxes be checked after submitting the form and also if the user unchecks the checkbox I would like to use the third else statement in the php where it will retrieve all rows from the table. How can I achieve this? is it also possible to hide the unchecked box and only show the checked box when a user checks one box. Thanks in advance.

<div> Size
<form id="form" method="post" action="">
<input type="checkbox" name="small" class="checkbox" /> Small
<input type="checkbox" name="medium"  class="checkbox">  Medium<br>
</form> 
</div>

<script type="text/javascript">  
    $(function(){
     $('.checkbox').on('change',function(){
        $('#form').submit();
        });
    });
</script>

<?php
if (isset($_POST["small"])){
$paginate = new pagination($page, 'SELECT * FROM pants WHERE size='small' ORDER BY id desc', $options);

}  
else if (isset($_POST["medium"])){
  $paginate = new pagination($page, 'SELECT * FROM pants where size='medium' ORDER BY id desc', $options);
 }
else { $paginate = new pagination($page, 'SELECT * FROM pants ORDER BY id desc', $options);

}
 ?>

Upvotes: 3

Views: 7707

Answers (2)

trijin
trijin

Reputation: 483

<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>

Upvotes: 3

lsouza
lsouza

Reputation: 2488

This may do the job:

<input type="checkbox" name="small" class="checkbox" <?php if ($_POST['small']) echo 'checked'; ?> /> Small
<input type="checkbox" name="medium"  class="checkbox" <?php if ($_POST['medium']) echo 'checked'; ?> />  Medium<br>

Upvotes: 0

Related Questions