最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

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 />
发布评论

评论列表(0)

  1. 暂无评论