View Full Version : PHP problems


putnamehere
10-09-2005, 02:21 PM
Hey, i am creating a script that ask a user ther name and ther favorit color. it then echo the users name in ther favorit color, and it also saves a cookie for next time.

Heres the code that is used for them to submit ther data

<?php
$user = $_POST['user'];
$color = $_POST['color'];
$self = $_SERVER['PHP_SELF'];

if ( ( $user != null ) and ( $color != null ) )
{
setcookie ( "firstname", $user, time()+36000 );

setcookie ( "fontcolor", $color, time()+36000 );
header ("Location:getcookie.php" );
exit();
}
?>

and jsut below that php is this code, ( it's all one doc )

<html><head><title>ye<title></head><body>

<form action="<?php echo ( $self ); ?>" method="post">
Please enter your name
<input type="text" name="user"> <br><br>
Please select your color
<input type="radio" name="color" value ="#FF0000"> Red
<input type="radio" name="color" value ="#00FF00"> Green
<input type="radio" name="color" value ="#0000FF"> Blue
<br><br>
<input type="submit" value="submit">
</form>
</body>
</html>


The code in getcookie.php works fine the error message i am getting is,


Notice: Undefined index: user in C:\Abyss Web Server\htdocs\user.php on line 4

Notice: Undefined index: color in C:\Abyss Web Server\htdocs\user.php on line 5

Lines 4 & 5;


$user = $_POST['user'];
$color = $_POST['color'];


Witch is understable, as thers been no data submited so how do i get round this?
ps ( i want it as one file )

Douglas
10-09-2005, 04:06 PM
here i fixed your code:


<?php
$user=$_POST['user'];
$color=$_POST['color'];
$self=$_SERVER['PHP_SELF'];

if ($user != null && $color != null) {
setcookie("firstname", $user, time()+36000, "/", ".YOURSITE.com");
setcookie("fontcolor", $color, time()+36000, "/", ".YOURSITE.com");
header("Location: getcookie.php");
exit;
}
?>
<html>

<head>
<title>ye<title>
</head>

<body>

<?php
if (isset($_COOKIE['fontcolor'])) {
if (isset($_COOKIE['firstname'])) {
echo "Current Name/Color: <b>".$_COOKIE['firstname']."/".$_COOKIE['fontcolor']."</b><br />";
}
else {

}
}
else {

}
?>
<form action="<?php echo $self; ?>" method="post">
Please enter your name&nbsp;
<input type="text" name="user" /><br /><br />
Please select your color&nbsp;
<input type="radio" name="color" value ="#FF0000" />&nbsp;Red&nbsp;
<input type="radio" name="color" value ="#00FF00" />&nbsp;Green&nbsp;
<input type="radio" name="color" value ="#0000FF" />&nbsp;Blue&nbsp;
<br /><br />
<input type="submit" value="Submit" />
</form>

</body>

</html>

putnamehere
10-09-2005, 04:17 PM
I'm getting the same error with no form.

Douglas
10-09-2005, 04:24 PM
ok try this:


<?
$user=$_POST['user'];
$color=$_POST['color'];
$self=$_SERVER['PHP_SELF'];
if ($user != "" && $color != "") {
header("Content-Type: text/html; charset=ISO-8859-1");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
setcookie("firstname", $user, time()+36000, "/", ".YOURSITE.com");
setcookie("fontcolor", $color, time()+36000, "/", ".YOURSITE.com");
header("Location: getcookie.php");
exit;
}
else {
header("Content-Type: text/html; charset=ISO-8859-1");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
}
?>
<html>

<head>
<title>ye<title>
</head>

<body>

<?
if (isset($_COOKIE['fontcolor'])) {
if (isset($_COOKIE['firstname'])) {
echo "Current Name/Color: <b>".$_COOKIE['firstname']."/".$_COOKIE['fontcolor']."</b><br />";
}
else {}}else{}
?>
<form action="<?=$self?>" method="post">
Please enter your name&nbsp;
<input type="text" name="user" /><br /><br />
Please select your color&nbsp;
<input type="radio" name="color" value ="#FF0000" />&nbsp;Red&nbsp;
<input type="radio" name="color" value ="#00FF00" />&nbsp;Green&nbsp;
<input type="radio" name="color" value ="#0000FF" />&nbsp;Blue&nbsp;
<br /><br />
<input type="submit" value="Submit" />
</form>

</body>

</html>

putnamehere
10-09-2005, 04:26 PM
nope

Notice: Undefined index: user in C:\Abyss Web Server\htdocs\cook.php on line 2

Notice: Undefined index: color in C:\Abyss Web Server\htdocs\cook.php on line 3

Warning: Cannot modify header information - headers already sent by (output started at C:\Abyss Web Server\htdocs\cook.php:2) in C:\Abyss Web Server\htdocs\cook.php on line 16

Warning: Cannot modify header information - headers already sent by (output started at C:\Abyss Web Server\htdocs\cook.php:2) in C:\Abyss Web Server\htdocs\cook.php on line 17

Warning: Cannot modify header information - headers already sent by (output started at C:\Abyss Web Server\htdocs\cook.php:2) in C:\Abyss Web Server\htdocs\cook.php on line 18

Warning: Cannot modify header information - headers already sent by (output started at C:\Abyss Web Server\htdocs\cook.php:2) in C:\Abyss Web Server\htdocs\cook.php on line 19

putnamehere
10-09-2005, 04:27 PM
when i go to view source heres wat i get,
looks like ther / were ther aitn ment to be /'s

<br />
<b>Notice</b>: Undefined index: user in <b>C:\Abyss Web Server\htdocs\cook.php</b> on line <b>2</b><br />
<br />
<b>Notice</b>: Undefined index: color in <b>C:\Abyss Web Server\htdocs\cook.php</b> on line <b>3</b><br />
<br />
<b>Warning</b>: Cannot modify header information - headers already sent by (output started at C:\Abyss Web Server\htdocs\cook.php:2) in <b>C:\Abyss Web Server\htdocs\cook.php</b> on line <b>16</b><br />
<br />
<b>Warning</b>: Cannot modify header information - headers already sent by (output started at C:\Abyss Web Server\htdocs\cook.php:2) in <b>C:\Abyss Web Server\htdocs\cook.php</b> on line <b>17</b><br />
<br />
<b>Warning</b>: Cannot modify header information - headers already sent by (output started at C:\Abyss Web Server\htdocs\cook.php:2) in <b>C:\Abyss Web Server\htdocs\cook.php</b> on line <b>18</b><br />
<br />
<b>Warning</b>: Cannot modify header information - headers already sent by (output started at C:\Abyss Web Server\htdocs\cook.php:2) in <b>C:\Abyss Web Server\htdocs\cook.php</b> on line <b>19</b><br />

<html>

<head>
<title>ye<title>
</head>

<body>


<form action="/cook.php" method="post">
Please enter your name&nbsp;
<input type="text" name="user" /><br /><br />
Please select your color&nbsp;
<input type="radio" name="color" value ="#FF0000" />&nbsp;Red&nbsp;
<input type="radio" name="color" value ="#00FF00" />&nbsp;Green&nbsp;
<input type="radio" name="color" value ="#0000FF" />&nbsp;Blue&nbsp;
<br /><br />
<input type="submit" value="Submit" />
</form>

</body>

</html>


ps!!!!
cook.php is fine, and i no that my other code may link to a different file but that is not the porb

Douglas
10-09-2005, 04:32 PM
i can fix the header errors:


<?
$self=$_SERVER['PHP_SELF'];
if ($_POST['user'] != "" && $_POST['color'] != "") {
setcookie("firstname", $user, time()+36000, "/", ".YOURSITE.com");
setcookie("fontcolor", $color, time()+36000, "/", ".YOURSITE.com");
header("Location: getcookie.php");
exit;
}
else {

}
?>
<html>

<head>
<title>ye<title>
</head>

<body>

<?
if (isset($_COOKIE['fontcolor'])) {
if (isset($_COOKIE['firstname'])) {
echo "Current Name/Color: <b>".$_COOKIE['firstname']."/".$_COOKIE['fontcolor']."</b><br />";
}
else {}}else{}
?>
<form action="<?=$self?>" method="post">
Please enter your name&nbsp;
<input type="text" name="user" /><br /><br />
Please select your color&nbsp;
<input type="radio" name="color" value ="#FF0000" />&nbsp;Red&nbsp;
<input type="radio" name="color" value ="#00FF00" />&nbsp;Green&nbsp;
<input type="radio" name="color" value ="#0000FF" />&nbsp;Blue&nbsp;
<br /><br />
<input type="submit" value="Submit" />
</form>

</body>

</html>

try that ... and by the way your pm inbox is full and i cant reply to your message

putnamehere
10-09-2005, 04:43 PM
now saying,
Notice: Undefined index: user in C:\Abyss Web Server\htdocs\cook.php on line 3
and line 3 is,
if ($_POST['user'] != "" && $_POST['color'] != "") {
ps, inbox ready

Douglas
10-09-2005, 04:46 PM
o hahahaha this isnt online is it lol, that is just a notice lol... whenever you upload this on the internet and to a site it will go away... i had this a lot when i tested on my comp lol....

putnamehere
10-09-2005, 04:52 PM
oh ok, thanks!

Douglas
10-09-2005, 04:53 PM
no problem lol :D