Jump to content

Using Radio Buttons With PHP


kwdelre

Recommended Posts

Hello guys. I am learning PHP little by little and am writing a VERY basic quiz. For functionality in the future I want to be able to change questions and answers in one "included" php doc. The problem i am having with my current code is that I would like to place a variable in the "value" definition on the radio input. I am unable to do this I guess. Any suggestions to accomplish the same thing? Sorry for my horrible code, this is the first thing I have written from scratch.

 

The form page:

 

<?php
include ("test.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=UTF-8" />
<title>Untitled Document</title>
</head>

<body>


<h2><? print $q1; ?></h2>

<form method="post" action="test.php">
<input type="radio" name="q1answered" value="Basketball"/>

<?php
print $q1ans[0];
?>

<input type="radio" name="q1answered" value="Football"/>

<?php
print $q1ans[1];

?>

<br /><br />

<input type="submit" value="Submit Your Answer!" name="submit" />
</form>
</body>
</html>

 

 

The 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=UTF-8" />
<title>Untitled Document</title>
</head>

<body>

<?php

//Enter all questions below

$q1 = "What is your favorite Sport?";
$q2 = "What is your favorite hobby?";

//End Questions

//Enter Asnwer choices for each question

$q1ans = array("Basketball", "Football");

//End Answers

//All question defined variables here to make grading easier

$qbank = array
($q1, $q2,);



if (isset($_POST["submit"])) {
$choice = $_POST["q1answered"];



if ($choice == $q1ans[0]){


print "Your Favorite Sport is $q1ans[0].";
}

else {
	print "Your Favorite Sport is $q1ans[1].";
}


}



?>



</body>
</html>

 

 

Thanks!

Link to comment
Share on other sites

Sure you can, however you cannot if you hard code the whole input tag. Instead dymaically create them!

 

echo $q1 . '<br/>'
foreach( $q1ans as $answer) {
echo "<input type='radio' name='q1answered' value='$answer'/>$answer";
}

 

This would create radio buttons for each member of your array $q1ans

 

I hope this helps!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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