This is my form :

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

I want to send the value of "dept" as soon as the checkbox is checked and want the page to be refreshed with the checkbox checked and the value for that dept passed to the page. I dont want any SUBMIT button, so i m using the this.form.submit(), but it keeps all the checkboxes checked.

Please help.

Thank you.

Member Avatar for diafol

Show the whole form. The php/form doesn't make much sense to me since both checkboxes will be checked regardless of which one is checked on submission. Why do they have the same names? Are you building a control array? If so, use name="dept[]". Would radiobuttons be more appropriate, where you can only have the one checked item. In which case, the name="dept" would be fine.

If using radiobuttons, I think the following would work...

<?php if((isset($_GET['dept']) && $_GET['dept']=="sales") || !isset($_GET['dept']) echo "checked='checked'"; ?>
<?php if(isset($_GET['dept']) && $_GET['dept']=="it") echo "checked='checked'"; ?>

Which would give your 'sales' the default checked value on page load.

<form>
  <ul>
    <li><a href="?cat=1"> cat1 </a></li>
    <li><a href="?cat=2"> cat2 </a></li>
    <li><a href="?cat=3"> cat3 </a></li>
    <li><a href="?cat=4"> cat4 </a></li>
 </ul> 
      <br><br>
 <input type="checkbox" name="dept1" value="sales" <?php if(isset($_GET['dept1'])) echo "checked ='checked'";?> onclick="this.form.submit()";> Sales <BR />
 <input type="checkbox" name="dept2" value="it" <?php if(isset($_GET['dept2'])) echo "checked ='checked'";?> onclick="this.form.submit()";> IT
</form>

This is my form. I sorted the issue with checkbox but unable to keep the value from <li>.

So if cat3 is clicked from the <li> and checkbox for Sales is selected, i want to display cat=3 and dept=1 in the url so i can populate the results accordingly. [mysite.php?cat=3&dept1=sales].

Am i doing it right

change your links into radiobutons

<style>
input[type=radio] {  
        display: none;  
    }  
</style>
    <form>
    <ul>
    <li><label><input type="radio" name="cat" value="1"  onchange="this.form.submit()"
        <?php if(isset($_GET['cat'])&& $_GET['cat']==1) echo "checked ='checked'";?> >
        ca1</label></li>
    <li><label><input type="radio"  name="cat" value="2"  onchange="this.form.submit()" 
        <?php if(isset($_GET['cat'])&& $_GET['cat']==2) echo "checked ='checked'";?> >
        ca2</label></li>
    <li><label><input type="radio"  name="cat" value="3" onchange="this.form.submit()"
        <?php if(isset($_GET['cat'])&& $_GET['cat']==3) echo "checked ='checked'";?> >
        ca3</label></li>
    <li><label><input type="radio" name="cat" value="4"  onchange="this.form.submit()" 
        <?php if(isset($_GET['cat'])&& $_GET['cat']==4) echo "checked ='checked'";?> >
        ca4</label></li>
    </ul>
    <br><br>
    <input type="checkbox" name="dept1" value="sales" <?php if(isset($_GET['dept1'])) echo "checked ='checked'";?> onclick="this.form.submit()";> Sales <BR />
    <input type="checkbox" name="dept2" value="it" <?php if(isset($_GET['dept2'])) echo "checked ='checked'";?> onclick="this.form.submit()";> IT
    </form>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.