Home > Backend Development > PHP Problem > How to use post method to implement page jump function in php

How to use post method to implement page jump function in php

PHPz
Release: 2023-04-04 19:36:02
Original
1788 people have browsed it

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:

  1. Create a form, set the action attribute to the page you want to jump to, and set the method attribute to the POST method, as shown below:
<form action="target.php" method="POST">
    <!-- 表单元素 -->
</form>
Copy after login
  1. Add the data items that need to be submitted in the form. You can use input, textarea and other tags to set form elements, as shown below:
<form action="target.php" method="POST">
    <input type="text" name="name" value="张三">
    <textarea name="desc">这是一篇介绍POST方式实现页面跳转的文章。</textarea>
    <input type="submit" value="提交">
</form>
Copy after login

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.

  1. The target page processes the submitted data. In the target page, you can use the $_POST array to get the submitted data:
<?php
    $name = $_POST[&#39;name&#39;];
    $desc = $_POST[&#39;desc&#39;];
?>
Copy after login

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.

  1. After processing the data, you can jump to the page. You can use the header function to achieve page jump:
<?php
    $name = $_POST[&#39;name&#39;];
    $desc = $_POST[&#39;desc&#39;];

    // 处理数据
    // ...
    
    // 页面跳转
    header(&#39;Location: http://example.com&#39;);
?>
Copy after login

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[&#39;error&#39;])): ?>
        <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>
Copy after login

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[&#39;username&#39;];
    $password = $_POST[&#39;password&#39;];

    // 检查用户名和密码是否正确
    if($username == &#39;admin&#39; && $password == &#39;123456&#39;) {
        // 登录成功,跳转至欢迎页面
        header("Location: welcome.php");
    } else {
        // 登录失败,返回登录页面,并附带错误提示
        header("Location: login.php?error=true");
    }
?>
Copy after login

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!

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