kwdelre Posted October 18, 2010 Share Posted October 18, 2010 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 https://forums.phpfreaks.com/topic/216136-using-radio-buttons-with-php/ Share on other sites More sharing options...
objnoob Posted October 18, 2010 Share Posted October 18, 2010 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 https://forums.phpfreaks.com/topic/216136-using-radio-buttons-with-php/#findComment-1123269 Share on other sites More sharing options...
kwdelre Posted October 18, 2010 Author Share Posted October 18, 2010 Ohh man that really helped. Glad I asked now. That way is a lot easier! Thanks again. Link to comment https://forums.phpfreaks.com/topic/216136-using-radio-buttons-with-php/#findComment-1123294 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.