Jump to content

php script problem


vampke

Recommended Posts

Hi guys,

 

First post here and totally newby on php.

I tried to get this script to work but i can't.

I need a user to answer some questions with a radio box, each radio box will have a specific number assigned to it. At the end, the sum of these is shown to the users.

apparently the script will work 'somewhat'. If I answer all but one questions with option 5 and the other with option 4 i will get 25 as a result, where it should be 24.

 

example on http://flor.astralwebhost.org/quiz.html

 

php code:

<?php
$result = intval($_POST['question1']) + intval($_POST['question2']) + intval($_POST['question3']) + intval($_POST['question4']) + intval($_POST['question5']) ;
if ($result < '21') {     $msg="<p>score = " . $result . ". Not bad.</p> ";
    }
elseif ($result ='25') {
    $msg="<p>Perfect score! " . $result . " points!</p> ";
    }
elseif ($result >'20') {
    $msg="<p>Loser! Only " . $result . " points.</p>";
    }
?>
<?php print $msg; ?>

 

html code:

<form action="calc.php" method="post">
question1<br />
<input type="radio" name="question1" value="1">value=1<br />
<input type="radio" name="question1" value="2">value=2<br />
....
<input type="radio" name="question5" value="5">value=5<br />
<br />
<input name="post" type="submit" value="post" />
</form>

 

anyone any ideas?

Link to comment
https://forums.phpfreaks.com/topic/38761-php-script-problem/
Share on other sites

vampke,

 

Howdy!  I ran your script through Zend Studio 5.5 Professional ( $299 from Zend.com ) and it pointed to the problem right away...

 

 

Your line : elseif ($result ='25') { 

 

Should be:

 

elseif ($result ==  '25') {

 

which means: If  $result is EXACTLY EQUAL To the Value '25', then do something.

 

The way you have it coded, you are re-assigning the value from your $_POST arithmetic to '25' by your use of a single equal sign.

 

Scot L. Diddle, Richmond VA

 

 

Link to comment
https://forums.phpfreaks.com/topic/38761-php-script-problem/#findComment-186252
Share on other sites

ProjectFear,

 

The "worth" of a program product is relative.  I know the syntax of PHP pretty well, but I did not spot the error manually.  I popped out at me as I saw the value of $result change from 24 to 25 by using the IDE.

 

As a noobee myself, I find the use of tools that are available go a great way towards productivity and an increased understanding of what PHP can, and cannot do, and why thing you thought would work, don't

 

I agree that Zend Studio 5.5 Professional is expensive, but I have never regretted spending the money.

 

Scot L. Diddle, Richmond VA

 

 

Link to comment
https://forums.phpfreaks.com/topic/38761-php-script-problem/#findComment-186265
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.