javascript - onclick set php session to keep checkbox checked. On uncheck end session - Stack Overflow

This is my form : <input type="checkbox" name="dept" value="sales" <

This is my form :

<input type="checkbox" name="dept" value="sales" <?php if(isset($_POST['sales'])) echo "checked='checked'"; ?> onclick="this.form.submit();" /><br />

When i click the checkbox, the page is refreshed with the ?dept=sales in the URL. Just as i want it to be. But the checkbox is unchecked. I want the checkbox to be checked. And when the checkbox is unchecked i want the ?dept=sales to be removed from teh URL.

Thanks.

This is my form :

<input type="checkbox" name="dept" value="sales" <?php if(isset($_POST['sales'])) echo "checked='checked'"; ?> onclick="this.form.submit();" /><br />

When i click the checkbox, the page is refreshed with the ?dept=sales in the URL. Just as i want it to be. But the checkbox is unchecked. I want the checkbox to be checked. And when the checkbox is unchecked i want the ?dept=sales to be removed from teh URL.

Thanks.

Share Improve this question asked Sep 5, 2013 at 10:36 user2742581user2742581 611 silver badge6 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 5

Your checkbox's name is dept, not sales. The value of your checkbox is sales. This means that if you want to access the value of your checkbox, you will need to access it via $_POST['dept'], not $_POST['sales']. If your form method isn't declared as method="post", use $_GET['dept'] instead of $_POST['dept'].

At first check your check box name, It's dept but you fetch from sales $_POST, another hint is that if your request is shown on the url then it's get not post, If you want to remove parameter from your url add method="post" to your form, At last your code should be like this:

<form action="your action here" method="post">
<input type="checkbox" name="sales" value="sales" <?php if(isset($_POST['sales'])) echo "checked='checked'"; ?> onclick="this.form.submit();" /><br />
</form>

You're checking for an input with the name "sales" change $_POST['sales'] to $_POST['dept'] that'll work :)

Submit the form only when the checked property true like,

<input type="checkbox" name="dept" value="sales" 
     <?php if($_REQUEST['dept']=='sales')) echo "checked='checked'"; ?> 
     onclick="if(this.checked==true) this.form.submit();" /><br />

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

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信