I've looked at several examples and have even copied solutions; no matter what I do, my HTML data within the form will not post.
The most typical solution on SO for users that were stuck was to include the name
attribute, but I've had that since my first attempt.
Here is my HTML:
<form style="color:red;" action="test.php" method="post">
Name: <input style="color:blue;" type="text" name="formName"><br>
E-mail: <input style="color:blue;" type="text" name="formEmail"><br>
<input type="submit">
</form>
Below is a snapshot of what the page looks like.
Here is the code for test.php
<html>
<body>
Welcome <?php echo $_POST["formName"]; ?><br>
Your email address is: <?php echo $_POST["formEmail"]; ?>
</body>
</html>
Below is a picture of data I've filled in.
And the resulting page when I click submit.
It's been years since I've used PHP; I don't remember it being this complicated so I assume I'm either doing something wrong or something is wrong with my ini file? Thanks.
EDIT: Here are some suggestions that I've tried:
"Try putting <?php phpinfo(); ?>
into a test .php"
It worked successfully, as can be seen below.
"What happens when you var_dump($_POST)
?"
The only thing printed to the screen is array(0) { }
"How are you accessing this? as http://localhost/file.xxx
or as file:///file.xxx
?"
This actually helped narrow down the problem! As stated before, I've tried testing this in two ways; through PhpStorm (which deploys in a localhost environment) and through my server. I screwed up with the server method and wasn't running it through localhost; once I fixed this, the PHP script works on the server as expected! So now I can narrow it down to the issue being specifically with my PhpStorm environment, which runs the scripts in localhost
. Please keep in mind that I've tried all suggestions with both the server and PhpStorm, so that means that I did try <?php phpinfo(); ?>
in PhpStorm and it worked as expected. Because the server method works fine, it's possible my set up, .ini, and/or php install on my machine is bad.
答案 0 :(得分:2)
The code works for me (and apparently, for other people). From the process of elimination, there's really only one answer to your question:
Your environment is messed up.
First things first, ensure that PHP is installed and running properly. Create a test .php
file with this code:
<?php
phpinfo();
?>
By visiting this page in a web browser, you should see a giant dump of PHP information. If you don't see anything, then PHP is not installed or configured correctly.
答案 1 :(得分:1)
Please check, that test.php is in the same directory, than your first html file. If it's true, try to replace by that :
the first:
<form style="color:red;" method="POST" action="test.php">
Name: <input style="color:blue;" type="text" name="formName"><br>
E-mail: <input style="color:blue;" type="text" name="formEmail"><br>
<input type="submit">
</form>
test.php
<html>
<body>
Welcome <?=$_POST["formName"]?><br>
Your email address is: <?=$_POST["formEmail"]?>
</body>
</html>
UPDATE Try to add this code to your php-script's header, this will show any errors :
<?
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
?>
答案 2 :(得分:1)
这实际上是PhpStorm的一个众所周知的问题。您可以按照本指南PHPstorm 10.0.3 Error 502 Bad Gateway (Due to JavaVM?)进行操作,并发现它可以解决此问题(就像它对我所做的那样)。正如作者在那里所述,“您正在使用PhpStorm自己的内置Web服务器,它现在有一些问题(特别是POST请求,例如WEB-17317)。
基本上,我所做的是按照此页面上的步骤操作:https://confluence.jetbrains.com/display/PhpStorm/Deploying+PHP+applications+with+PhpStorm
一个很好的开始是将项目配置为在服务器上部署。
工具 - &gt;部署 - &gt;构造强>
答案 3 :(得分:-3)
You forgot the ?php directive to call the interpreter.