Home >Backend Development >PHP Tutorial >mysql - php cannot get form data
1. In the environment built by wamp, the return value of using post to obtain form submission data is empty
The code is as follows:
<code><!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title></title> </head> <body> <form action="test.php" method="post"> Firstname: <input type="text" name="firstname" value="aaa" /> Lastname: <input type="text" name="lastname" /> Age: <input type="text" name="age" /> <input type="submit" /> </form> </body> </html></code>
<code><?PHP $firstname = $_POST["firstname"]; $lastname = $_POST["lastname"]; $age = $_POST["age"]; if($firstname==""){ echo "<script>alert('Your username is empty!');</script>"; }else{ var_dump($firstname); } ?></code>
Error report: Notice: Undefined index: firstname in E:WebWeb SoftwarewampwwwDEMOtest.php
2. There is nothing wrong with the fact that the php server is Apache and the port number is also 80
3. Tried the get method, but still can’t get the data
I found my own problem: the html file was not opened from the same path as the php file~
1. In the environment built by wamp, the return value of using post to obtain form submission data is empty
The code is as follows:
<code><!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title></title> </head> <body> <form action="test.php" method="post"> Firstname: <input type="text" name="firstname" value="aaa" /> Lastname: <input type="text" name="lastname" /> Age: <input type="text" name="age" /> <input type="submit" /> </form> </body> </html></code>
<code><?PHP $firstname = $_POST["firstname"]; $lastname = $_POST["lastname"]; $age = $_POST["age"]; if($firstname==""){ echo "<script>alert('Your username is empty!');</script>"; }else{ var_dump($firstname); } ?></code>
Error report: Notice: Undefined index: firstname in E:WebWeb SoftwarewampwwwDEMOtest.php
2. There is nothing wrong with the fact that the php server is Apache and the port number is also 80
3. Tried the get method, but still can’t get the data
I found my own problem: the html file was not opened from the same path as the php file~
Determine a few questions!
1. The test1.html
and test.php
files of this test are both in the same level directory DEMO
.
2. Please do not use the file://
path to access your PHP file
, so that the PHP file will not be interpreted by the php.exe interpreter installed on your computer.
3. Please use the http://
protocol to access your test1.html
, and fill in the correct values to submit the form to test.php
.
4. Please pay attention to whether your PHP file starts with <?php
or <?PHP
.
Confirm the above points and see if the problem still exists?
Test your code and there is no problem
Firstname: <input type="text" name="firstname"value="aaa" /> 改成 Firstname: <input type="text" name="firstname" value="aaa" /> 试试 name 和 value 之间少了一个空格吧
Seems like there’s no problem?
Error report: Notice: Undefined index: firstname in E:WebWeb SoftwarewampwwwDEMOtest.php
This error is a Notice-level error and can be ignored. It wants to tell you that $firstname is already in use before it has been declared. But if you don't want it to happen, there are many ways. For example, modify php_error in php.ini.