Home > Backend Development > PHP Tutorial > mysql - php cannot get form data

mysql - php cannot get form data

WBOY
Release: 2023-03-03 08:02:01
Original
1504 people have browsed it

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>
Copy after login
Copy after login
<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>
Copy after login
Copy after login

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

mysql - php cannot get form data
mysql - php cannot get form data
mysql - php cannot get form data

I found my own problem: the html file was not opened from the same path as the php file~

Reply content:

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>
Copy after login
Copy after login
<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>
Copy after login
Copy after login

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

mysql - php cannot get form data
mysql - php cannot get form data
mysql - php cannot get form 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
mysql - php cannot get form data

Firstname: <input type="text" name="firstname"value="aaa" />

改成

Firstname: <input type="text" name="firstname" value="aaa" />

试试 name 和 value 之间少了一个空格吧
                            
            

mysql - php cannot get form data

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.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template