Display the $_POST content

  • (2 Pages)
  • +
  • 1
  • 2

29 Replies - 755 Views - Last Post: 30 April 2013 - 06:48 AM Rate Topic: **--- 1 Votes

#1 ferdinandmucos  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 23-April 13

Display the $_POST content

Posted 25 April 2013 - 03:01 AM

I want to be able to display the answered questions, their answers either the wright one or the wrong one and be able to compute the score and display the total outcome. this is the code I have:
  
    <?php
    $score=0;
    print_r($_POST[question]);
    $answers = $_POST['selected_answers'];
    //First question
    if ($answers[0]=="risk management")
    {
    $score =1;
    }
   elseif($answers[0]=="quality management")
    {
    $score =0;
    }
    elseif($answers[0]=="configuration management")
    {
    $score =0;
    }
    else
    {
    print_r($answers[0]);
    }

    //Second question
    if ($answers[1]=="Data coupling")
    {
    $score1 =1;
    }
    elseif($answers[1]=="Control coupling")
    {
    $score1 =0;
    }
    elseif($answers[1]=="Stamp coupling")
    {
    $score1 =0;
    }
     else
     {
     print_r($answers[1]);
     }

    //third question
    if ($answers[2]=="Unit testing")
    {
    $score2 =1;
    }
    elseif($answers[2]=="Beta testing")
    {
    $score2 =0;
    }
    elseif($answers[2]=="Stress testing")
    {
    $score2 =0;
    }
    else
    {
    print_r($answers[2]);
    }

    //fourth question

    if ($answers[3]=="Mutation testing")
    {
    $score3 =1;
    }
    elseif($answers[3]=="Stress testing")
    {
    $score3 =0;
    }
    elseif($answers[3]=="Black box testing")
    {
    $score3 =0;
    }
    else
    {
    print_r($answers[3]);
    }
    //fifth question

    if ($answers[4]=="white box testing")
    {
    $score4 =1;
    }
    elseif($answers[4]=="Stress testing")
    {
    $score4 =0;
    }
    elseif($answers[4]=="integrated testing")
    {
    $score4 =0;
    }
    else
    {
    print_r($answers[4]);
    }

    //sixth question

    if ($answers[5]=="Ad hoc")
    {
    $score5 =1;
    }
    elseif($answers[5]=="Repeatable")
    {
    $score5 =0;
    }
    elseif($answers[5]=="Reusable")
    {
    $score5 =0;
    }
    else
    {
    print_r($answers[5]);
    }

    //seventh question
    if ($answers[6]=="information domain values")
    {
    $score6 =1;
    }
    elseif($answers[6]=="project schedule")
    {
    $score6 =0;
    }
    elseif($answers[6]=="software function")
    {
    $score6 =0;
    }
    else
    {
    print_r($answers[6]);
    }


    //eigth question

    if ($answers[7]=="Functional and behavioral")
    {
    $score7 =1;
    }
    elseif($answers[7]=="Algorithmic and data structure")
    {
    $score7 =0;
    }
    elseif($answers[7]=="Architectural and structural")
    {
    $score7 =0;
    }
    else
    {
    print_r($answers[7]);
    }

    //nineth question

    if ($answers[8]=="Efficiency")
    {
    $score8 =1;
    }
    elseif($answers[8]=="Scalability")
    {
    $score8 =0;
    }
    elseif($answers[8]=="Dependability")
    {
    $score8 =0;
    }
    else
    {
    print_r($answers[8]);
    }

    //code to generate the total score 

    $total =  $score + $score1 + $score2 + $score3 + $score4 + $score5 
		+ $score6 + $score7 + $score8;

		echo '$total';
    ?>


the code that passes the the question and answers is:
    <html>
    <head>
	<style>
    #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" align="center">
        <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 with all 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);

    //Stuffing 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;
    foreach($qas as $k=>$v)
    {
    echo "<input id='text_disabled' style='width:40%' type='text' value='".$v['question']."'     name='questions[]' disabled>";
    echo "<select name='selected_answers[]>";
    foreach($v['answers'] as $answer){
    echo "<option value='".$answer."'>".$answer."</option>";
    }
    echo "</select>";
    echo "<br/><br/>";
    }
    ?>
    <input type="submit" value="Submit Answers">&nbsp <input type="reset" value ="Clear   Answers">
    </form>
    </body>
    </html>



Where could I be going wrong? Because I have tried to check but I am not seeing.

Is This A Good Question/Topic? 0
  • +

Replies To: Display the $_POST content

#2 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3177
  • View blog
  • Posts: 10,639
  • Joined: 12-December 12

Re: Display the $_POST content

Posted 25 April 2013 - 03:16 AM

Do you receive errors? If so, what are they and what lines do they refer to? Otherwise you will need to describe what is wrong with the code: what does it do, and what is it supposed to do.

However, you are not assigning $score1, etc., with a default value, so if the answer is wrong these variables are unassigned.

BTW You shouldn't use print_r() with production code, this is purely to help with debugging.
Was This Post Helpful? 0
  • +
  • -

#3 ferdinandmucos  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 23-April 13

Re: Display the $_POST content

Posted 25 April 2013 - 03:36 AM

I wanted to display the questions, the answers both the chosen ones and the others and also try to add 1 for each wright chosen answer and 0 for each wrong chosen answer. And in the end compute the obtained score out of the total possible score. Thank you.

May be to explain what I had. I had the following text file that has questions and four multiple answers.

$string = 
1)	The most important feature of spiral model is: requirement analysis. risk management. quality management. configuration management 2)	The worst type of coupling is: Data coupling. control coupling. stamp coupling. content coupling 3)	One of the fault base testing techniques is: unit testing. beta testing. Stress testing. mutation testing 4)	A fault simulation testing technique is: Mutation testing. Stress testing. Black box testing. White box testing 5)	RS is also known as specification of: White box testing. Stress testing. Integrated testing. Black box testing 6)	Which of these terms is a level name in the Capability Maturity Model?: Ad hoc. Repeatable. Reusable. Organized 7)	FP-based estimation techniques require problem decomposition based on?: information domain values. project schedule. software functions. process activities 8)	What types of models are created during software requirements analysis?: Functional and behavioral. Algorithmic and data structure. Architectural and structural. Usability and reliability 9)	Which of the following is not an attribute of software engineering: Efficiency. Scalability. Dependability. Usability 	



The code that was suppose to read the text file, put the questions and answers into html forms that can be read, selected and submitted:

<html>
    <head>
	<style>
#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" align="center">
        <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 with all 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);

    //Stuffing 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;
foreach($qas as $k=>$v)
{
echo "<input id='text_disabled' style='width:40%' type='text' value='".$v['question']."' name='questions[]' disabled>";
echo "<select name='selected_answers[]>";
foreach($v['answers'] as $answer){
echo "<option value='".$answer."'>".$answer."</option>";
}
echo "</select>";
echo "<br/><br/>";
}
?>
<input type="submit" value="Submit Answers">&nbsp <input type="reset" value ="Clear Answers">
</form>
    </body>
</html>



and the processor.php

which is suppose to do the above mentioned.

<?php
$answers = $_POST['selected_answers'];
$total=0;

if ($answers[0]=="Risk management")
{
$total++;
}
else
{
print_r($answers[0]);
}
if ($answers[1]=="Data coupling")
{
$total++;
}
else
{
print_r($answers[1]);
}

//third question
if ($answers[2]=="Unit testing")
{
$total++;
}
else
{
print_r($answers[2]);
}

//fourth question

if ($answers[3]=="Mutation testing")
{
$total++;
}
else
{
print_r($answers[3]);
}
//fifth question

if ($answers[4]=="white box testing")
{
$total++;
}
else
{
print_r($answers[4]);
}

//sixth question

if ($answers[5]=="Ad hoc")
{
$total++;
}
else
{
print_r($answers[5]);
}

//seventh question
if ($answers[6]=="information domain values")
{
$total++;
}
else
{
print_r($answers[6]);
}


//eigth question

if ($answers[7]=="Functional and behavioral")
{
$total++;
}
else
{
print_r($answers[7]);
}

//nineth question

if ($answers[8]=="Efficiency")
{
$total++;
}
else
{
print_r($answers[8]);
}

//code to generate the total score 
echo '<br /> <br />';
    echo $total;
?>


Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3177
  • View blog
  • Posts: 10,639
  • Joined: 12-December 12

Re: Display the $_POST content

Posted 25 April 2013 - 03:51 AM

You still haven't explained the problems, or errors, you have. The code in your second post is different to that in your first; which code are you asking about?
Was This Post Helpful? 0
  • +
  • -

#5 ferdinandmucos  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 23-April 13

Re: Display the $_POST content

Posted 25 April 2013 - 04:16 AM

The problem is the html form does not display the four multiple choices of each question. It only displays three of them. I don't receive any errors but when I run the codes, It does display the answers but is does not display the total outcome as expected. I posted the one i'm trying to edit, I am talking about this code:

<?php
    $score=0;
    print_r($_POST[question]);
    $answers = $_POST['selected_answers'];
    //First question
    if ($answers[0]=="risk management")
    {
    $score =1;
    }
   elseif($answers[0]=="quality management")
    {
    $score =0;
    }
    elseif($answers[0]=="configuration management")
    {
    $score =0;
    }
    else
    {
    print_r($answers[0]);
    }

    //Second question
    if ($answers[1]=="Data coupling")
    {
    $score1 =1;
    }
    elseif($answers[1]=="Control coupling")
    {
    $score1 =0;
    }
    elseif($answers[1]=="Stamp coupling")
    {
    $score1 =0;
    }
     else
     {
     print_r($answers[1]);
     }

    //third question
    if ($answers[2]=="Unit testing")
    {
    $score2 =1;
    }
    elseif($answers[2]=="Beta testing")
    {
    $score2 =0;
    }
    elseif($answers[2]=="Stress testing")
    {
    $score2 =0;
    }
    else
    {
    print_r($answers[2]);
    }

    //fourth question

    if ($answers[3]=="Mutation testing")
    {
    $score3 =1;
    }
    elseif($answers[3]=="Stress testing")
    {
    $score3 =0;
    }
    elseif($answers[3]=="Black box testing")
    {
    $score3 =0;
    }
    else
    {
    print_r($answers[3]);
    }
    //fifth question

    if ($answers[4]=="white box testing")
    {
    $score4 =1;
    }
    elseif($answers[4]=="Stress testing")
    {
    $score4 =0;
    }
    elseif($answers[4]=="integrated testing")
    {
    $score4 =0;
    }
    else
    {
    print_r($answers[4]);
    }

    //sixth question

    if ($answers[5]=="Ad hoc")
    {
    $score5 =1;
    }
    elseif($answers[5]=="Repeatable")
    {
    $score5 =0;
    }
    elseif($answers[5]=="Reusable")
    {
    $score5 =0;
    }
    else
    {
    print_r($answers[5]);
    }

    //seventh question
    if ($answers[6]=="information domain values")
    {
    $score6 =1;
    }
    elseif($answers[6]=="project schedule")
    {
    $score6 =0;
    }
    elseif($answers[6]=="software function")
    {
    $score6 =0;
    }
    else
    {
    print_r($answers[6]);
    }


    //eigth question

    if ($answers[7]=="Functional and behavioral")
    {
    $score7 =1;
    }
    elseif($answers[7]=="Algorithmic and data structure")
    {
    $score7 =0;
    }
    elseif($answers[7]=="Architectural and structural")
    {
    $score7 =0;
    }
    else
    {
    print_r($answers[7]);
    }

    //nineth question

    if ($answers[8]=="Efficiency")
    {
    $score8 =1;
    }
    elseif($answers[8]=="Scalability")
    {
    $score8 =0;
    }
    elseif($answers[8]=="Dependability")
    {
    $score8 =0;
    }
    else
    {
    print_r($answers[8]);
    }

    //code to generate the total score 

    $total =  $score + $score1 + $score2 + $score3 + $score4 + $score5 
		+ $score6 + $score7 + $score8;

		echo '$total';
    ?>

Was This Post Helpful? 0
  • +
  • -

#6 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3177
  • View blog
  • Posts: 10,639
  • Joined: 12-December 12

Re: Display the $_POST content

Posted 25 April 2013 - 04:51 AM

You seem to have missed my previous advice: You are not giving $score1, $score2, etc., a value if the answer is not one of the three that you are checking for.
Was This Post Helpful? 0
  • +
  • -

#7 ferdinandmucos  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 23-April 13

Re: Display the $_POST content

Posted 25 April 2013 - 05:37 AM

How can I go about it then? Give me an insight how to go about it, then i proceed from there... Thank you.
Was This Post Helpful? 0
  • +
  • -

#8 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3177
  • View blog
  • Posts: 10,639
  • Joined: 12-December 12

Re: Display the $_POST content

Posted 25 April 2013 - 05:46 AM

View Postferdinandmucos, on 25 April 2013 - 12:37 PM, said:

How can I go about it then? Give me an insight how to go about it, then i proceed from there... Thank you.

$score = $score1 = .. = 0;

This post has been edited by andrewsw: 25 April 2013 - 05:46 AM

Was This Post Helpful? 0
  • +
  • -

#9 ferdinandmucos  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 23-April 13

Re: Display the $_POST content

Posted 28 April 2013 - 12:55 AM

Despite having done how you instructed, its still not computing the total score. And How do I display the questions posted too, together with the answers. Thank you.
Was This Post Helpful? 0
  • +
  • -

#10 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3177
  • View blog
  • Posts: 10,639
  • Joined: 12-December 12

Re: Display the $_POST content

Posted 28 April 2013 - 03:44 AM

Check the posted data using print_r($_POST); view the source for the page as it will be easier to read. Does this array contain all the information that you need to work out their score? If not, then you'll need to concentrate on the first page.

Although your code can be made to work it is a bit elaborate for what it does. For the total score there is no need to create a separate $scorex variable for each question. You could just have one $score (or $total) variable, and set it initially to zero:

$total = 0;

Then, if they answer a question correctly, increment this value:

$total++;

If you want to store whether each question was answered correctly or not then I would store this information in an array.

View Postferdinandmucos, on 28 April 2013 - 07:55 AM, said:

And How do I display the questions posted too, together with the answers. Thank you.

You'll need to open the file questionandanswers.txt again, in order to retrieve the questions.
Was This Post Helpful? 0
  • +
  • -

#11 ferdinandmucos  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 23-April 13

Re: Display the $_POST content

Posted 28 April 2013 - 04:29 AM

H ad tried it, this was my code and still it was not computing the total score:

<pre>
<?php

$score =  0;
if (isset($_POST['submit']))
{
$answers = $_POST['selected_answers'];

echo '<br />';
//First question

if ($answers[0]=="risk management")
{
$score++;
}
echo '<br />';

//Second question
if ($answers[1]=="Data coupling")
{
$score++;
}
echo '<br />';

//third question
if ($answers[2]=="Unit testing")
{
$score++;
}
echo '<br />';
//fourth question

if ($answers[3]=="Mutation testing")
{
$score++;
}

echo '<br />';
//fifth question

if ($answers[4]=="white box testing")
{
$score ++;
}
echo '<br />';
//sixth question

if ($answers[5]=="Ad hoc")
{
$score++;
}
echo '<br />';

//seventh question
if ($answers[6]=="information domain values")
{
$score++;
}

echo '<br />';
//eigth question

if ($answers[7]=="Functional and behavioral")
{
$score++;
}

echo '<br />';
//nineth question

if ($answers[8]=="Efficiency")
{
$score++;
}
}
//code to generate the total score 
echo '<br />';

echo '<b>Your Total score is: $score </b>';

?>
</pre>

Was This Post Helpful? 0
  • +
  • -

#12 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3177
  • View blog
  • Posts: 10,639
  • Joined: 12-December 12

Re: Display the $_POST content

Posted 28 April 2013 - 04:38 AM

Using apostrophes, PHP won't interpret variables. Use double-quotes:

echo "<b>Your Total score is: $score </b>";

Was This Post Helpful? 0
  • +
  • -

#13 ferdinandmucos  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 23-April 13

Re: Display the $_POST content

Posted 28 April 2013 - 04:55 AM

just by inserting that code everything in the page goes blank except that it displays the

Your Total score is: 0 

yet they are the correct answers.
Was This Post Helpful? 0
  • +
  • -

#14 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3177
  • View blog
  • Posts: 10,639
  • Joined: 12-December 12

Re: Display the $_POST content

Posted 28 April 2013 - 09:04 AM

Based on your recent code, there is nothing else that you are outputting to the page :dontgetit:/>

If the score is 0 then what printed when you used print_r to display the $_POST array?

This post has been edited by Dormilich: 30 April 2013 - 12:57 AM

Was This Post Helpful? 0
  • +
  • -

#15 ferdinandmucos  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 23-April 13

Re: Display the $_POST content

Posted 28 April 2013 - 12:32 PM

This is what printed when is used the print_r($_POST['selected_answer'])

Array ( [0] => requirement analysis [1] => Data coupling [2] => Unit testing [3] => Mutation testing [4] => White box testing [5] => Ad hoc [6] => Information domain values [7] => Functional and behavioral [8] => Efficiency ) 


Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2