Jump to content

Display the username


tecmeister

Recommended Posts

Im having trouble displaying the username on every page.

 

I get it on the first page.  But that is it.

 

This is the code that im using of the page you enter your username:

<form method="post" action="q1.php">
<input type="text" name="username" />
<input type="submit" value="Enter" />
</form>

This second page:

<?php
$username = $_REQUEST['username'];
echo $username;
?>

 

I use the same code on the other pages but it will not display the username.

 

Thanks for your help.

 

tecmeister

Link to comment
https://forums.phpfreaks.com/topic/91505-display-the-username/
Share on other sites

I have entered the $_SESSION.

 

I seem to be have the same problem.

This is the code of the first question:

<?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>



<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
$_SESSION['username'] = $_POST['username'];
echo $_SESSION['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="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>

 

Second question page:

<?php


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

<!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>


<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
$_SESSION['username'] = $_POST['username'];
echo $_SESSION['username'];
?>

</div>
<div style="z-index: 101; position: absolute; top: 53px; width: 39px; left: 76px; height: 16px;">
<?php
echo $_SESSION['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="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>

</div>

</body>
</html>

 

Thanks for your help guru.

 

tecmeister

Link to comment
https://forums.phpfreaks.com/topic/91505-display-the-username/#findComment-468714
Share on other sites

ok for quick fix add a hidden input field in every page eg:

 

<input type="hidden" name="username" value="<?php echo($_POST['username']); ?>">

 

Then you will not need sessions.

-------------

This however is crude.

 

I believe your session problem is caused by the fact that your not passing your session_id to each subsequent page, (and if you were this code:

<?php
$_SESSION['username'] = $_POST['username'];
echo $_SESSION['username'];
?>

this would wipe it clean unless the page before had a username field as well. you would only need

echo $_SESSION['username'];

for ever page AFTER the page you enter your username.

------

To continue using sessions, Change the <Form> Tag of all question pages From:

 

<form method="post" action="q2.php">

to

<form method="post" action="q2.php?<?php echo(SID); ?>">

--

then in all question pages AFTER Question Page _ONE_, get rid of this line:

$_SESSION['username'] = $_POST['username'];

--------------

 

For more information on session usage see http://uk2 dot php dot net/function.session-start

 

 

hope this helps

Link to comment
https://forums.phpfreaks.com/topic/91505-display-the-username/#findComment-468721
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.