php - keep checkbox checked after submitting form - Stack Overflow

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

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

}
 ?>

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

}
 ?>
Share asked Aug 8, 2013 at 22:00 user2510039user2510039 1673 gold badges6 silver badges13 bronze badges 5
  • Simply check if neither of them are set? else if (!isset($_POST['small']) && !isset($_POST['medium']) – user1508519 Commented Aug 8, 2013 at 22:03
  • what about keeping it checked then? – user2510039 Commented Aug 8, 2013 at 22:05
  • So you want the other one to go away? Why would you do that? Either way, it's trivial to do in jquery. – user1508519 Commented Aug 8, 2013 at 22:07
  • You can save the POST values from your form in your database and then pull those values when the form is rendered again. The HTML checked attribute (w3schools./tags/att_input_checked.asp) will be helpful here. This will allow long term saving of form values if that's something you're interested in. – lostphilosopher Commented Aug 8, 2013 at 22:08
  • In a nutshell: if... condition is met <input type="checkbox" name="medium" class="checkbox" checked> just a scenario. Not an SQL guy, but you get the general idea ;-) – Funk Forty Niner Commented Aug 8, 2013 at 22:12
Add a ment  | 

2 Answers 2

Reset to default 3
<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>

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>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745098731a4611142.html

相关推荐

  • php - keep checkbox checked after submitting form - Stack Overflow

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

    12小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信