tecmeister Posted February 16, 2008 Share Posted February 16, 2008 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 Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted February 16, 2008 Share Posted February 16, 2008 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 Quote Link to comment Share on other sites More sharing options...
tecmeister Posted February 16, 2008 Author Share Posted February 16, 2008 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 () Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted February 16, 2008 Share Posted February 16, 2008 Put the session_start() at the very start of the scripts: <?php session_start(); ?> Ken Quote Link to comment Share on other sites More sharing options...
tecmeister Posted February 16, 2008 Author Share Posted February 16, 2008 No i'm still getting the same error. I have had this error everytime i have entered it. Is there something that im going wrong. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted February 16, 2008 Share Posted February 16, 2008 You have to put it before any output is sent to the browser. Please post the entire script. Ken Quote Link to comment Share on other sites More sharing options...
tecmeister Posted February 16, 2008 Author Share Posted February 16, 2008 <!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> Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted February 16, 2008 Share Posted February 16, 2008 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 Quote Link to comment Share on other sites More sharing options...
tecmeister Posted February 16, 2008 Author Share Posted February 16, 2008 That works thank you so much. That shows you how much of a newbie i am. There is another question for you. How do i make it so the users can't go back and change the answer? Thanks again for the help you have given me. tecmeister Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.