Log in

View Full Version : Yesterday is FINE, but today always UNDEFINED INDEX, HELP



smansakra
07-20-2011, 01:15 PM
<?php
if($_POST['myname'])
echo "My name is ".$_POST['myname'];
else echo $ret;

$ret = '<html>
<head>
<title>Try it</title>
</head>
<body>
<form action="'.$_SERVER['PHP_SELF'].'">
My Name: <input type="text" name="myname"/>
<input type="submit" name="submit" value="Show it"/>
</body>
</html>';
?>

I have code above, yesterday is fine, but today when i open it in my browser always say:

Notice: Undefined index: myname in C:\xampp\htdocs\try_it.php on line 2

Notice: Undefined variable: ret in C:\xampp\htdocs\try_it.php on line 4

CAN SOMEBODY TELL ME? thannks so much :confused:

bluewalrus
07-20-2011, 02:08 PM
You can't echo something that hasn't been set so we gotta swap the order. You want to check if that array has that value first, and if it does if it has a value. You also should set the method of your form to be sure it will be post.


<?php
$ret = '<html>
<head>
<title>Try it</title>
</head>
<body>
<form action="'.$_SERVER['PHP_SELF'].'" method="POST">
My Name: <input type="text" name="myname"/>
<input type="submit" name="submit" value="Show it"/>
</body>
</html>';
if(!empty($_POST['myname']))
echo "My name is ".$_POST['myname'];
else
echo $ret;
?>

smansakra
07-20-2011, 03:42 PM
UPS,,, THANKS so much, ,,, it works,,
<3