How to create a form using PHP
To create a form, you first need to create an HTML file containing form elements, and then use PHP to handle the data submitted by the form. Here is a simple example:
- Create an HTML form file (form.html):
<!DOCTYPE html>
<html>
<head>
<title>表单示例</title>
</head>
<body>
<h2>请填写表单</h2>
<form action="process_form.php" method="post">
<label for="name">姓名:</label>
<input type="text" name="name" id="name"><br><br>
<label for="email">邮箱:</label>
<input type="email" name="email" id="email"><br><br>
<input type="submit" value="提交">
</form>
</body>
</html>
- Create a PHP file (process_form.php) to handle the data submitted through the form.
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $_POST["name"];
$email = $_POST["email"];
echo "您输入的姓名是:" . $name . "<br>";
echo "您输入的邮箱是:" . $email;
}
?>
- When the user submits the form, the form data will be sent to the process_form.php file, which will retrieve the form data from the $_POST array and display it on the page.
- To view the form.html file, fill out the form and submit it in the browser, then you will be able to see the entered name and email displayed on the page.
This is a simple example that you can customize and expand the form functionality according to your needs.