-4

When i first go to the page, without doing anything i receive this error below. I don't understand why because I'm checking to see if the submit button got clicked.

Notice: Undefined index: username in /Applications/XAMPP/xamppfiles/htdocs/kwame.php on line 3

<?

$user_name = $_POST['username'];
$submit = isset($_POST['submit']);


if($submit){

echo $user_name;

}

else {
echo 'leave';
}

?>


<html>
<head>
<title> My Php Exercises </title>
</head>

<body>
<FORM name='login' method='POST' action='kwame.php'>

<input type='text' name='username' >
<input type='submit' name='submit'>


</FORM>

</body>
</html>
Rizier123
  • 58,877
  • 16
  • 101
  • 156

3 Answers3

0

You don't open your php correctly. Use:

<?php

if(isset($_POST['submit'])){
$user_name = $_POST['username'];
echo $user_name;

}

else {
echo 'leave';
}

?>
Michelangelo
  • 5,888
  • 5
  • 31
  • 50
0

php array variable gives a warning message when the key data transport. You should check whether the variable is defined for this.

Determine if a variable is set http://php.net/manual/en/function.isset.php

<?php

if(isset($_POST['submit']) && isset($_POST['username'])){

   echo $_POST['username'];

 }else {
    echo 'leave';
 }

?>
Buse Gönen
  • 238
  • 1
  • 5
0

you should use

if(isset($_POST['username']))
    {$user_name = $_POST['username'];}
else
    {$user_name = "";}

instread 

Or you can simply moving your line int that position:

if($submit)
    {$user_name = $_POST['username'];
    ....
    }   
user3450036
  • 85
  • 1
  • 2
  • 10