dlebowski Posted May 26, 2007 Share Posted May 26, 2007 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 https://forums.phpfreaks.com/topic/53055-php-checkbox-value-of-on-or-off/ Share on other sites More sharing options...
taith Posted May 26, 2007 Share Posted May 26, 2007 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 https://forums.phpfreaks.com/topic/53055-php-checkbox-value-of-on-or-off/#findComment-262096 Share on other sites More sharing options...
dlebowski Posted May 26, 2007 Author Share Posted May 26, 2007 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 https://forums.phpfreaks.com/topic/53055-php-checkbox-value-of-on-or-off/#findComment-262098 Share on other sites More sharing options...
taith Posted May 26, 2007 Share Posted May 26, 2007 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 https://forums.phpfreaks.com/topic/53055-php-checkbox-value-of-on-or-off/#findComment-262100 Share on other sites More sharing options...
dlebowski Posted May 26, 2007 Author Share Posted May 26, 2007 I understand. I am going to have to move in a different direction. Thanks again. Link to comment https://forums.phpfreaks.com/topic/53055-php-checkbox-value-of-on-or-off/#findComment-262101 Share on other sites More sharing options...
Barand Posted May 26, 2007 Share Posted May 26, 2007 try doing this to see if the checkbox was checked or not <?php $ud_test = isset($_POST['ud_test']) ? 'On' : 'Off' ; ?> Link to comment https://forums.phpfreaks.com/topic/53055-php-checkbox-value-of-on-or-off/#findComment-262316 Share on other sites More sharing options...
taith Posted May 29, 2007 Share Posted May 29, 2007 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 https://forums.phpfreaks.com/topic/53055-php-checkbox-value-of-on-or-off/#findComment-263807 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.