Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

Error in php form

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

Error in php form

Postby carlo2837 » Thu Feb 07, 2013 9:00 am

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.
carlo2837
New php-forum User
New php-forum User
 
Posts: 7
Joined: Fri Jan 25, 2013 2:40 am

Re: Error in php form

Postby seandisanti » Thu Feb 07, 2013 11:02 am

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.
seandisanti
php-forum Fan User
php-forum Fan User
 
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm


Return to PHP coding => General

Who is online

Users browsing this forum: landi and 2 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.