Jump to content

radio button help


ianco

Recommended Posts

Hi all,

 

I'm haveing trouble with a quiz form i have. The radio buttons aren't selective and somehow i always get the first answer in the list.

 

Can anyone see where i'm going wrong please?

Thanks

 

Ian

 

<FORM NAME ="form1" METHOD ="POST" ACTION ="testy4.php">

<b>Q1.</b>
Je m'appelle?  <br>

<Input type = 'radio' Name ='q1' value ="a" > Ian<br>
<Input type = 'radio' Name ='q1' value ="aa" > Sally<br>

<Input type = 'radio' Name ='q1' value ="aaa" > Keef the fish<br>

<b>Q2.</b>
Voulez vous couche avec moi? <br>

<Input type = 'radio' Name ='q2' value ="b" > Oui<br>
<Input type = 'radio' Name ='q2' value ="bb" > Non<br>
<Input type = 'radio' Name ='q2' value ="bbb" > Je don't know<br>


<INPUT TYPE = "Submit" Name = "Submit1" VALUE = "Submit"><br>

</FORM>

 

<?php



if (isset($_POST['Submit1'])) {
    $cost = array();

/* Q1 */

echo "<b>Q1.<br>

Je m'appelle?

</b><br><br>";


    if (isset($_POST['q1'])) {
        ($_POST['q1'] = "a") ? $cost['q1'] = 'a' : $cost['q1'] = FALSE; echo "Well done, Je m'appelle Ian<br>";
    }
    elseif (isset($_POST['q1'])) {
        ($_POST['q1'] = "aa") ? $cost['q1'] = 'aa' : $cost['q1'] = FALSE; echo "Sally?!?!? You idiot, Je m'appelle Ian<br>";
    }

    elseif (isset($_POST['q1'])) {
        ($_POST['q1'] = "aaa") ? $cost['q1'] = 'aaa' : $cost['q1'] = FALSE; echo "You idiot Keith is a fish! Je m'appelle Ian<br>";
    }
}

if (isset($_POST['Submit1'])) {
    $cost = array();

/* Q2 */

echo "<br><b>Q2.<br>

Voulez vous couche avec moi?

</b><br><br>";

    if (isset($_POST['q2'])) {
    ($_POST['q2'] = 'b') ? $cost['q2'] = 'b' : $cost['q2'] = FALSE; echo "You chose yes. You lucky girl<br>";
    }

    elseif (isset($_POST['q2'])) {
    ($_POST['q2'] = 'bb') ? $cost['q2'] = 'bb' : $cost['q2'] = FALSE; echo "You picked no. Oh dear<br>";
    }

    elseif (isset($_POST['q2'])) {
    ($_POST['q2'] = 'bbb') ? $cost['q2'] = 'bbb' : $cost['q2'] = FALSE; echo "What do you mean you don't know<br>";
    }


}
echo '<br><b>Thanks for doing the quiz</b>';


?>

Link to comment
https://forums.phpfreaks.com/topic/190654-radio-button-help/
Share on other sites

Okay, for starters. You're HTML is extremely poor, this is how an input should look:

 

<input type="radio" name="q1" value="a" />

 

Now, on to your PHP. To check which answer you selected you need to do something like this:

 

//First you need to check if they selected an answer for Question 1.
if(isset($_POST['q1'])){
//So, they selected an answer. Not to check the value to see what they choose.
if($_POST['q1'] == "a"){
	//They choose A.
	echo "Well done, Je m'appelle Ian<br>";
}elseif($_POST['q1'] == "aa"){
	//The choose AA.
	echo "Sally?!?!? You idiot, Je m'appelle Ian<br>";
}elseif($_POST['q1'] == "aaa"){
	//They choose AAA.
	echo "You idiot Keith is a fish! Je m'appelle Ian<br>";
   }
}else{
//No answer was selected for this question, show an error.
echo "You did not select an answer for Question 1!";
}

 

You need to make sure that you properly check your users submitted form data for errors, as people will sometimes try anything and sometimes that can result in errors. So ensure you have proper validation. I've commented the example code above so that it may help you further.

 

Regards,

Project Fear

Link to comment
https://forums.phpfreaks.com/topic/190654-radio-button-help/#findComment-1005895
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.