-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?

7
  • You are actually posting the form right? Commented Oct 13, 2014 at 12:26
  • 1
    in your form attribute please use method="post" instead of action="post". Commented Oct 13, 2014 at 12:26
  • It means no value was passed in. PHP notices can generally be ignored unless you're having other problems. Commented Oct 13, 2014 at 12:26
  • possible duplicate of Undefined index with $_POST Commented Oct 13, 2014 at 12:28
  • @hardiksolanki Yea, it was a typo. Probably result of not having sleep last night. Thank you! :D Commented Oct 13, 2014 at 12:33

6 Answers 6

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.
Sign up to request clarification or add additional context in comments.

Comments

1

use isset

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

} 

Comments

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";
   }
}
?>

2 Comments

No matter what name I enter now, it always says "someone".
try this i have changed
0

Try following

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

Comments

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";
            }
        }
        ?>

Comments

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";
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.