Thauwa Posted April 2, 2009 Share Posted April 2, 2009 Status - I have an HTML form, which has checkboxes named c1, c2, c3 & c4. When checked, the value (thing I want to insert to mysql) must be 'yes', and the remaining must be valued 'no'. ??? Situation- I can't find how to do it. :-\ Help! Does anyone know how to achieve this? Thanks in advance! Link to comment https://forums.phpfreaks.com/topic/152186-iffing-checkbox-values/ Share on other sites More sharing options...
Showcase Posted April 2, 2009 Share Posted April 2, 2009 When a checkbox is unchecked, nothing is sent via POST. When a checkbox is checked, it's html value feild is sent. For setting a variable to yes or no if the checkbox is checked or unchecked, you can check it using the isset function. $c1 = (isset($_POST['c1'] ? "yes" : "no"); $c2 = (isset($_POST['c2'] ? "yes" : "no"); $c3 = (isset($_POST['c3'] ? "yes" : "no"); $c4 = (isset($_POST['c4'] ? "yes" : "no"); Link to comment https://forums.phpfreaks.com/topic/152186-iffing-checkbox-values/#findComment-799225 Share on other sites More sharing options...
Thauwa Posted April 2, 2009 Author Share Posted April 2, 2009 thanks, i'll try it. then, there must be no value to the box in the html? Link to comment https://forums.phpfreaks.com/topic/152186-iffing-checkbox-values/#findComment-799226 Share on other sites More sharing options...
Showcase Posted April 2, 2009 Share Posted April 2, 2009 Well, in your situation, the value of the checkbox is not being used. So it won't effect the results at all. Link to comment https://forums.phpfreaks.com/topic/152186-iffing-checkbox-values/#findComment-799228 Share on other sites More sharing options...
Thauwa Posted April 2, 2009 Author Share Posted April 2, 2009 Thank you! I hope that it works! Link to comment https://forums.phpfreaks.com/topic/152186-iffing-checkbox-values/#findComment-799229 Share on other sites More sharing options...
Thauwa Posted April 3, 2009 Author Share Posted April 3, 2009 Cannot I use 'y' instead of 'yes' and 'n' instead of 'no'. An error occurs when it is, saying that a ') or a ',' must be there Link to comment https://forums.phpfreaks.com/topic/152186-iffing-checkbox-values/#findComment-800234 Share on other sites More sharing options...
Yesideez Posted April 3, 2009 Share Posted April 3, 2009 $c1=($_POST['c1'] ? "y" : "n"); $c2=($_POST['c2'] ? "y" : "n"); $c3=($_POST['c3'] ? "y" : "n"); $c4=($_POST['c4'] ? "y" : "n"); Try that. Link to comment https://forums.phpfreaks.com/topic/152186-iffing-checkbox-values/#findComment-800237 Share on other sites More sharing options...
Yesideez Posted April 3, 2009 Share Posted April 3, 2009 What the above is equal to: if ($_POST['c1']) { $c1="y"; } else { $c1="n"; } You can see that having $_POST['c1'] in the if() on its own just checks if the checkbox was set - isn't checking against a specific value. Link to comment https://forums.phpfreaks.com/topic/152186-iffing-checkbox-values/#findComment-800239 Share on other sites More sharing options...
Thauwa Posted April 4, 2009 Author Share Posted April 4, 2009 I think that the new code is correct, because, when the previous code is put, Parse error: syntax error, unexpected '?', expecting ',' or ')' in D:\Hosting\3577251\html\quiz\submit.php on line 6 occurs. Thanks, I'll try the new one! Link to comment https://forums.phpfreaks.com/topic/152186-iffing-checkbox-values/#findComment-800875 Share on other sites More sharing options...
unrelenting Posted April 4, 2009 Share Posted April 4, 2009 Might try: $c1=($_POST['c1']) ? "y" : "n"; $c2=($_POST['c2']) ? "y" : "n"; $c3=($_POST['c3']) ? "y" : "n"; $c4=($_POST['c4']) ? "y" : "n"; Link to comment https://forums.phpfreaks.com/topic/152186-iffing-checkbox-values/#findComment-800922 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.