Ferdinand Posted May 1, 2013 Share Posted May 1, 2013 How do I post the following through a form? $answers = $_POST['selected_answers$questionNr']; Every time I post the browser displays the following: see the following error Notice: Undefined index: selected_answers{$questionNr} This is the code I am using to $_POST the form output: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <head> <style> style1 {font-family: "Baskerville Old Face"} .style2 { font-family: "Baskerville Old Face"; font-size: 12px; font-style: italic; } body { background-color: #99CC66; } #text_disabled{ border:0; font-size:10pt; color:black; } </style> <title>Question and Answer Read From Text File</title> </head> <body> <form action="processor.php" method="POST"> <b><u> QUESTIONS AND ANSWERS QUIZ</u></b> <br /><br /> <?php $openFile = fopen("questionandanswers.txt", "r") or exit ("unable to open the text file"); $string = fread($openFile, filesize("questionandanswers.txt")); //Regex to get from the first number to the string's end, so we ignore the Number Question... bit; preg_match('/\d.*/', $string, $match); //We get all the strings starting with a number until it finds another number (which will be the beginning of another question; preg_match_all('/\d\D*/', $match[0], $results); $qas = array(); // We prepare an array that will store the questions/answers foreach($results[0] as $result) { //Separating the question from the string with all the answers. list($question, $all_answers) = explode(':', $result); //Separating the different answers $answers_array = explode('.', $all_answers); //removes unnecesary spaces in the questions $answers_array_trimmed = array_map('trim', $answers_array); //Storing the question and the array with all the answers into the previously prepared array; $qas[] = array('question' => $question, 'answers' => $answers_array); } //Looping through the array and outputting all the info into a form; echo "<script type=\"text/javascript\" src=\"js/form.js\"></script>"; $questionNr = 0; foreach($qas as $k=>$v) { echo "<input id='question{$questionNr}' style='width:40%' type='text' value='".$v['question']."' name='questions[]' >"; echo '<br /><br />'; //Puts the answers of each question unique and in the form of radios foreach($v['answers'] as $answer) { echo "<input type=\"radio\" name=\"selected_answers{$questionNr}[]\" value='\"' />"; echo "$answer"; echo '<br/><br />'; } $questionNr++; } ?> <input name= "submit" type="submit" value="Submit Answers">  <input type="reset" value ="Clear Answers"> </form> </body> </html> Quote Link to comment Share on other sites More sharing options...
oaass Posted May 1, 2013 Share Posted May 1, 2013 Strings in single quotes will be treated as nothing but pure text. If you need a variable to work in a string you need to either end the single quote or use double quotes Quote Link to comment Share on other sites More sharing options...
Ferdinand Posted May 1, 2013 Author Share Posted May 1, 2013 Thank you for the insight Oaass, and where exactly I'm I suppose to apply the concept in my code? Thank you. Quote Link to comment Share on other sites More sharing options...
oaass Posted May 1, 2013 Share Posted May 1, 2013 $answers = $_POST['selected_answers$questionNr']; you can start with looking at that line of code Quote Link to comment Share on other sites More sharing options...
Ferdinand Posted May 1, 2013 Author Share Posted May 1, 2013 I have used it before and I get this error: SCREAM: Error suppression ignored for ( ! ) Notice: Undefined index: selected_answers$questionNr in D:\wamp\www\frmfile\processor.php on line 34 Quote Link to comment Share on other sites More sharing options...
davidannis Posted May 1, 2013 Share Posted May 1, 2013 (edited) I brlieve that curly braces { } are not valid as part of the name in an html input. ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods ("."). http://www.w3.org/TR/html401/types.html#type-cdata Perhaps that is causing your problem. Edited May 1, 2013 by davidannis Quote Link to comment Share on other sites More sharing options...
oaass Posted May 1, 2013 Share Posted May 1, 2013 did you change the quotes? Quote Link to comment Share on other sites More sharing options...
davidannis Posted May 1, 2013 Share Posted May 1, 2013 $answers = $_POST['selected_answers$questionNr']; you can start with looking at that line of code Did you change it to something like? $k='selected_answers'.$questionNr; $answers = $_POST[$k]; Quote Link to comment Share on other sites More sharing options...
Ferdinand Posted May 1, 2013 Author Share Posted May 1, 2013 This is how I'm employing your method: //Posting of the chosen answers if(isset($_POST['submit'])) { echo '<b><u>THE ARE THE QUESTIONS IN THEIR ORDER</u></b><br />'; $question = $_POST['questions']; foreach($question as $quiz) { echo '<br />'; print_r($quiz); } $k='selected_answers'.$questionNr; $answers = $_POST[$k]; print_r($answers); But its still displaying the following error: ( ! ) SCREAM: Error suppression ignored for ( ! ) Notice: Undefined variable: questionNr in D:\wamp\www\frmfile\processor.php on line 33 And the following array: Array ( [0] => " [1] => " [2] => " [3] => " [4] => " [5] => " [6] => " [7] => " [8] => " ) Quote Link to comment Share on other sites More sharing options...
oaass Posted May 1, 2013 Share Posted May 1, 2013 (edited) It does not "still display"... That is a completely new error message.. You've gone from undefined index to undefined variable. Read the error message again.. It tells you everything you need to know.. You're trying to use a variable that does not exist... Are you actually reading the error messages or are you just copying it over here for us to solve it? Edited May 1, 2013 by oaass Quote Link to comment Share on other sites More sharing options...
davidannis Posted May 1, 2013 Share Posted May 1, 2013 Which line is 34? Did you remove the curly braces from the radio button name? Can you print_r ($_POST) ; at the top so we can see what's there? Thanks, Quote Link to comment Share on other sites More sharing options...
Ferdinand Posted May 1, 2013 Author Share Posted May 1, 2013 This is what is displayed when I print_r($_POST); Array ( [questions] => Array ( [0] => 1) The most important feature of spiral model is [1] => 2) The worst type of coupling is [2] => 3) One of the fault base testing techniques is [3] => 4) A fault simulation testing technique is [4] => 5) RS is also known as specification of [5] => 6) Which of these terms is a level name in the Capability Maturity Model? [6] => 7) FP-based estimation techniques require problem decomposition based on? [7] => What types of models are created during software requirements analysis? [8] => 9) Which of the following is not an attribute of software engineering ) [selected_answers] => Array ( [0] => " [1] => " [2] => " [3] => " [4] => " [5] => " [6] => " [7] => " [8] => " ) [submit] => Submit Answers ) And I am not sure where the variable is suppose to be declared, because in the other page is where it is declared. Quote Link to comment Share on other sites More sharing options...
davidannis Posted May 1, 2013 Share Posted May 1, 2013 What does this line from your original question look like now? echo "<input type=\"radio\" name=\"selected_answers{$questionNr}[]\" value='\"' />"; You need something in the value="" or it just adds a key with no value to the selected_answers array. Quote Link to comment Share on other sites More sharing options...
Jessica Posted May 2, 2013 Share Posted May 2, 2013 My word. The HTML name can be an array. Change them to name="answer[$x]" Etc and see the print_r Quote Link to comment Share on other sites More sharing options...
Ferdinand Posted May 2, 2013 Author Share Posted May 2, 2013 Thank you all for those who assisted me, I managed to figure it out. This is how i corrected it: In my form i changed the following line of code: echo "<input type=\"radio\" name=\"selected_answers[$questionNr]\" value=\"$answer\" />"; And POSTED it this way: $answers = $_POST['selected_answers']; It seemed the variable $questionNr with the curly braces were confusing me. Thanks again to you all. 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.