Jump to content

Passing form content


Ferdinand
Go to solution Solved by requinix,

Recommended Posts

In the following code, When I try to pass questions together with the answers, only the answers are Posted as $_POST, How do $_POST the questions to from the form together with the answers?

<html>
<head>
03
    <style>
04
#text_disabled{
05
border:0;
06
font-size:10pt;
07
color:black;
08
}
09
</style>
10
    <title>Question and Answer Read From Text File</title>
11
    </head>
12
    <body>
13
        <form action="processor.php" method="POST" align="center">
14
        <b><u> QUESTIONS AND ANSWERS QUIZ</u></b> <br /><br />
15
<?php
16
$openFile = fopen("questionandanswers.txt", "r") or exit ("unable to open the text       file");
17
$string = fread($openFile, filesize("questionandanswers.txt"));
18
 
19
 
20
//Regex to get from the first number to the string's end, so we ignore the Number Question... bit;
21
preg_match('/\d.*/', $string, $match);
22
 
23
//We get all the strings starting with a number until it finds another number (which will be the beginning of another question;
24
preg_match_all('/\d\D*/', $match[0], $results);
25
 
26
$qas = array(); // We prepare an array that will store the questions/answers
27
foreach($results[0] as $result)
28
{
29
    //Separating the question from the string with all the answers.
30
    list($question, $all_answers) = explode(':', $result);
31
 
32
    //Separating the different answers
33
     
34
    $answers_array = explode('.', $all_answers);
35
     
36
    //removes unnecesary spaces in the questions
37
     
38
    $answers_array_trimmed = array_map('trim', $answers_array);
39
     
40
    //Storing the question and the array with all the answers into the previously prepared array;
41
     
42
    $qas[] = array('question' => $question, 'answers' => $answers_array);
43
}
44
 
45
//Looping through the array and outputting all the info into a form;
46
foreach($qas as $k=>$v)
47
{
48
echo "<input id='text_disabled' style='width:40%' type='text' value='".$v['question']."' name='questions[]' disabled>";
49
echo "<select name='selected_answers[]'>";
50
foreach($v['answers'] as $answer){
51
echo "<option value='".$answer."'>".$answer."</option>";
52
}
53
echo "</select>";
54
echo "<br/><br/>";
55
}
56
?>
57
<input name= "submit" type="submit" value="Submit Answers">&nbsp <input type="reset" value ="Clear Answers">
58
</form>
59
    </body>
60
</html>

such that after $_POST I can do the following:

if(isset($_POST['submit']))
{print_r($_POST['questions'])}

and it prints the questions.

 

 

 

 

Link to comment
Share on other sites

Thank you requinix I have sorted it out, but how do I separate the questions and answers such that for each questions the respective multiple answers are displayed together? Currently the output is as follows:

1)	The most important feature of spiral model is 2)	The worst type of coupling is 3)	One of the fault base testing techniques is 4)	A fault simulation testing technique is 5)	RS is also known as specification of 6)	Which of these terms is a level name in the Capability Maturity Model?7)	FP-based estimation techniques require problem decomposition based on?	What types of models are created during software requirements analysis?9)	Which of the following is not an attribute of software engineering
Link to comment
Share on other sites

I have managed to pass the form submission with the questions. What I'm trying to accomplish is to for each question, its subsequent answer chosen follows. This is what I have so far:

These are the questions:


1) The most important feature of spiral model is2) The worst type of coupling is3) One of the fault base testing techniques is4) A fault simulation testing technique is5) RS is also known as specification of6) Which of these terms is a level name in the Capability Maturity Model?7) FP-based estimation techniques require problem decomposition based on? What types of models are created during software requirements analysis?9) Which of the following is not an attribute of software engineering 

These are the answers:

 

THE ANSWERS YOU HAVE CHOSEN ARE:

Data coupling

Unit testing
Mutation testing
White box testing
Ad hoc
Information domain values
Functional and behavioral
Efficiency

You Total Score is : 8 

Now What I am trying to achieve is something like this:

foreach ($question as quiz)
{
echo "echo $answers[]";
}

Without interfearing with my codes. These are the codes that I have on the same so far:


<html>
<head>
<title>Chosen Answers</title>
<style >
<!--
body {
background-color: #00FFFF;
}

-->
</style>
</head>
<body>
<pre>
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');

//Posting of the chosen answers
if(isset($_POST['submit']))
{
$question = $_POST['questions'];
$answers = $_POST['selected_answers'];
echo '<b><u>THE ANSWERS YOU HAVE CHOSEN ARE:</u></b><br /><br />';
$answers_trimmed = array_map('trim', $answers);
}
foreach($question as $quiz)
{
echo ($answers_trimmed);
}
//Opening of the answers file, reading and printing it

$openFile = fopen("answers.txt", "r") or exit ("unable to open the answers file");
$fileContents = fread($openFile, filesize("answers.txt"));
trim($fileContents);
fclose($openFile);
$delimiter = " ";
$myArray = explode($delimiter, $fileContents);
//Deletes unnecessary spaces in the read text files.
$myArray_trimmed = array_map('trim', $myArray);

//Computation of marks scored for the answered questions
$score = 0;
$no_questions = 9;

for ($x = 0; $x < $no_questions; $x++)
{
if ($answers_trimmed [$x]== $myArray_trimmed[$x])
{
echo "$answers_trimmed[$x]";
echo '<br />';
$score++;
}
}
echo '<br />';
echo "<b>You Total Score is : <u>$score</u></b>";
?>
</pre>
</body>
</html>
Link to comment
Share on other sites

  • Solution

Now What I am trying to achieve is something like this:

foreach ($question as quiz)
{
echo "echo $answers[]";
}

 

Sure, but that doesn't really explain what it's supposed to do.

 

Are you trying to output the answers along with the questions? Every question has a matching answer, right? From the form. Question #23 has a corresponding answer #23.

Get that #number using the key of that $question array you're looping over, then look up the answer to match.

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.