Welcome! Log In Create A New Profile

Advanced

Topic 5B

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
Anonymous User
Re: Topic 5B
May 11, 2006 03:33PM
Angus - may help if you read the other posts since it was answered already with libby. smile

You send it with a GET, then request it with a $_POST????

Re: Topic 5B
May 11, 2006 04:07PM
The way I see this code is:

if there is no cookie, which is the case then the else statements are executed, there is a GET in this statement already.

I did read all the other posts but I am not winning
Anonymous User
Re: Topic 5B
May 11, 2006 04:44PM
Point is you send the form info with a GET
echo "<form method=GET action='setcookie.php'>";

Then you try to extract it with a $_POST. Can't be done, so there exists no information with which to SET the cookie.

echo "<form method=POST action='setcookie.php'>";
Re: Topic 5B
May 11, 2006 07:13PM
Phew. Topic completed.

*trying to hurry*
Re: Topic 5B
May 12, 2006 02:00PM
OK Thank you BUT
I am still not on the buss

I get the following output / error
mmmm
Warning: Cannot add header information - headers already sent by (output started at C:\FoxServ\www\StudentPHP\setcookie.php:3) in C:\FoxServ\www\StudentPHP\setcookie.php on line 26

the color does not seem to be getting through as only the mmmm are displayed

my code is

setcookie

<?php

echo "<form method=POST action='setcookie.php'>";

$color=$_POST['color'];

echo "mmmm $color";

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

}

//INSERT CODE HERE TO SET THE COOKIE
setcookie("background", $bgcolor, time()+3600);
echo "<html>";
echo "<head><title>Selected Background Color</title></head>";
echo "<body bgcolor='$bgcolor'>";
echo "</body>";
echo "</html>";
?>

cookie

<!--INSERT CODE HERE-->
<?php
echo "<form method=GET action='setcookie.php'>";

$background = $_COOKIE[background];

if ($background)
{
echo "<html>";
echo "<head><title>Selected Background Color</title></head>";
echo "<body bgcolor='$background' vlink='white'>";
echo "<a href='delete_cookie.php'>Delete Cookie</a>";
echo "</body>";
echo "</html>";
}
else
{
echo "<html>";
echo "<head><title>Select a Background Color</title></head>";
echo "<body>";
echo "<form method=POST action='setcookie.php'>";
echo "Select a background color";
echo "<br>";
echo "<input name='color' TYPE='radio' VALUE='Yellow'> Yellow";
echo "<br>";
echo "<input name='color' TYPE='radio' VALUE='Blue'> Blue";
echo "<br>";
echo "<input name='color' TYPE='radio' VALUE='Silver'> Silver";
echo "<br>";
echo "<input name='color' TYPE='radio' VALUE='Beige'> Beige";
echo "<br>";
echo "<br>";
echo "<input type='submit'>";
echo "</form>";
echo "</body>";
echo "</html>";
}
?>
avatar
Mac
Re: Topic 5B
May 12, 2006 03:47PM
When you set cookies, make sure there is no blank lines on top of the page where the code its that sets the cookie.

so if this line is the
--------------------------------top of page
<?php
setcookie ("background", $bgcolor, time()+3600);


Look for space after the <?php tag as well


Then change the GET to POST!
Re: Topic 5B
May 15, 2006 10:08AM

OK still not complete

I now get the background color change and the mmm$color works correctly so I removed this code.

I still get the same arror ie
Warning: Cannot add header information - headers already sent by (output started at C:\FoxServ\www\StudentPHP\setcookie.php:2) in C:\FoxServ\www\StudentPHP\setcookie.php on line 22

I commented out the post but this had no effect
I removed and inserted the white space to test the effect which I did not find any problem with white space at all.

the cookie is still not being created so I cannot test the first part of the if statement


cookie
<!--INSERT CODE HERE-->
<?php
echo "<form method=POST action='setcookie.php'>";
$background = $_COOKIE[background];

if ($background)
{
echo "<html>";
echo "<head><title>Selected Background Color</title></head>";
echo "<body bgcolor='$background' vlink='white'>";
echo "<a href='delete_cookie.php'>Delete Cookie</a>";
echo "</body>";
echo "</html>";
}
else
{
echo "<html>";
echo "<head><title>Select a Background Color</title></head>";
echo "<body>";
echo "<form method=GET action='setcookie.php'>";
echo "Select a background color";
echo "<br>";
echo "<input name='color' TYPE='radio' VALUE='Yellow'> Yellow";
echo "<br>";
echo "<input name='color' TYPE='radio' VALUE='Blue'> Blue";
echo "<br>";
echo "<input name='color' TYPE='radio' VALUE='Silver'> Silver";
echo "<br>";
echo "<input name='color' TYPE='radio' VALUE='Beige'> Beige";
echo "<br>";
echo "<br>";
echo "<input type='submit'>";
echo "</form>";
echo "</body>";
echo "</html>";
}
?>




setcookie
<?php
echo "<form method=POST action='setcookie.php'>";
$bgcolor=$_POST['color'];

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

}

//INSERT CODE HERE TO SET THE COOKIE
setcookie("background", $bgcolor, time()+3600);
echo "<html>";
echo "<head><title>Selected Background Color</title></head>";
echo "<body bgcolor='$bgcolor'>";
echo "</body>";
echo "</html>";
?>
Re: Topic 5B
May 15, 2006 12:47PM
OK I finally got it working correctly

avatar
Mac
Re: Topic 5B
May 15, 2006 06:18PM
Post what you have learned winking smiley
Re: Topic 5B
May 16, 2006 08:25AM
Well I got one line of code wrong in setcookie
I had
$color=$_POST['color']; should be
$bgcolor=$_POST['color'];

I did not need //echo "<form method=POST action='setcookie.php'>";

In cookie
I found the the GET works just fine I did change it to POST and this also worked

I played around with white space and found this to be a non issue as it is with most other languages that I have used. White space helps me to understand what is going on. So I need it.

I am a little confused by the fact that certain commands are placed first ie on line 1 eg a POST, but they seem to be executed last. I am more comfortable with a system that does command 1 and then 2 and then 3 etc.
Also in cookie there are in fact 2 POSTS one on line 1 or 2 and then on about line 20 (here i have a working GET) so this feels to me like I am sending the information twice. It also feels like the first POST should be the last line of code as the passing of data would be the last thing to happen before calling the next program.

A question:
can one do all of this in one program? We seem to be passing data from program to program in every task that we do, I do not always see why, it seems a bit odd?


my code is

cookie
<!--INSERT CODE HERE-->
<?php
echo "<form method=POST action='setcookie.php'>";
$background = $_COOKIE[background];

if ($background)
{
echo "<html>";
echo "<head><title>Selected Background Color</title></head>";
echo "<body bgcolor='$background' vlink='white'>";
echo "<a href='delete_cookie.php'>Delete Cookie</a>";
echo "</body>";
echo "</html>";
}
else
{
echo "<html>";
echo "<head><title>Select a Background Color</title></head>";
echo "<body>";
echo "<form method=GET action='setcookie.php'>";
echo "Select a background color";
echo "<br>";
echo "<input name='color' TYPE='radio' VALUE='Yellow'> Yellow";
echo "<br>";
echo "<input name='color' TYPE='radio' VALUE='Blue'> Blue";
echo "<br>";
echo "<input name='color' TYPE='radio' VALUE='Silver'> Silver";
echo "<br>";
echo "<input name='color' TYPE='radio' VALUE='Beige'> Beige";
echo "<br>";
echo "<br>";
echo "<input type='submit'>";
echo "</form>";
echo "</body>";
echo "</html>";
}
?>



setcookie

<?php
//echo "<form method=POST action='setcookie.php'>"; This is not needed at all
$bgcolor=$_POST['color']; // I got this wrong it was $color=$_POST['color']

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

}

//INSERT CODE HERE TO SET THE COOKIE
setcookie("background", $bgcolor, time()+3600);
echo "<html>";
echo "<head><title>Selected Background Color</title></head>";
echo "<body bgcolor='$bgcolor'>";
echo "</body>";
echo "</html>";
?>
Sorry, you do not have permission to post/reply in this forum.