How to create a form in php?

To create a form, you can follow these steps:

  1. Create an HTML page using the following code as a foundation.
<!DOCTYPE html>
<html>
<head>
    <title>表单示例</title>
</head>
<body>
    <form action="process.php" method="POST">
        <label for="name">姓名:</label>
        <input type="text" id="name" name="name" required><br><br>
        
        <label for="email">邮箱:</label>
        <input type="email" id="email" name="email" required><br><br>
        
        <label for="message">留言:</label>
        <textarea id="message" name="message" required></textarea><br><br>
        
        <input type="submit" value="提交">
    </form>
</body>
</html>
  1. Save the above code as a file named form.php.
  2. Create a PHP file for handling form data, you can use the following code as a foundation:
<?php
if($_SERVER["REQUEST_METHOD"] == "POST"){
    $name = $_POST["name"];
    $email = $_POST["email"];
    $message = $_POST["message"];
    
    // 处理表单数据,比如保存到数据库或发送邮件等
    
    // 返回一个响应给用户
    echo "表单提交成功!";
}
?>
  1. Save the above code as a file named process.php.
  2. Deploy these two files on the web server.

Now, once a user fills out the form and clicks the submit button, the form data will be sent to the process.php file for handling. You have the flexibility to process the form data in the process.php file according to your needs, such as saving it to a database or sending an email.

bannerAds