0

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.

3
Contributors
3
Replies
19
Views
4 Years
Discussion Span
Last Post by pzuurveen
0

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.

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

0

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>
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.