Page 1 of 1

Error in php form

Posted: Thu Feb 07, 2013 9:00 am
by carlo2837
Hi, I am doing PHP Home & Learn Tutorial. I got to the lesson about forms. I keep getting this error:
PHP Notice: Undefined index: username in C:\inetpub\wwwroot\basicForm.php on line 7 .
This is the code :

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<title>A BASIC HTML FORM</title>
<?PHP
$username = $_POST['username'];
print($username);
?>


</head>
<body>
<FORM NAME ="form1" METHOD ="POST" ACTION = "basicForm.php">

<INPUT TYPE = "TEXT" VALUE="username" NAME="username">
<INPUT TYPE = "Submit" Name = "Submit1" VALUE = "Login">

</FORM>

</body>
</html>

It says to ignore the error message and click on submit or login in this case button.The problem is that the browser, IE9 just displays the error message, I cannot click the button because you cannot see it. if I don't fix this I will not be able to go ahead with the tutorial.Any idea? Thank you.

Re: Error in php form

Posted: Thu Feb 07, 2013 11:02 am
by seandisanti
you're getting the error because you're displaying a value that doesn't yet exist. no $_POST variables are posted when you first visit the page until you've submitted the form. It looks like you're handling it with the same page that's submitting, so you'd want to react differently depending on whether or not it's in response to a post...

Code: Select all

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<title>A BASIC HTML FORM</title>
</head>
<body>
<?PHP
if ($_POST){
	$username = $_POST['username'];
	print($username);
} else {
echo '<FORM NAME ="form1" METHOD ="POST" ACTION = "trystuff.php">';
echo '<INPUT TYPE = "TEXT" VALUE="username" NAME="username">';
echo '<INPUT TYPE = "Submit" Name = "Submit1" VALUE = "Login">';
echo '</FORM></body></html>';
}
i changed the name of the form handler because i do my sample testing in a file called 'trystuff.php' so you'd want to change that to the name of your script.