PHP form processing: form data processing and result display

PHPz
Release: 2023-08-08 09:08:01
Original
1570 people have browsed it

PHP form processing: form data processing and result display

PHP form processing: form data processing and result display

In web development, forms are one of the important ways for users to interact with the website. As a powerful server-side scripting language, PHP can process the received form data and display the results to the user. This article will introduce how PHP processes form data and displays the processing results to the user.

1. Obtaining form data

In HTML, form elements are wrapped with form tags, and users can enter data through input boxes, drop-down menus, radio button boxes, etc. When a user submits a form, a server-side script processes the form data. PHP obtains form data through the super global variable $_POST or $_GET, depending on whether the POST method or the GET method is used to submit the form.

The following is a simple form example, which contains an input box and a submit button.

<form method="POST" action="process.php">
    <input type="text" name="username" placeholder="请输入用户名">
    <input type="submit" value="提交">
</form>
Copy after login

In the above form, we need to pass the user name entered by the user to the server for processing. In the server-side PHP script (here process.php), we can use the $_POST superglobal variable to get the form data.

<?php
$username = $_POST['username'];
echo "您输入的用户名是:" . $username;
?>
Copy after login

In the above code, we use $_POST['username'] to obtain the user name entered by the user, and display the result to the user through the echo statement.

2. Verification and processing of form data

In actual development, we need to verify and process the form data submitted by the user to ensure the legitimacy of the data. Below is an example of verifying a mobile phone number.

<?php
$phone = $_POST['phone'];

// 验证手机号码格式是否正确
if (!preg_match("/^1[3-9]d{9}$/", $phone)) {
    echo "请输入正确的手机号码";
} else {
    // 进行处理逻辑
    // ...
}
?>
Copy after login

In the above code, we used the regular expression /^1[3-9]d{9}$/ to verify whether the format of the mobile phone number is correct. If the mobile phone number does not meet the requirements, an error message will be output, otherwise our processing logic will be executed.

3. Result display and feedback

During the form data processing process, we need to display the processing results to the user. In PHP, we can use a hybrid method of HTML and PHP to embed the results into HTML code.

The following is a sample code that displays the form data processing results in an HTML page.

<?php
// 获取表单数据
$username = $_POST['username'];

// 处理逻辑
// ...

// 显示处理结果
?>
<!DOCTYPE html>
<html>
<head>
    <title>表单处理结果</title>
</head>
<body>
    <h1>表单处理结果</h1>
    <p>您输入的用户名是:<?php echo $username; ?></p>
    <p>处理结果为:...</p>
</body>
</html>
Copy after login

In the above code, we use the form <?php echo $username; ?> in the HTML tag to change the value of the variable $username displayed to the user. You can also perform other business processing as needed and display the results on the page.

Summary

Through the above examples, we learned how to use PHP to process form data and display the results to the user. In actual development, based on business needs, we can perform more complex data verification, processing and result display. Using PHP to process forms allows us to efficiently process user-submitted data and achieve a good interactive experience with users.

The above is the detailed content of PHP form processing: form data processing and result display. For more information, please follow other related articles on the PHP Chinese website!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!