Jump to content

add to score


tecmeister

Recommended Posts

Hi everyone,

 

I have entered a code for the score, but on every page it displays 2.

 

How do i make each question add to the score?

 

This is the code that i have used on the first page:

<?php
$username = $_REQUEST['username'];
echo $username;
?>
</div>
<div style="z-index: 101; position: absolute; top: 53px; width: 39px; left: 76px;">
<?php
$score = 0
?>
<?php
echo $score;
?>
</div>
</div>

<div style="z-index: 300; position: absolute; left: 460px; width: 334px; height: 203px; top: 70px;">
Which State in America was the Titanic Heading to?
  </td>
</tr>
<tr>
  <td>
   <form method="post" action="adults easy q2.php">
   <input type="hidden" value="<? echo $score ?>" />
   <table width="133" align="center">
    <tr>
     <td width="112">
   Florida
   </td>
   <td>
     <input type="radio" name="answer" value="false" />
   </td>
   </tr>
   <tr>
   <td>
   New York
   </td>
   <td>
   <input type="radio" name="answer" value="true" />
   </td>
   </tr>
   <tr>
   <td>
   Chicago
   </td>
   <td>
   <input type="radio" name="answer" value="false" />
   </td>
   </tr>
   <tr>
   <td>
   L.A
   </td>
   <td>
   <input type="radio" name="answer" value="false" />
   </td>
   </tr>
   <tr>
   <td align="center"><input type="submit" value="Submit" />
   </td></tr>
   </table>
   </form>
   
   </table>
</div>

 

This is the code on every other page:

<?php
$score = $_REQUEST['score'];
$answer = $_REQUEST['answer'];
?>
</head>

<body>


<div style="z-index: 101; position: absolute; background-image: url(icon/score_user.png); background-repeat:no-repeat; left: 251px; width: 151px; height: 101px; top: 26px;">
<div style="z-index: 101; position: absolute; width: 89px; height: 25px; left: 36px; top: 22px;">

</div>
<div style="z-index: 101; position: absolute; top: 53px; width: 39px; left: 76px;">

<?php

if ($answer == "true"){
$score = $score +2;
} else {
$score = $score -1;
}
echo $score;

?>
</div>
</div>

<div style="z-index: 300; position: absolute; left: 460px; width: 308px; height: 203px; top: 70px;">
Which planet is closest to the sun?
  <form method="post" action="adults easy q3.php">
   <table width="133" align="center">
    <tr>
     <td width="112">Mercury</td>
     <td>
     <input type="radio" name="answer" value="true" />
   </td>
   </tr>
   <tr>
   <td>Saturn</td>

   <td>
   <input type="radio" name="answer" value="false" />
   </td>
   </tr>
   <tr>
   <td>Mars</td>
   <td>
   <input type="radio" name="answer" value="false" />
   </td>
   </tr>
   <tr>
   <td>
   Jupitor</td>
   <td><input type="radio" name="answer" value="false" />
   </td>
   </tr>
   <tr>
   <td align="center">
   
   <input type="submit" value="Submit" />
   </td></tr>
   </table>
   </form>
   
</table>

<?php
$score = $_POST['score'];
?>
</div>

 

Thanks for your help

 

tecmeister

Link to comment
https://forums.phpfreaks.com/topic/91393-add-to-score/
Share on other sites

You're not passing the new score to other scripts, you should be using sessions. Also, having spaces in file names is not a good idea. It will work now, but may break in the future. Another thing, since you are using method="post" in the forms, use $_POST instead of $_REQUEST.

 

In your first script:

<?php
session_start();
$_SESSION['score'] = 0;
?>

and remove

<input type="hidden" value="<?php echo $score ?>" />

 

In the other scripts:

<?php
session_start();
if ($_POST['answer'] == "true")
     $_SESSION['score'] += 2;
else
     $_SESSION['score']--;
echo $_SESSION['score'];
?>

 

Remove

<?php
$score = $_REQUEST['score'];
$answer = $_REQUEST['answer'];
?>

and

<?php
$score = $_POST['score'];
?>

 

Ken

Link to comment
https://forums.phpfreaks.com/topic/91393-add-to-score/#findComment-468315
Share on other sites

When i enter that code i'm getting a error with the session:

 

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home/sites/nurevolution.co.uk/public_html/quiz/adults easy q1.php:12) in /home/sites/nurevolution.co.uk/public_html/quiz/adults easy q1.php on line 21

 

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/sites/nurevolution.co.uk/public_html/quiz/adults easy q1.php:12) in /home/sites/nurevolution.co.uk/public_html/quiz/adults easy q1.php on line 21

 

line 21 = session start ()

Link to comment
https://forums.phpfreaks.com/topic/91393-add-to-score/#findComment-468325
Share on other sites

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>

</head>

<body>
<?php
session_start();
?>


<div style="z-index: 101; position: absolute; background-image: url(icon/score_user.png); background-repeat:no-repeat; left: 251px; width: 151px; height: 101px; top: 26px;">
<div style="z-index: 101; position: absolute; width: 89px; height: 25px; left: 36px; top: 22px;">
<?php
$username = $_REQUEST['username'];
echo $username;
?>
</div>
<div style="z-index: 101; position: absolute; top: 53px; width: 39px; left: 76px;">

<?php
$_SESSION['score'] = 0;
echo $score;
?>
</div>
</div>

<div style="z-index: 300; position: absolute; left: 460px; width: 334px; height: 203px; top: 70px;">
Which State in America was the Titanic Heading to?
  </td>
</tr>
<tr>
  <td>
   <form method="post" action="adults easy q2.php">
     <table width="133" align="center">
    <tr>
     <td width="112">
   Florida
   </td>
   <td>
     <input type="radio" name="answer" value="false" />
   </td>
   </tr>
   <tr>
   <td>
   New York
   </td>
   <td>
   <input type="radio" name="answer" value="true" />
   </td>
   </tr>
   <tr>
   <td>
   Chicago
   </td>
   <td>
   <input type="radio" name="answer" value="false" />
   </td>
   </tr>
   <tr>
   <td>
   L.A
   </td>
   <td>
   <input type="radio" name="answer" value="false" />
   </td>
   </tr>
   <tr>
   <td align="center"><input type="submit" value="Submit" />
   </td></tr>
   </table>
   </form>
   
   </table>
</div>

</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/91393-add-to-score/#findComment-468341
Share on other sites

That's not at the very start of the script. Do this:

<?php
session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>

</head>

<body>

 

Ken

Link to comment
https://forums.phpfreaks.com/topic/91393-add-to-score/#findComment-468343
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.