this is the code i am working on (it is part of this tutorial http://www.homeandlearn.co.uk/php/php4p9.html )
this works fine
`
A BASIC HTML FORM
<?PHP
if (isset($_POST['Submit1'])) {
$username=$_POST['username'];
if($username=="ken"){
print("you the man");
}
else {
print("you are not supposed to be here");
}
}
else{
$username="";
}
?>
</head>
<body>
<FORM NAME ="form1" METHOD ="post" ACTION = "basicForm.php">
username: <INPUT TYPE = "TEXT" VALUE ="<?PHP print $username;?>"NAME="username">
<INPUT TYPE = "Submit" Name = "Submit1" VALUE = "Login">
</FORM>
</body>
</html>`
But this does not
<html>
<head>
<title>A BASIC HTML FORM</title>
<?PHP
if (isset($_POST['Submit1'])) {
$username=$_POST['username'];
$nickname=$_POST['nickname'];
if($username=='ken'and$nickname=='hawk'){
print("you the man");
}
else {
print("you are not supposed to be here");
}
}
else{
$username=""and$nickname="";
}
?>
</head>
<body>
<FORM NAME ="form1" METHOD ="post" ACTION = "testformken.php">
nickname: <input type="text" VALUE ="<?PHP print $nickname;?>" name="nickname" /><br />
username: <input type="text" VALUE ="<?PHP print $username;?>" name="username" />
<INPUT TYPE = "Submit" Name = "Submit1" VALUE = "Login">
</FORM>
</body>
</html>
I get this Notice: Undefined variable: nickname in C:\wamp\www\testformken.php on line 30 Call Stack #TimeMemoryFunctionLocation 10.1800367256{main}( )..\testformken.php:0 ” name=”nickname” />
I have messed with a few things and if i change
nickname: <input type="text" VALUE ="<?PHP print $nickname;?>" name="nickname" /><br />
to
nickname: <input type="text" VALUE ="<?PHPprint$nickname;?>" name="nickname" /><br />
I do not get a the undifined variable but it does not print the nickname either
if i change the value to
<?PHP print $username;?>
enter code here i do not get the undifined variable.
You’re simply always printing
$nicknameon your site but you only define it in the if-block (if (isset($_POST['Submit1'])) {) is entered.To avoid this, change your printing-part to:
Note that I’m using the ternary operator (a shorthand for if-else) here which might be new for you if you’re a beginner.
To avoid XSS-attacks you also shouldn’t output variables without escaping in real-life usage like Jack suggested (for a tutorial it’s ok to stay simple).