Welcome! Log In Create A New Profile

Advanced

Topic 5C

Posted by Anonymous User 
Announcements Last Post
Announcement SoC Curricula 09/30/2017 01:08PM
Announcement Demarcation or scoping of examinations and assessment 02/13/2017 07:59AM
Announcement School of Computing Short Learning Programmes 11/24/2014 08:37AM
Announcement Unisa contact information 07/28/2011 01:28PM
Re: Topic 5C
May 23, 2006 11:51AM
ok - session.php :

<?php
session_start();
$bgcolor=$_POST;
$_SESSION[bgcolor]=$bgcolor;

echo $_SESSION['bgcolor'];
echo"<br><br>";

if (isset ($_SESSION[bgcolor])) {
switch ($bgcolor) {
case "Yellow":
$bgcolor="#FFCC00";
break;
case "Blue":
$bgcolor="#4682B4";
break;
case "Silver":
$bgcolor="#C0C0C0";
break;
case "Beige":
$bgcolor="#FFE4C4";
break;
default:
$bgcolor = "white";
break;
} // end switch
}

if (empty ($bgcolor)) {
$bgcolor = "white";
}
?>
<html>
<head><title>Session Example</title></head>

<body bgcolor='<?php echo $bgcolor; ?>'>

<form method=POST action='session.php?<?php echo session_id(); ?>'>
Select a background color
<br>
<input name='color' TYPE='radio' VALUE='Yellow'> Yellow
<br>
<input name='color' TYPE='radio' VALUE='Blue'> Blue
<br>
<input name='color' TYPE='radio' VALUE='Silver'> Silver
<br>
<input name='color' TYPE='radio' VALUE='Beige'> Beige
<br>
<br>
<input type='submit' value='Change background color'>
</form>
<a href='session.php?<?php echo session_id(); ?>'>Refresh page, Dont change background color</a>
<br><br>
<a href='session2.php?<?php echo session_id(); ?>'>To other page</a>


</body>
</html>

--------------------------------
now, session2.php:

<?php
session_start();
?>
<html>
<head><title>Session Example</title></head>

<body bgcolor='<?php echo $bgcolor; ?>'>

What colour is my background?
<br>
<br>
<?php
echo $_SESSION['bgcolor'];
echo "<br><br>";
?>
<br>
<a href='session.php?<?php echo session_id(); ?>'>Back to form page</a>

</body>
</html>
--------------------------------

Heeelp me please!
avatar
Mac
Re: Topic 5C
May 23, 2006 12:24PM
Your session.php code works for me? The color changes 100% when I run it in locahost??


BTW - try to code consistently

$bgcolor=$_POST;
$_SESSION[bgcolor]=$bgcolor;
echo $_SESSION['bgcolor'];

you're missing ''s which may cause you problems later on. If you don't force the syntax now, you'll do it elsewhere and then sit for days trying to find errors.

Re: Topic 5C
May 23, 2006 12:34PM
Thank you for pointing that out... i didn't even notice (always happens, huh, til you're stuck for ages missing a 'winking smiley.

Yes, my colour changes, but the colour change doesnt stay there when i refresh or go to session2.php - the session id stays, tho.

Let me try to upload it onto a server and see if it works..
avatar
Mac
Re: Topic 5C
May 23, 2006 01:01PM
Oh sorry - read it too fast. Will check it.
avatar
Mac
Re: Topic 5C
May 23, 2006 01:39PM
Same problem with my localhost.

The following code from session.php works well on our server:

<?php
@extract($_POST);
session_start();
session_register("bgcolor"winking smiley;
if (isset($color)) {

switch ($color) {
case "Yellow":
$bgcolor="#FFCC00";
break;
case "Blue":
$bgcolor="#4682B4";
break;
case "Silver":
$bgcolor="#C0C0C0";
break;
case "Beige":
$bgcolor="#FFE4C4";
break;
default:
$bgcolor = "white";
break;
} // end switch
}

if (empty ($bgcolor)) {
$bgcolor = "white";
}
?>
<html>
<head><title>Session Example</title></head>

<body bgcolor='<?php echo $bgcolor; ?>'>
<?php echo "color=$color bgcolor=$bgcolor"; ?>
<form method=POST action='session.php?<?php echo session_id(); ?>'>
Select a background color
<br>
<input name='color' TYPE='radio' VALUE='Yellow'> Yellow
<br>
<input name='color' TYPE='radio' VALUE='Blue'> Blue
<br>
<input name='color' TYPE='radio' VALUE='Silver'> Silver
<br>
<input name='color' TYPE='radio' VALUE='Beige'> Beige
<br>
<br>
<input type='submit' value='Change background color' name='submit'>
</form>
<a href='session.php?<?php echo session_id(); ?>'>Refresh page, Dont change background color</a>
<br><br>
<a href='session2.php?<?php echo session_id(); ?>'>To other page</a>


</body>
</html>
Re: Topic 5C
May 23, 2006 03:17PM
Mac - that didn't work for me either! BUT i did get a bit of help from a friend and it works now! *huge sigh of relief*

Right - now to focus on the project 110%
avatar
Mac
Re: Topic 5C
May 23, 2006 03:35PM
Maybe share what you did to get it working on localhost. I don't work on localhost, but others may.
Re: Topic 5C
May 23, 2006 03:41PM
my friend said to change instances of <body bgcolor='<?php echo $bgcolor; ?>'> to <body bgcolor='<?php echo $_SESSION['bgcolor']; ?>'> ... and basically to change all plain $bgcolor to $_SESSION['bgcolor']

and now it works! smiling smiley I dont think i've ever in my life been this happy to see a friggin colour background!
avatar
Mac
Re: Topic 5C
May 24, 2006 08:36AM
This is of course related to php5 in that you need to do some extraction from a form or link or session on the new page before using it.

But it seems that you are in fact hard-coding the bgcolor in $_SESSION then using it, which is not the same as extracting it from the session....
Re: Topic 5C
May 25, 2006 04:35PM
Completed - No Worries
Sorry, you do not have permission to post/reply in this forum.