blah blah - 2 months ago 5x
HTML Question

$_POST undefined index PHP error

My html code which receives the string:

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

My PHP which processes it: (process.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?


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.