Reputation:
I am having a problem with a simple HTML form with PHP validation. It is a generic question so here are some stack overflow answers I already looked at:
HTML form with PHP - uses javascript
Tried: Html form with php validation and honeypot But it gave me: Server Error in '/' Application.
Here is my HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<form action ="process.php" method ="POST">
First Name: <input type="text" name="fName">
<input type="submit">
</form>
</body>
</html>
And PHP:
<?php
if($_SERVER["REQUEST_METHOD" =="POST"]) {
//collect value of input fields:
$fName=$_REQUEST['fName'];
echo $fName;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
</body>
</html>
The form submits and calls my process.php page, but it does not echo my fName value.
Upvotes: 1
Views: 213
Reputation: 3723
There are some basic problem in your code, Thomas.
It is interesting, when passing parameters with GET or POST to get them somewhere else, specify the enctype.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<form action ="process.php" method ="POST" enctype="multipart/form-data">
First Name: <input type="text" name="fName">
<input type="submit">
</form>
</body>
</html>
But this is not what is going to solve your problem. Just an advice.
Your problem is in the fact you are doing it wrong in your PHP code.
First, there is a syntax error here:
if($_SERVER["REQUEST_METHOD" =="POST"]) {
This line should read:
if($_SERVER["REQUEST_METHOD"] =="POST") {
As you may see, you misplaced the closing bracket.
Besides, since you're using POST, the correct place to get the post variables is $_POST, not $_REQUEST. Then, instead of
$fName=$_REQUEST['fName'];
it should be
$fName=$_POST['fName'];
Finally, the right test to be done is not "what type of method my request used?" as you did with
if($_SERVER["REQUEST_METHOD"] =="POST")
but "is the variable I want to get here really defined?", as in
if (isset($_POST["fName"))
because the lack of this test could generate an error when you try to assign a value that does not exist to a variable.
But then I have a question: Why do you think you need this in your PHP code?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
</body>
</html>
This is doing NOTHING in your PHP code. You're not even using this HTML structure to display your data. This reminds me of an essential rule in programming: Take out all you don't need, so you may focus on what really matters.
Upvotes: -1
Reputation: 34914
You can also do like this.
<?php
if(isset($_POST['fName'])) {
//collect value of input fields:
$fName=$_POST['fName'];
echo $fName;
}
?>
Upvotes: 1
Reputation: 22532
Typo over here
$_SERVER["REQUEST_METHOD" =="POST"]// wrong close of squre bracket
^^
It would be
$_SERVER["REQUEST_METHOD"] =="POST"
Read http://php.net/manual/en/reserved.variables.server.php
Upvotes: 1