Saturday 5 January 2019

html - $_POST undefined index PHP error



My html code which receives the string:






title




Suggestion:








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?


Answer



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






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:







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.


No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...