Home > Article > Backend Development > How to use post method to implement page jump function in php
The POST method in PHP is a very common way to submit data. Unlike the GET method, the POST method does not expose data on the URL, so for some sensitive data such as passwords, it is more secure to use the POST method.
In the actual development process, it is often necessary to submit one page to another page to achieve jumps between pages. This article will introduce how to use PHP's POST method to achieve page jump.
1. Page jump overview
Page jump means that after the user clicks a button or link on the current page, he will jump to another page. Jumping can be achieved through links, redirects, form submissions, etc. This article mainly introduces page jump based on form submission.
2. Form submission method
The form is one of the commonly used elements in HTML. Forms allow users to submit data to the server for processing. In PHP, to submit data through a form, you can use the POST method to achieve page jump.
The specific steps are as follows:
<form action="target.php" method="POST"> <!-- 表单元素 --> </form>
<form action="target.php" method="POST"> <input type="text" name="name" value="张三"> <textarea name="desc">这是一篇介绍POST方式实现页面跳转的文章。</textarea> <input type="submit" value="提交"> </form>
In the above code, the input and textarea tags are used to set the input box and text field respectively, and the name attribute represents the element. The name, value attribute represents the value of the element. After submitting the form, PHP will pass these data to the target page target.php in POST mode.
<?php $name = $_POST['name']; $desc = $_POST['desc']; ?>
In the above code, $_POST['name'] means to get the element named name when submitting the form. value. Similarly, $_POST['desc'] means getting the value of the element named desc.
<?php $name = $_POST['name']; $desc = $_POST['desc']; // 处理数据 // ... // 页面跳转 header('Location: http://example.com'); ?>
Through the header function, you can redirect the page to http://example.com to achieve page jump.
3. POST Jump Example
The following is a specific POST jump example. Suppose there is a login page where the user needs to enter their username and password to log in. When the login fails, the login page will be returned and an error message will be displayed.
The login page code is as follows:
<!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title>用户登录</title> </head> <body> <?php if(isset($_GET['error'])): ?> <p>用户名或密码错误!</p> <?php endif; ?> <form action="login.php" method="POST"> <p>用户名:<input type="text" name="username"></p> <p>密码:<input type="password" name="password"></p> <p><input type="submit" value="登录"></p> </form> </body> </html>
When the user clicks the login button, the page will be submitted to login.php for processing. The login.php code is as follows:
<?php $username = $_POST['username']; $password = $_POST['password']; // 检查用户名和密码是否正确 if($username == 'admin' && $password == '123456') { // 登录成功,跳转至欢迎页面 header("Location: welcome.php"); } else { // 登录失败,返回登录页面,并附带错误提示 header("Location: login.php?error=true"); } ?>
In the above code, first use $_POST to obtain the username and password data submitted by the login page. Then perform login verification. If correct, use the header function to jump to the page welcome.php; otherwise, use the header function to jump to login.php, and attach an error parameter to the jump URL to indicate a login error.
To sum up, submitting data through POST can realize jumps between pages and pass some necessary data when jumping. This is very important for web development.
The above is the detailed content of How to use post method to implement page jump function in php. For more information, please follow other related articles on the PHP Chinese website!