-2

My html code which receives the string:

<!DOCTYPE html>
<head>
    <title>title</title>
</head>
<body>
    <font face="Segoe UI" siz ="3">
    <form action="process.php" action="post">
    Suggestion: <input type="text" name="sgst" autocomplete="off">
    <input type="submit">
</form>
</font>
</body>
</html>

My PHP which processes it: (process.php)

<?php
    if ($_POST["sgst"]=="john") {
        echo "john";
    } else {
        echo "someone";
    }
?>

I am getting the error

Notice: Undefined index: sgst in C:\xampp\htdocs\test\process.php on line 2

Can anyone tell me what is causing the error?

Anthony
  • 36,459
  • 25
  • 97
  • 163
blah
  • 103
  • 3
  • 12
  • You are actually posting the form right? – Jonathon Oct 13 '14 at 12:26
  • 1
    in your form attribute please use method="post" instead of action="post". – Hardik Solanki Oct 13 '14 at 12:26
  • It means no value was passed in. PHP notices can generally be ignored unless you're having other problems. – Anthony Oct 13 '14 at 12:26
  • possible duplicate of [Undefined index with $\_POST](http://stackoverflow.com/questions/10809937/undefined-index-with-post) – andrew Oct 13 '14 at 12:28
  • @hardiksolanki Yea, it was a typo. Probably result of not having sleep last night. Thank you! :D – blah Oct 13 '14 at 12:33
  • @andrew oh hey that's one of my first questions, but yea hardiksolanki is correct – Xander Luciano Aug 10 '16 at 19:36
  • Possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – Kara Aug 10 '16 at 22:02

6 Answers6

1

You're re-using the action attribute on the form element:

<form action="process.php" action="post">

I'm surprised the form is even posting at all given that. Though given the symptoms it seems to at least be enough to confuse the POST. I think you meant to use the method attribute:

<form action="process.php" method="post">

A couple other notes:

  • You'll want to make use of isset() in your server-side code. The code shouldn't assume that all values will be posted, since the server can't control what a client sends it. Best to explicitly validate the data coming in before using it.
  • font elements are pretty dated, and if they're not officially deprecated they should be :) CSS styling would be the way to go for client-side UI styles.
David
  • 208,112
  • 36
  • 198
  • 279
1

use isset

if(isset($_POST["sgst"]=="john")){

} 
andrew
  • 9,313
  • 7
  • 30
  • 61
0

it shows undefined because at first the value is not initialized

use isset() inorder to check for value declared or not then it wont show error...

try this i have changed

<?PHP 
if (isset($_POST["sgst"])
{
   if ($_POST["sgst"]=="john") {
    echo "john";
   } else {
    echo "someone";
   }
}
?>
Ronser
  • 1,855
  • 6
  • 20
  • 38
0

Try following

use Method attribute like method="post"in form tag instead of action="post"

bhushya
  • 1,317
  • 1
  • 19
  • 33
0
<!DOCTYPE html>
<head>
    <title>title</title>
</head>
<body>
    <font face="Segoe UI" siz ="3">
    <form action="process.php" method="post">
    Suggestion: <input type="text" name="sgst" autocomplete="off">
    <input type="submit">
</form>
</font>
</body>
</html>    


 if (isset($_POST["sgst"]) && $_POST["sgst"]=="john") {
            echo "john";
        } else {
            echo "someone";
        }
Manish Jesani
  • 1,339
  • 5
  • 20
  • 36
0

You are missing method attribute.

Using two forms- Remove first form.

Try this,

<form action="process.php" method="post">

 <?php
        if(!empty($_POST)){
        if ($_POST["sgst"]=="john") {
                echo "john";
            } else {
                echo "someone";
            }
        }
        ?>
Vinod VT
  • 6,946
  • 11
  • 51
  • 75