Forum : Whats the error in code?
Brief description  about Online courses   join in Online courses
View  's Profile

Whats the error in code?

hi...

When i run below code, getting an notice undefined index username and colorchoice, whats the mistake in that code?
also im not getting 'PHP_SELF'.. why we hav e to use tat?

cookie1.php
<?php



$user = $_POST['username'];

$color = $_POST['colorchoice'];

$self = $_SERVER['PHP_SELF'];



if( ( $user != null ) and ( $color != null ) )

{

setcookie( "firstname", $user , time() 86400 ); // 24 hours

setcookie( "fontcolor", $color, time() 86400 );

header( "Location:cookie2.php" );

exit();

}

?>
cookie2.php
<?php

if (isset($_COOKIE['firstname']))

{

$user = $_COOKIE['firstname'];

$color= $_COOKIE['fontcolor'];

}

?>



Asked by | Jun 22, 2010 |  Reply now
Replies (2)
View  's Profile
Thnak you...
Jul 19, 2010
View teacher siliconindia 's Profile
Hi Uma,

The notices which you are getting is because you are trying to access an undefined variable.

In your code:

$user = $_POST['username'];

You are storing a post value in to $user variable directly.

If you are storing a post value in to a variable, check for the condition as below.

if(isset($_POST['username']) && !empty($_POST['username'])) {
$user = $_POST['username'];
}

PHP self returns the URL for the page in the browser.

Please do go through the below link:

http://www.html-form-guide.com/php-form/php-form-action-self.html

Thanks
Jul 19, 2010