I'm working on a page in PHP 5.3.5, and it seems that $_POST doesn't contain the data submitted from my form.
This is the HTML file :
<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>
And this is the PHP file:
<html>
<body>
Welcome <?php echo $_POST["fname"]; ?>!<br />
You are <?php echo $_POST["age"]; ?> years old.
</body>
</html>
The problem is that the form values are not passed, so I get output like this:
Welcome !
You are years old.
Instead of this:
Welcome John!
You are 28 years old.
What am I doing wrong?
Try <?php var_dump($_POST); ?> to see what's really in there. There's no way $_POST is broken - it's gonna be something else. Possibly a server setting could be disabling $_POST.
It's probably because you have magic_quotes_gpc turned on. You should turn it off:
http://www.php.net/manual/en/security.magicquotes.disabling.php
Have you check your php.ini ?
I broken my post method once that I set post_max_size the same with upload_max_filesize.
I think that post_max_size must less than upload_max_filesize.
Tested with PHP 5.3.3 in RHEL 6.0
PHP 5.3 is moving away from using global, pre-set variables, like $_POST, to avoid vulnerabilities.
The issue, as I understand it, is that programmers who have never had to use $_POST or $_GET might treat it as any other variable and open themselves up to security threats.
I haven't discovered the "proper" way to retrieve $_POST data yet, but the method I've using seems fairly sanitary.
<?php parse_url(file_get_contents("php://input"), $_POST);
This transfers the parsed HTTP POST string to the $_POST variables
Is your HTML form method se to to POST?
<form method="post" action="process.php">
<fieldset><legend>your data</legend>
<input type="text" name="txtname" id="id_name" />
<input type="text" name="txtage" id="id_age" />
</fieldset>
<button type="submit">OK</button>
</form>
You have not POSTed, or something is clobbering $_POST.
If you have no mention of $_POST in your code before trying to access them, then you haven't POSTed to your script.
Here it worked, but in PHP 5.3.1. Try this:
<!doctype html>
<html>
<head>
<title>form</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<form method="post" action="process.php">
<fieldset><legend>your data</legend>
<input type="text" name="txtname" id="id_name" />
<input type="text" name="txtage" id="id_age" />
</fieldset>
<button type="submit">OK</button>
</form>
</body>
</html>
<?php
if(isset($_POST["txtname"]))
$txtname = $_POST["txtname"];
else
unset($txtname);
if(isset($_POST["txtage"]))
$txtage = $_POST["txtage"];
else
unset($txtname);
?>
<!doctype html>
<html>
<head>
<title>form</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<p>Welcome <?=$txtname; ?>!<br />
You are <?=$txtage; ?> years old.</p>
</body>
</html>
Try to use capitals in POST:
<form action="file.php" method="POST">
Instead of:
<form action="file.php" method="post">
I also had the same problem.I used windows notepad for editing .php file and accidentally saved the file in Unicode encoding and that was creating the problem. Then I saved it in the UTF-8 encoding, and it worked like a charm. Hope this might help.
Related
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.
The post superglobal variable is empty after form submission; however, get works!
Here is the code:
index.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Simple Form</title>
</head>
<body>
<fieldset>
<form action="print.php" method="POST" >
first name:<input type="text" name="Fname"><br>
last name:<input type="text" name="Lname"><br>
<input type="submit" name="submit" value="submit">
</form>
</fieldset>
</body>
</html>
print.php
<?php
echo $_POST['Fname'];
echo $_POST['Lname'];
?>
same code works fine on my PC and print.php prints something but here on my Macbook $_post is always null after submitting the form.
I use IntelliJ IDEA as my IDE with Php plugin.
I use php 7
IntelliJ is not mapped to the MAMP server. That's why php is interpreted but POST does not work. In order to map MAMP to IntelliJ use this guide which works for IntelliJ ultimate edition and PhpStorm.
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.
I have no idea what is going on here, but my form won't post. I've stripped it back to absolute basics, but still no joy. Get works fine and I'm working on MAMP on localhost.
Here it is: simplest form in the world that won't work.
<?php
print_r($_POST);
?>
<!DOCTYPE html>
<html lang="en" class="">
<head>
<title>Site Title</title>
</head>
<body class="wrapper">
<p>This is the form</p>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
<input type="text" name="firstName" />
<input type="submit" name="fSubmit" value="submit" />
</form>
</body>
</html>
I've probably missed something so stupid, hence getting some more eyes on it!
Cheers
Alright folks. Thanks for all your ideas - I had already tried EVERY POSSIBLE combination of actions, names, etc... I had the corret hml headers posted to the page. I just restarted my mac and re-ran the servers and everything seems to be ok. I have no idea what went wrong, but it seems to be fixed now.
Apologies for the fairly useless question.
maybe try
<?php
print_r($_POST);
?>
<!DOCTYPE html>
<html lang="en" class="">
<head>
<title>Site Title</title>
</head>
<body class="wrapper">
<p>This is the form</p>
<form action="" method="post">
<input type="text" name="firstName" />
<input type="submit" name="fSubmit" value="submit" />
</form>
</body>
</html>
or even just make sure php is installed/enabled
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>