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.
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
You can also do like this.
<?php
if(isset($_POST['fName'])) {
//collect value of input fields:
$fName=$_POST['fName'];
echo $fName;
}
?>
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.
Related
I have attached the code samples of my html and php files along with the error I'm getting whenever I try to
run the code to get values entered in my html form to my php document.
I'm using the XAMPP server.
I have done this exactly as mentioned in the site mentioned below:
https://www.w3schools.com/php/php_forms.asp
I would really appreciate it of someone could help me out with this.
HTML SAMPLE:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
<form action="welcome.php" method="POST">
Name : <input type="text" name="name"> <br><br>
E-mail: <input type="text" name="email"><br> <br>
<input type="submit">
</form>
</body>
</html>
PHP SAMPLE:
<!DOCTYPE html>
<html>
<body>
Hello <?php echo $_POST['name']; ?>
Your email address is <?php echo $_POST['email']; ?>
</body>
</html>
ERROR in my browser:
Warning: Undefined array key "name" in
C:\xampp\htdocs\BasicIntro\welcome.php on line 5
Your email address is
Warning: Undefined array key "email" in
C:\xampp\htdocs\BasicIntro\welcome.php on line 6
Both pages are working. no mistakes. I dont know why it does not work for you.
Try reinstalling XAMPP.
I have a problem with $_POST function.
I have this simple code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<?php
if (isset($_POST["password"])){
echo $_POST["password"];
};
?>
<form action="" method="POST">
Password: <br>
<input type="password" name="password"><br><br>
<input type="submit" name="submit" value="SUBMIT">
</form>
</body>
</html>
I run this code on PhpFiddle and it work as it should. However, when I running this on my laptop I get this error:
Notice: Undefined index: password
Thanks for your help. I am running XAMPP control pannel v3.22. IDE:PhpStorm. Php version: 7. OS: Windows 10.
I even asked this question here:
$_POST not working
You are missing to place a value inside the action attribute in your form. Replace this code:
<form action="" method="POST">
with this code:
<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="POST">
The previous php code inside the action attribute informs the form that the input should be sent to the same page. The htmlspecialchars() function is used to prevent XSS attacks on your site.
EDITED:
You also forgot to place a closing parethesis. Replace the following code:
if (isset($_POST["password"]){
with
if (isset($_POST["password"])){
Let me know if that works for you.
I've just started to learn PHP. I found the $_POST variable is not working and posted the same at the below link
$_POST[] not working in php
and as per the advise i installed XAMPP. But still the proble of $_POST variable is not solved.
Now i've a doubt whether i need to configure any global variable to make $_POST work. I'm totally lost on this and dont know how to proceed.
Any help on this is verryy much appreciated.
Below is the html code - report.html
<html>
<title></title
<head></head>
<body>
<form action="report.php" method="POST" >
<label for="firstname">First name:</label>
<input type="text" id="firstname" name="firstname" value="TestOnly" /><br />
</form>
</body>
</html>
and below is the php code - report.php
<html>
<head>
</head>
<body>
<?php
print( $_POST['firstname']);
?>
</body>
</html>
Below is the view that i got from chrome network data
Thanks.
See, try this.
Source of index.htm File:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>POST</title>
</head>
<body>
<form method="post" action="post.php">
<input type="text" name="name" />
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>
Source of post.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<title>Output</title>
</head>
<body>
<?php
if(isset($_POST["name"]))
echo "You have posted " . $_POST["name"];
else
echo "Nothing has been set!";
?>
</body>
</html>
Now try saving these two files under same directory. Enter something in the text box and click on submit. Let us know what you have got.
A few things to check:
Your script will have no data in the POST array unless a form has been submitted (Posted) to that script.
If you have a script and it's submitting properly, make sure the form has method="Post" in its opening tag. If your form submits and in the result script you see a bunch of variables in the address bar separated by ampersands (&) then it's using GET instead of POST. Make sure you have the above statement method="Post" in the form tag.
If you've submitted the script and you still can't see anything, try putting print_r($_POST) or var_dump($_POST) at the top of the target script to see a dump of the $_POST array. If this gives you nothing, try var_dump($_REQUEST) and see if your form data shows up there.
Have you ever tried if your php is correct?
<?php
phpinfo();
?>
or
<?php
print_r($_POST);
?>
what is your code? in default $_POST settings is active in every server unless in server ignore posted data for securitical reasons. i think you mistake is in your code ,don't change XAMP settings
not change the settings of php
either you use get method in html file and use post in action file in both you should use post.
This is my html form and below this i included my php text.
But I am not getting correct output,i don't no where the problem is ?
I also included the output ,please suggest me what shuld i do?
<html>
<head>
<title>
Entering data into text
</title>
</head>
<body>
<h1>
Entering data into text
</h1>
<form action="text.php" method="post">
What is ur name ?
<input type="text" name="data" />
<input type="submit" value="send" />
</form>
</body>
</html>
This is my php text:
<html>
<head>
<title>
Reading data from textfields
</title>
</head>
<body>
<h1>
reading data from text field
</h1>
Thanks for answering,
<?php echo $_POST["data"];?>
</body>
</html>
Output:
reading data from text field
Thanks for answering,
problem is that ,data send is not included after response of sever
please help me as fast as possible
I can only speak for my own experience, but this works on my server. I'd suggest, then, that one of the following is true:
Your server isn't set up to handle php (though this would surprise me), also, as #gAMBOOKa noted (in the comments), if your server's not set up to handle php the script wouldn't output anything other than the raw text-contents of the php script, literally "<?php echo $_POST["data"];?>".
You're trying to access the pages through the filesystem (file:///path/to/file.html), rather than through your server (http://localhost/file.html).
If '2' is correct, move your web-page and php script into a directory within your server's document root, on *nix this could be something like /var/www/directoryName/ and access the page via http://localhost/directoryName/dataEntryForm.html. If you're on Windows, with IIS it could be C:\inetPub\directoryName\ accessed, as above, with http://localhost/directoryName/dataEntryForm.html.
Incidentally, please forgive me for not linking to a demo of the page running, it's just that I'd prefer not to run the risk of exposing my server to, presumably, vulnerable scripts.
Your full code is like this and I tested it, working perfectly.
<html>
<head>
<title>
Entering data into text
</title>
</head>
<body>
<h1>
Entering data into text
</h1>
<form action="text.php" method="post">
What is ur name ?
<input type="text" name="data" />
<input type="submit" value="send" name="btn_save" />
</form>
</body>
</html>
text.php
<?php
if(isset($_POST['btn_save']))
{
$data=$_POST['data'];
}
?>
<html>
<head>
<title>
Reading data from textfields
</title>
</head>
<body>
<h1>
reading data from text field
</h1>
Thanks for answering,
<?php echo $_POST["data"];?>
</body>
</html>
working perfectly.
I have written some basic code so that a text area is displayed, and when the user inputs the text, clicks submit and it shows on the page in the method=''.
The code i have for the form is :
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<h1>Fun Translator</h1>
<form method="post" action="query.php">
<textarea name="txtarea">
</textarea>
<input type="submit" />
</form>
The code on the page query.php is:
<html>
<body>
<?php
echo $_POST["txtarea"];
?>
</body>
</html>
By looking at google, other questions and such, this should work, but doesnt!
Solution:
Thanks to Marc Audet, I put in phpinfo and all that came up in a big table as it does, I took it out and it started working. Any explanations?
this code is perfectly fine. something else is going wrong.
The textarea is not link to your form, you should add form="" in textarea like this:
<form method="post" action="query.php" name="myform">
<textarea name="txtarea" form="myform">
use
echo htmlspecialchars($_POST['name']);
to get value.
some browsers like the ID...
add an ID like this:
<form method="post" action="query.php">
<textarea name="txtarea" id="txtarea">
</textarea>
<input type="submit" />
</form>
Best guess: Your server doesn't support PHP.
Check the source code as delivered to the browser. If the <?php bit shows up, then the server hasn't run the page through a PHP engine.
As far as I know you should be doing textarea in the quotes not txtarea, I am not really sure though, it might be beneficial to use an ID instead.