0

How do I post the following through a form?

$answers = $_POST['selected_answers$questionNr'];

Every time I post the browser displays the following:

I 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">&nbsp <input type="reset" value ="Clear Answers">
</form>
    </body>
</html>

Where exactly Am I going wrong because It seems not to be going through.

Edited by ferdinandmucos

2
Contributors
3
Replies
33
Views
4 Years
Discussion Span
Last Post by ferdinandmucos
0

The way you have it now:
$answers = $_POST['selected_answers0'] answer to question 0
$answers = $_POST['selected_answers1'] answer to question 1
$answers = $_POST['selected_answers2'] answer to question 2

if you change line 65 to

    echo "<input type=\"radio\" name=\"selected_answers[{$questionNr}]\" value='\"' />";

the result will be in $answers = $_POST['selected_answers']
this is an array, the index will be the number of the question and you can do a foreach-loop

0

Thank you Pzuurveen, Imtrying to implement it this way but it seems not to be picking:

$answers = $_POST['selected_answers'];
echo '<b><u>THE ANSWERS YOU HAVE CHOSEN ARE:</u></b><br /><br />';
$answers [] = array('answer' => $questionNr);
foreach($answer as $k=>$v)
        {
            echo "$v[answer]";
        }

Where could I be going wrong? Thank you.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.