williamZanelli Posted August 29, 2008 Share Posted August 29, 2008 Hi guys, I have the following code, <form action="computer1.php" method="post" name="radio_form"> <br/><li>Question 1 is...</li> <p><input type="radio" name="Q1" value = "1" /> Answer a - one possible answer <br /> <input type="radio" name="Q1" value = "2" /> Answer b - one possible answer <br /> <input type="radio" name="Q1" value = "3" /> Answer c - one possible answer<br /> </p> <br/><li>Question 2 is...</li> <p><input type="radio" name="Q2" value = "1" /> Answer a - one possible answer <br /> <input type="radio" name="Q2" value = "2" /> Answer b - one possible answer <br /> <input type="radio" name="Q2" value = "3" /> Answer c - one possible answer<br /> </p> <br/><li>Question 3 is...</li> <p><input type="radio" name="Q3" value = "1" /> Answer a - one possible answer <br /> <input type="radio" name="Q3" value = "2" /> Answer b - one possible answer <br /> <input type="radio" name="Q3" value = "3" /> Answer c - one possible answer<br /> </p> What I want to do is make sure from each set of radio buttons, one is checked. if It isnt, I want a message to be disspaled beside that set of radio buttons.. - sounds imple enough, but I cant seem to find any code examples which will allow me to do this. I have come across some JavaScript, but are unsure how to incorprate this into my code- as in, if the validatiion test is passed I'll have some pass control to a PHP file.. if I used onClick it will run the JS and then stop? How do I call the PHP file to process the form then [in this case the PHP file is computer1.php] Any code examples/tips will be appreciated. Thnaks in advance William Link to comment https://forums.phpfreaks.com/topic/121919-solved-radio-button-validation/ Share on other sites More sharing options...
DarkWater Posted August 29, 2008 Share Posted August 29, 2008 You can process a form on the same page that it is displayed quite easily. Rename the action parameter to the current page, then just put an if statement on top of the page: <?php if (isset($_POST['submit'])) { //handle form } //display form, handle errors, whatever Link to comment https://forums.phpfreaks.com/topic/121919-solved-radio-button-validation/#findComment-629179 Share on other sites More sharing options...
williamZanelli Posted August 29, 2008 Author Share Posted August 29, 2008 Thanks for the reply DarkWater I'm not sure of your approach, What I thiought is if one of the RadioButtons is not selected I could use some simple JS to alert the user somehow? Do you have a full example of yoru method? Thanks for your advice William Link to comment https://forums.phpfreaks.com/topic/121919-solved-radio-button-validation/#findComment-629188 Share on other sites More sharing options...
Fadion Posted August 29, 2008 Share Posted August 29, 2008 Basically you can have: <?php if(isset($_POST['submit'])){ if($_POST['Q1'] == ''){ $message1 = 'Please select a value in the first group'; } if($_POST['Q2'] == ''){ $message2 = 'Please select a value in the second group'; } } ?> The $message variables (which can easily be an array) get a value when a group hasn't any selected value. Make a simple php echo() aside that group and you're done Link to comment https://forums.phpfreaks.com/topic/121919-solved-radio-button-validation/#findComment-629189 Share on other sites More sharing options...
DarkWater Posted August 29, 2008 Share Posted August 29, 2008 That's really unwieldy though, GuiltyGear. He can probably just make a loop. Link to comment https://forums.phpfreaks.com/topic/121919-solved-radio-button-validation/#findComment-629196 Share on other sites More sharing options...
Fadion Posted August 29, 2008 Share Posted August 29, 2008 That's really unwieldy though, GuiltyGear. He can probably just make a loop. What do you mean? What does "unwieldy" mean by the way? I'm not a native english speaker Link to comment https://forums.phpfreaks.com/topic/121919-solved-radio-button-validation/#findComment-629202 Share on other sites More sharing options...
DarkWater Posted August 29, 2008 Share Posted August 29, 2008 That's really unwieldy though, GuiltyGear. He can probably just make a loop. What do you mean? What does "unwieldy" mean by the way? I'm not a native english speaker Wow, what's your native language? You speak English well enough to have fooled me (except the occasional mistake I've seen here or there). Anyway, unwieldy means that it's not easily put into action or it's just not practical, in terms of coding. Link to comment https://forums.phpfreaks.com/topic/121919-solved-radio-button-validation/#findComment-629209 Share on other sites More sharing options...
williamZanelli Posted August 29, 2008 Author Share Posted August 29, 2008 Hey guys, thanks for the responses, What I've done is... session_start(); $flag = true; if(isset($_POST['submit'])){ if($_POST['Q1'] == ''){ $message1 = 'Please select a value for this question'; $flag == false; } if($_POST['Q2'] == ''){ $message2 = 'Please select a value for this question'; $flag == false; } if($_POST['Q3'] == ''){ $message3 = 'Please select a value for this question'; $flag == false; } if ($flag){ $_SESSION['post_data'] = $_POST; header("Location: http://www.somesite.com/Quiz/computer1.php"); The problem I'm having is it keeps comming back to this page. My form looks like <form action="Quiz.php" method="post" name="radio_form"> <br/><li>Question 1 is...</li> <p><input type="radio" name="Q1" value = "1" /> Answer a - one possible answer <br /> <input type="radio" name="Q1" value = "2" /> Answer b - one possible answer <br /> <input type="radio" name="Q1" value = "3" /> Answer c - one possible answer<br /> ............... I hope this gives you a better understanding of what I'm tryong to accomplish. Thanks William. Link to comment https://forums.phpfreaks.com/topic/121919-solved-radio-button-validation/#findComment-629210 Share on other sites More sharing options...
williamZanelli Posted August 29, 2008 Author Share Posted August 29, 2008 To clarify, the above code is all on one PHP page, called Quiz.php Thanks for your advice. William Link to comment https://forums.phpfreaks.com/topic/121919-solved-radio-button-validation/#findComment-629212 Share on other sites More sharing options...
MatthewJ Posted August 29, 2008 Share Posted August 29, 2008 Your flag variable would be set with $flag = false; not $flag == false; Link to comment https://forums.phpfreaks.com/topic/121919-solved-radio-button-validation/#findComment-629216 Share on other sites More sharing options...
williamZanelli Posted August 29, 2008 Author Share Posted August 29, 2008 Ahh , still same problem, even after I change the "==" to "=" My full code is below, I can't seem to see the problem, if one radio button/group is checked, should I be redirected to http://www.somesite.com/Quiz/computer1.php??? Thanks in advance guys. <?php session_start(); $flag = true; if(isset($_POST['submit'])){ if($_POST['Q1'] == ''){ $message1 = 'Please select a value for this question'; $flag = false; } if($_POST['Q2'] == ''){ $message2 = 'Please select a value for this question'; $flag = false; } if($_POST['Q3'] == ''){ $message3 = 'Please select a value for this question'; $flag = false; } if($_POST['Q4'] == ''){ $message4 = 'Please select a value for this question'; $flag = false; } if($_POST['Q5'] == ''){ $message5 = 'Please select a value for this question'; $flag = false; } if($_POST['Q6'] == ''){ $message6 = 'Please select a value for this question'; $flag = false; } if($_POST['Q7'] == ''){ $message7 = 'Please select a value for this question'; $flag = false; } if($_POST['Q8'] == ''){ $message8 = 'Please select a value for this question'; $flag = false; } if($_POST['Q9'] == ''){ $message9 = 'Please select a value for this question'; $flag = false; } if($_POST['Q10'] == ''){ $message10 = 'Please select a value for this question'; $flag = false; } if($_POST['Q11'] == ''){ $message11 = 'Please select a value for this question'; $flag = false; } if($_POST['Q12'] == ''){ $message12 = 'Please select a value for this question'; $flag = false; } if ($flag){ //redirect to $_SESSION['post_data'] = $_POST; header("Location: http://www.somesite.com/Quiz/computer1.php"); } } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>| Quiz</title> </head> <body> <p> </p> <p>Questions, questions, questions.... </p> <ol> <form action="Quiz.php" method="post" name="radio_form"> <br/><li>Question 1 is...</li> <p><input type="radio" name="Q1" value = "1" /> Answer a - one possible answer <br /> <input type="radio" name="Q1" value = "2" /> Answer b - one possible answer <br /> <input type="radio" name="Q1" value = "3" /> Answer c - one possible answer<br /> </p> <br/><li>Question 2 is...</li> <p><input type="radio" name="Q2" value = "1" /> Answer a - one possible answer <br /> <input type="radio" name="Q2" value = "2" /> Answer b - one possible answer <br /> <input type="radio" name="Q2" value = "3" /> Answer c - one possible answer<br /> </p> <br/><li>Question 3 is...</li> <p><input type="radio" name="Q3" value = "1" /> Answer a - one possible answer <br /> <input type="radio" name="Q3" value = "2" /> Answer b - one possible answer <br /> <input type="radio" name="Q3" value = "3" /> Answer c - one possible answer<br /> </p> <br/><li>Question 4 is...</li> <p><input type="radio" name="Q4" value = "1" /> Answer a - one possible answer <br /> <input type="radio" name="Q4" value = "2" /> Answer b - one possible answer <br /> <input type="radio" name="Q4" value = "3" /> Answer c - one possible answer<br /> </p> <br/><li>Question 5 is...</li> <p><input type="radio" name="Q5" value = "1" /> Answer a - one possible answer <br /> <input type="radio" name="Q5" value = "2" /> Answer b - one possible answer <br /> <input type="radio" name="Q5" value = "3" /> Answer c - one possible answer<br /> </p> <br/><li>Question 6 is...</li> <p><input type="radio" name="Q6" value = "1" /> Answer a - one possible answer <br /> <input type="radio" name="Q6" value = "2" /> Answer b - one possible answer <br /> <input type="radio" name="Q6" value = "3" /> Answer c - one possible answer<br /> </p> <br/><li>Question 7 is...</li> <p><input type="radio" name="Q7" value = "1" /> Answer a - one possible answer <br /> <input type="radio" name="Q7" value = "2" /> Answer b - one possible answer <br /> <input type="radio" name="Q7" value = "3" /> Answer c - one possible answer<br /> </p> <br/><li>Question 8 is...</li> <p><input type="radio" name="Q8" value = "1" /> Answer a - one possible answer <br /> <input type="radio" name="Q8" value = "2" /> Answer b - one possible answer <br /> <input type="radio" name="Q8" value = "3" /> Answer c - one possible answer<br /> </p> <br/><li>Question 9 is...</li> <p><input type="radio" name="Q9" value = "1" /> Answer a - one possible answer <br /> <input type="radio" name="Q9" value = "2" /> Answer b - one possible answer <br /> <input type="radio" name="Q9" value = "3" /> Answer c - one possible answer<br /> </p> <br/><li>Question 10 is...</li> <p><input type="radio" name="Q10" value = "1" /> Answer a - one possible answer <br /> <input type="radio" name="Q10" value = "2" /> Answer b - one possible answer <br /> <input type="radio" name="Q10" value = "3" /> Answer c - one possible answer<br /> </p> <br/><li>Question 11 is...</li> <p><input type="radio" name="Q11" value = "1" /> Answer a - one possible answer <br /> <input type="radio" name="Q11" value = "2" /> Answer b - one possible answer <br /> <input type="radio" name="Q11" value = "3" /> Answer c - one possible answer<br /> </p> <br/><li>Question 12 is...</li> <p><input type="radio" name="Q12" value = "1" /> Answer a - one possible answer <br /> <input type="radio" name="Q12" value = "2" /> Answer b - one possible answer <br /> <input type="radio" name="Q12" value = "3" /> Answer c - one possible answer<br /> </p> <br/> <input type="submit" value="Submit.."/> </form> </ol> </body> </html> Link to comment https://forums.phpfreaks.com/topic/121919-solved-radio-button-validation/#findComment-629235 Share on other sites More sharing options...
DarkWater Posted August 29, 2008 Share Posted August 29, 2008 First of all, change your submit button to: <input type="submit" name="submit" value="Submit.." /> Secondly, take a careful look at the logic for the form validation and think about why only 1 checked wouldn't redirect you. Link to comment https://forums.phpfreaks.com/topic/121919-solved-radio-button-validation/#findComment-629238 Share on other sites More sharing options...
Fadion Posted August 29, 2008 Share Posted August 29, 2008 It would be better if you would use this way: <input type="radio" name="Q1[]" value = "1" /> Answer a - one possible answer <br /> <input type="radio" name="Q1[]" value = "2" /> Answer b - one possible answer <br /> <input type="radio" name="Q1[]" value = "3" /> Answer c - one possible answer<br /> note the brackets in the name. They will make the radiogroup an array, which can be used in the script like this: <?php $errors = array(); $q1 = $_POST['Q1']; if(count($q1) == 0){ $errors['Q1'] = 'Please select a value.'; } ?> The same involves the other 3 radiogroups and you will definitely shorten some code. Link to comment https://forums.phpfreaks.com/topic/121919-solved-radio-button-validation/#findComment-629242 Share on other sites More sharing options...
williamZanelli Posted August 29, 2008 Author Share Posted August 29, 2008 Hey guys, Thanks for the replies. Ok the prob i'm having now is.. if say you miss answering one question of the quiz [one radio button group] - you get redirectd to the Quiz.php which loses the previous responses, so you have to re-answer all the qesutions. DarkWater - thanks for the help so far, however I'm not sure what you mean by "Secondly, take a careful look at the logic for the form validation and think about why only 1 checked wouldn't redirect you." - I want all the questions to be answered.. so I think correct in what in what I've done? GuiltyGear - I like your method. I'll give it a go, I see it will make the code look much neater, however I still have to the "if" statments for the teh 12 questions.. so the code would be about the same in size? Something like this? $q1 = $_POST['Q1']; if(count($q1) == 0){ $errors['Q1'] = 'Please select a value.'; $q2 = $_POST['Q2']; if(count($q2) == 0){ $errors['Q2'] = 'Please select a value.'; Thanks in advnace, William Link to comment https://forums.phpfreaks.com/topic/121919-solved-radio-button-validation/#findComment-629316 Share on other sites More sharing options...
Fadion Posted August 29, 2008 Share Posted August 29, 2008 In any method you'll have to do some if()s. Maybe a switch() can be a bit more tidy. As for the real problem, your form's action is set to "Quiz.php", which will be the script that validates the input. You could use sessions to store answers, errors and such, but the best/easiest way should be having the validation code in the same script and the form's action to the same page. Basically: <?php if(isset($_POST['submit'])){ //see if the form is submitted //validate the input } ?> //your html with the inputs Link to comment https://forums.phpfreaks.com/topic/121919-solved-radio-button-validation/#findComment-629318 Share on other sites More sharing options...
williamZanelli Posted August 30, 2008 Author Share Posted August 30, 2008 GuilyGear, Couldn't this validation be done much more cleanly and easily is JS? Rather than storing values in sessions etc. which seems a little but long winded? William Link to comment https://forums.phpfreaks.com/topic/121919-solved-radio-button-validation/#findComment-629337 Share on other sites More sharing options...
Fadion Posted August 30, 2008 Share Posted August 30, 2008 Only JavaScript? Sure it can look better/faster that way, but you'll always need server side validation if the client-side one failed. One can disable JavaScript and do with your form whatever he/she likes. Use JS for primary validation, but have the php backend in place if anyone wants to bypass the client-side. EDIT: Oh and you won't need to use sessions if you validate the form in the same script. Link to comment https://forums.phpfreaks.com/topic/121919-solved-radio-button-validation/#findComment-629340 Share on other sites More sharing options...
williamZanelli Posted August 30, 2008 Author Share Posted August 30, 2008 Hi there, Coluld some one please dwell more on the "EDIT" posted by GuiltyGear, - how could I avoid using sessions? How could I store the answers, such that, if the validation failed, I could still have the checked radio buttons displayed as checked? As it stands, when if the validation fails, I loose the answers from the radio buttons the user may have already checked. Thanks in advance for your thoughs William. Link to comment https://forums.phpfreaks.com/topic/121919-solved-radio-button-validation/#findComment-629650 Share on other sites More sharing options...
Fadion Posted August 30, 2008 Share Posted August 30, 2008 Mate, I told you in a previous post, you can have the validate script and form inputs in the same page. In this code you posted, the form and validation script are on the same page, but your form's action is set to "Quiz.php". You must make it point to the same page, or just leave it empty. Link to comment https://forums.phpfreaks.com/topic/121919-solved-radio-button-validation/#findComment-629710 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.