Jump to content

Passing Form $_Post Content


Ferdinand

Recommended Posts

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">&nbsp <input type="reset" value ="Clear Answers">
</form>
    </body>
</html>
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 by davidannis
Link to comment
Share on other sites

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] => "
)
Link to comment
Share on other sites

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 by oaass
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

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.