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! Quote Link to comment 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"); Quote Link to comment 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? Quote Link to comment 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. Quote Link to comment 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! Quote Link to comment 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 Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment 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! Quote Link to comment 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"; Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.