Display the $_POST content

  • (2 Pages)
  • +
  • 1
  • 2

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

#16 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3741
  • View blog
  • Posts: 13,092
  • Joined: 12-December 12

Re: Display the $_POST content

Posted 28 April 2013 - 12:42 PM

Your code is trying to read $_POST['selected_answers'], not 'selected_answer'.

Also, they won't get a score if the casing (upper or lower) is not exactly the same, and if there are any extra spaces involved. This is partly why I recommended previously that you use A, B, C, D as the input-values: you don't need to store, or check, the exact text of the answer.

However, if you wish to continue testing the full answer-text, then consider using strtoupper() to compare upper-case with upper-case, and trim() to remove unnecessary spaces.
Was This Post Helpful? 0
  • +
  • -

#17 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 - 09:41 PM

Kindly assist how I am suppose to go about it, I am new to php and still have not grasped much of the php functions. and how to implement the A, B, C, D concept. Thank you.
Was This Post Helpful? 0
  • +
  • -

#18 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3741
  • View blog
  • Posts: 13,092
  • Joined: 12-December 12

Re: Display the $_POST content

Posted 29 April 2013 - 01:36 AM

View Postferdinandmucos, on 29 April 2013 - 04:41 AM, said:

Kindly assist how I am suppose to go about it, I am new to php and still have not grasped much of the php functions. and how to implement the A, B, C, D concept. Thank you.

You should be taking a tutorial, or studying a book, to learn all the fundamentals of PHP first. Then you'll be able to do whatever you want :)
Was This Post Helpful? 0
  • +
  • -

#19 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 29 April 2013 - 03:50 AM

If I could introduce the following rule to check on the correct answer in the text file:
preg_match_all('/\d!{0,3}', $match[0], $result);


I think passing them together with the questions and the selected answers and compare against the correct answer in the text file will have solved my problem. How to pass both the correct answer and the question and compare against the correct answer is the problem, Kindly assist how to go about it. Thank you.
Was This Post Helpful? 0
  • +
  • -

#20 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 30 April 2013 - 02:06 AM

I changed and had the following answers in another text file called answers.txt . Now what is submitted from the form and the answers read from the text file are suppose to be compared. if the answers is similar to the correct answer it adds one otherwise adds zero. This is the code:
<html>
<head>
<title>Chosen answers</title>
</head>
<body>
<pre>
<?php
//Posting of the chosen answers

$answers = $_POST['selected_answers'];
echo '<b><u>THE ANSWERS YOU HAVE CHOSEN ARE:</u></b><br /><br />';
print_r($answers);

//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"));
fclose($openFile);
$delimiter = "  ";
$myArray = explode($delimiter, $fileContents);

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

//Computation of marks scored for the answered questions

if ($answers[0] == $myArray[0])
{
    $score = 1;
}
elseif ($answers[0] !=$myArray[0])
{
    $score = 0;
}echo '<br />';

if ($answers[1] == $myArray[1])
{
    $score1 = 1;
}
elseif ($answers[1] !=$myArray[1])
{
    $score1 = 0;
}echo '<br />';

if ($answers[2] == $myArray[2])
{
    $score2 = 1;
}
elseif ($answers[2] !=$myArray[2])
{
    $score2 = 0;
}echo '<br />';

if ($answers[3] == $myArray[3])
{
    $score3 = 1;
}
elseif ($answers[3] !=$myArray[3])
{
    $score3 = 0;
}echo '<br />';
if ($answers[4] == $myArray[4])
{
    $score4 = 1;
}
elseif ($answers[4] !=$myArray[4])
{
    $score4 = 0;
}echo '<br />';

if ($answers[5] == $myArray[5])
{
    $score5 = 1;
}
elseif ($answers[5] !=$myArray[5])
{
    $score5 = 0;
}echo '<br />';

if ($answers[6] == $myArray[6])
{
    $score6 = 1;
}
elseif ($answers[6] !=$myArray[6])
{
    $score6 = 0;
}echo '<br />';

if ($answers[7] == $myArray[7])
{
    $score7 = 1;
}
elseif ($answers[7] !=$myArray[7])
{
    $score7 = 0;
}echo '<br />';

if ($answers[8] == $myArray[8])
{
    $score8 = 1;
}
elseif ($answers[8] !=$myArray[8])
{
    $score8 = 0;
}

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

echo "<b><u>$Total</u></b>";
?>
</pre>
</body>
</html>


The problem is the arrays does not seem to compare and the computation does not seem to work, what might still be the problem with my code? thank you.
Was This Post Helpful? 0
  • +
  • -

#21 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3741
  • View blog
  • Posts: 13,092
  • Joined: 12-December 12

Re: Display the $_POST content

Posted 30 April 2013 - 02:31 AM

$delimiter = "  ";

You have two spaces quoted in the above line.

There is no need for all those elseifs as you have set the scores initially to 0 anyway.
Was This Post Helpful? 0
  • +
  • -

#22 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 30 April 2013 - 03:08 AM

The
$delimiter = "  ";


is used to separate the answers in the text file.
And also after removing the elseif part its still not computing the score. I don't understand why.
Was This Post Helpful? 0
  • +
  • -

#23 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3741
  • View blog
  • Posts: 13,092
  • Joined: 12-December 12

Re: Display the $_POST content

Posted 30 April 2013 - 03:24 AM

View Postferdinandmucos, on 30 April 2013 - 10:08 AM, said:

The
$delimiter = "  ";


is used to separate the answers in the text file.
And also after removing the elseif part its still not computing the score. I don't understand why.

I didn't suggest that removing the elseifs would fix your code, just that they were unnecessary.

Print out $myArray and compare it to $answers.
Was This Post Helpful? 0
  • +
  • -

#24 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 30 April 2013 - 03:48 AM

After printing $myArray: this is still the output...

Array
(
[0] => Risk management
[1] => stamp coupling
[2] => Stress testing
[3] => Black box testing
[4] => Integrated testing
[5] => Reusable
[6] => software functions
[7] => Architectural and structural
[8] => Scalability
)
Array
(
[0] => Risk management
[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
)

0

This post has been edited by ferdinandmucos: 30 April 2013 - 03:50 AM

Was This Post Helpful? 0
  • +
  • -

#25 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3741
  • View blog
  • Posts: 13,092
  • Joined: 12-December 12

Re: Display the $_POST content

Posted 30 April 2013 - 03:59 AM

Assuming the first array lists the answers then they should have a score of 1. But perhaps there are extra spaces at the end of either of the elements. Use trim() to remove these.

If you also add the following lines to the top of your PHP then it will display all errors:

error_reporting(E_ALL);
ini_set('display_errors', '1');

Was This Post Helpful? 0
  • +
  • -

#26 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 30 April 2013 - 05:43 AM

I have tried to implement it by using trim() and the display error tags but the problem still remains. Am I implementing the arrays correctly?

<html>
<head>
<title>Chosen Answers</title>
</head>
<body>
<pre>
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');

//Posting of the chosen answers

$answers = $_POST['selected_answers'];
echo '<b><u>THE ANSWERS YOU HAVE CHOSEN ARE:</u></b><br /><br />';
print_r($answers);

//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);
print_r($myArray);

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

//Computation of marks scored for the answered questions

if ($answers[0] == $myArray[0])
{
    $score = 1;
}
if ($answers[1] == $myArray[1])
{
    $score1 = 1;
}
if ($answers[2] == $myArray[2])
{
    $score2 = 1;
}
if ($answers[3] == $myArray[3])
{
    $score3 = 1;
}
if ($answers[4] == $myArray[4])
{
    $score4 = 1;
}

if ($answers[5] == $myArray[5])
{
    $score5 = 1;
}

if ($answers[6] == $myArray[6])
{
    $score6 = 1;
}

if ($answers[7] == $myArray[7])
{
    $score7 = 1;
}
if ($answers[8] == $myArray[8])
{
    $score8 = 1;
}

$Total = $score + $score1 + $score2 + $score3 + $score4 + $score5 + $score6 + $score7 + $score8 ;
echo '<br />';
echo "<b><u>$Total</u></b>";
?>
</pre>
</body>
</html>


Was This Post Helpful? 0
  • +
  • -

#27 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3741
  • View blog
  • Posts: 13,092
  • Joined: 12-December 12

Re: Display the $_POST content

Posted 30 April 2013 - 06:01 AM

trim($fileContents);

This will trim the beginning and end of the whole file, not the array elements.

$myArray_trimmed = array_map('trim', $myArray);

Of course, I can't guarantee that extra spaces are causing your page to fail, but this is a sensible thing to do anyway.

Add this line also:

echo '*' . $answers[0] . '*' . $myArray[0] . '*';    //etc.
// or
echo '*' . $answers[0] . '*' . $myArray_trimmed[0] . '*';

then you will be able to see EXACTLY what you are comparing.

This post has been edited by andrewsw: 30 April 2013 - 06:02 AM

Was This Post Helpful? 0
  • +
  • -

#28 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 30 April 2013 - 06:08 AM

Actually its comparing what I need to compare because when I run the code, this is the output:

* requirement analysis*Risk management*


Was This Post Helpful? 0
  • +
  • -

#29 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3741
  • View blog
  • Posts: 13,092
  • Joined: 12-December 12

Re: Display the $_POST content

Posted 30 April 2013 - 06:13 AM

View Postferdinandmucos, on 30 April 2013 - 01:08 PM, said:

Actually its comparing what I need to compare because when I run the code, this is the output:

* requirement analysis*Risk management*


This answer is still wrong of course, and there is an extra space at the front, so apply trim to both arrays, using the sample code in my previous post.
Was This Post Helpful? 0
  • +
  • -

#30 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 30 April 2013 - 06:48 AM

I have trimmed it by implementing:
$answers_trimmed = array_map('trim', $answers);

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2