Jump to content

Recommended Posts

Here's my first page:

<?php

echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">';

echo '<html xmlns="http://www.w3.org/1999/xhtml">';

echo '<head>';

echo '<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />';

echo '<title>Comp 1920 - Lab 17</title>';

echo '</head>';

echo '<body>';

if(!($_COOKIE["firstname"])){

echo '<form action = "./page2.php" method = "post" name = "L17_form">';

echo "Welcome!  This seems to be your first visit! <br /><br />";

echo 'Please enter your first name: <input type = "text" name="firstn"><br /><br />';

echo 'Please enter your last name: <input type = "text" name="lastn"><br /><br />';

echo 'Red  <input type="radio" name="colour" value"red" checked="off"><br /><br />';

echo 'Blue  <input type="radio" name="colour" value"blue" checked="off"><br /><br />';

echo 'Yellow  <input type="radio" name="colour" value"yellow" checked="off"><br /><br />';

echo 'Green  <input type="radio" name="colour" value"green" checked="off"><br /><br />';

echo '<input type="submit" name="submit_btn" value="Submit">';

echo '</form>';

}

else {

echo $_COOKIE["firstname"];
}


echo '</body>';

echo '</html>';

?>

 

And here's my second page:

<?php

/* The following code determines if the username field has a value.  If it doesn't, it will kill the script.
*/

if (!($_POST["firstn"])){
die ("Please press Back and enter your first name.");

}

/* The following code determines if the password name field has a value/valid value.  If it doesn't, it will kill the script.
*/

if (!($_POST["lastn"])) {
die ("Please press Back and enter your last name.");
}

$firstname = $_POST["firstn"];
$lastname = $_POST["lastn"];
$color = $_POST["colour"];

if(!($_COOKIE["firstname"]))
{
setcookie("firstname",$_POST["first"],(time()+(24*60*60)),"","",0);
echo "The cookie has been set.";

}
else {
if($_POST["colour"] != "red" ||$_POST["colour"] != "green"|| $_POST["colour"] != "blue" || $_POST["colour"] != "yellow"){
	echo "Hello $firstname $lastname";
}

else{
	echo"<body bgcolor=\"$color\">Hello $firstname $lastname";
}

}



?>

 

So...When I visit the first page, I get this error message: "

Notice: Undefined index: firstname in [filename and path] on line 24"

The line number'll be different as I took out some comments required for the assignment (ie, name, etc).

 

So...I'm not sure what I'm doing wrong here...  Any help please?

 

Link to comment
https://forums.phpfreaks.com/topic/52447-what-am-i-doing-wrong-regarding-cookies/
Share on other sites

Actually, I think we sorta fixed that...

 

Now I don't know how to actually call the setcookie function...

 

This is what the script's suppose to do.  The first page sees if you've set a cookie before and if not it'll say that you're a first timer, and you must enter your first and last names.  Choice of colour is optional (it should affect the bg color later).  Second script verifies that a first and last name has been entered, then sees if a colour has been chosen as a background colour.  If it has been chosen, then it'll change the background to that colour and echo out "welcome back [fullname]." or something of the like.

 

code for page 1:

<?php

echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">';

echo '<html xmlns="http://www.w3.org/1999/xhtml">';

echo '<head>';

echo '<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />';

echo '<title>Comp 1920 - Lab 17</title>';

echo '</head>';

echo '<body>';




if (!(isset($_COOKIE["firstname"]))) {

echo '<form action = "./ithanhL17-2.php" method = "post" name = "L17_form">';

echo "Welcome!  This seems to be your first visit! <br /><br />";

echo 'Please enter your first name: <input type = "text" name="firstn"><br /><br />';

echo 'Please enter your last name: <input type = "text" name="lastn"><br /><br />';

echo 'Red  <input type="radio" name="colour" value"red" checked="checked"><br /><br />';

echo 'Blue  <input type="radio" name="colour" value"blue"><br /><br />';

echo 'Yellow  <input type="radio" name="colour" value"yellow"><br /><br />';

echo 'Green  <input type="radio" name="colour" value"green"><br /><br />';

echo '<input type="submit" name="submit_btn" value="Submit">';

echo '</form>';

}

else {

echo isset($_COOKIE["firstname"]);

//echo $_COOKIE["first"];
}





echo '</body>';

echo '</html>';




?>

 

code for page 2:

<?php


/* The following code determines if the username field has a value.  If it doesn't, it will kill the script.
*/

if (!($_POST["firstn"])){
die ("Please press Back and enter your first name.");

}

/* The following code determines if the password name field has a value/valid value.  If it doesn't, it will kill the script.
*/

if (!($_POST["lastn"])) {
die ("Please press Back and enter your last name.");
}

$firstname = $_POST["firstn"];
$lastname = $_POST["lastn"];
$color = $_POST["colour"];

if (!(isset($_COOKIE["firstname"]))) {
setcookie("firstname",$_POST["firstn"],(time()+(24*60*60)),"","",0);
echo "The cookie has been set.";

}
else {
if($_POST["colour"] != "red" ||$_POST["colour"] != "green"|| $_POST["colour"] != "blue" || $_POST["colour"] != "yellow"){
	echo "Hello $firstname $lastname";
}

else{
	echo"<body bgcolor=\"$color\">Hello $firstname $lastname";
}

}



?>

 

So when I type in my information in page 1, get it sent to page 2, the "cookie has been set" shows up.  So then I refresh and all I get is "1"  So...how would I call the setcookie function properly in page 1?

Ermm i might be wrong but jus double check that when you use the ; (semicolon)on your echo's it usually tells the script that das it das the end of the echo which is not correct. try and use addslashes or something.

otherwise you can just leave the html tags outsiede like so.

 

<?php some php code here ?>

<meta>  </meta>

any html code here

 

<?php some php code here ?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.