Jump to content

PHP Checkbox Value of "On" or "Off"


dlebowski

Recommended Posts

I have been struggling with this for days now and finally decided I needed to ask for help.  I am wanting to submit a value to my database of either "On" or "Off" from the checkbox.  I don't understand why regardless of whether the user has the box checked or not, it always submits "On" in this case.  Can anyone help me with this?  I want the checkbox to submit "Off" if the box is unchecked and "On" if the box is checked.  I would also be able to work with this checkbox if it submitted nothing when it was unchecked or "On" if it was checked.  Any guidance would be appreciated.  Thanks!

 

<html>
    <head><script language="JavaScript" src="test.js"></script></head>
<form>
<tr>
<TD width=68 height="29" align="center"><input type="checkbox" name="ud_test" <? if($test == "on"){echo "CHECKED";}?>></td>         
<td><input value ="DEL" type="Button" onclick="test(ud_test.value)"></td>
</form>
</html>

Link to comment
Share on other sites

lol... i wish!

 

problem with checkboxes, is that if checked, they transmit on(or whatever you put for value=""), if their not checked, they dont transmit anything.

 

so if checked, post=array(checkbox=>"on"), if not checked, post=array()... so you'd need to set a loop, to unset() all the ones, that arnt checked

Link to comment
Share on other sites

taith, thanks for the quick reply.  For some reason, when I click submit with the box unchecked, it still submits "on"!  It sounds like it should be submitting a blank.  Is that correct?  I could deal with that with what I want to do.  I can't even get it to submit a blank when it isn't checked!

Link to comment
Share on other sites

if you die(print_r($_POST)); you shouldnt see it if its not checked... however... if your storing the information via a session/database, since its not transmitting an off value, its simply not changing it... not that its changing it to on...

Link to comment
Share on other sites

only problem is that if your checkboxes are dynamically made... you'd need a loop of sorts...

 

<?php
for($i=0; $i<=$_SESSION[highestval]; $i++){
if($_POST['ud_test'.$i]=='on') $ud_test[$i]='on';
else unset($ud_test[$i]);
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.