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.
Related
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.
Here are the two pages, the 1st has a checkbox and submit button, the 2nd is php code to examine whether the checkbox was checked. But instead of getting a yes or a no answer I get absolutely nothing, just a blank page.
1st page:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<body>
<form name="input" action="submit.html" method="GET">
cb1<input type="checkbox" name="cb1" value="cb1">
<input type="submit" value="Submit">
</form>
</body>
</html>
2nd page:
<?php
if isset($_GET['cb1']) {
echo 'Checkbox set';
}
else
{
echo 'Checkbox is not set';
}
?>
There is a mistake in your PHP-Code - you should enable "error_reporting" and "display_errors"!
The if Statement should be like this:
if(isset($_GET['cb1'])) {
Edit: Also the Submit-Page should be an PHP-File
You seem to be submitting to an html page ("submit.html"). Unless you do some url rewritting, that should be a php page.
Take a look at this <form name="input" action="submit.html" method="GET">. When you click submit button, then all the data will be send to submit.html. It's impossible to run php script with html extension.
Create the second file as a php file, instead of creating it as .html, create it as submit.php. Then it will work
I've taken the following code and placed them in their respective files. I've opened up register.html in Firefox and after entering the required details to the form it loads welcome.php albeit with out the entered information. The loaded page simply reads: "Welcome! You are years old!"
Both files are stored in the same folder.
Any suggestions on what I'm doing wrong?
register.html
<html>
<body>
<form action="welcome.php" method="post">
Name: <input type="text" name="fname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
</body>
</html>
welcome.php
Welcome <?php echo $_POST["fname"]; ?>!<br />
You are <?php echo $_POST["age"]; ?> years old.
</body>
</html>
For Apache:
1) Ensure the mime module is on.
2) Try making an .htaccess file in the same directory, with the following contents:
AddType application/x-httpd-php .php
This will only work if the rewrite module is on.
3) Otherwise, if you have access to the httpd.conf file, try adding that line somewhere in it, without the .htaccess file.
Make sure to restart the server any time you make changes to httpd.conf.
Edit:
I should have also mentioned to view source of the page, to see if the PHP code is in it. which would make these steps necessary.
Another troubleshooting technique is to add <?php error_reporting(E_ALL); ?> at the top of the page, in case PHP actually is running and since there are no errors. If it is disabled, it will enable reporting an Undefined index notice. It seems highly unlikely that it would be the case, but it also seems like a useful step that could help us understand exactly what's going on. Then again, I'm pretty sure PHP just isn't executing.
Close your body tag.
<body>
not
<body
I used the code as you posted it exactly, even with the broken body tag, and it still worked.
You should take a look at your apache installation and check that it's working correctly.
I'd recommend you run a simple phpinfo test, which is basically a document (ending with .php) with only this in it:
<?php phpinfo(); ?>
You can use this
<?php
if ($_POST) {
extract($_POST);
if (!empty($fname) || !empty($age)) {
echo 'First Name: '.$fname.' Age: '. $age;
} else {
echo 'Enter first name and age';
}
}
?>
<!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" />
<title>Form</title>
</head>
<body>
<form action="" method="post">
Name: <input type="text" name="fname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
</body>
</html>
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.