Home  >  Article  >  Backend Development  >  PHP practical tutorial: Master how to pass POST parameters and jump to the page

PHP practical tutorial: Master how to pass POST parameters and jump to the page

王林
王林Original
2024-03-07 13:54:03863browse

PHP practical tutorial: Master how to pass POST parameters and jump to the page

In PHP development, we often encounter situations where we need to submit data through a form and then jump to another page for processing. At this time, you need to master how to use POST parameters to transfer data and jump to the page. Next, we will demonstrate this process through specific code examples.

First, we need to create a page containing a form where users can enter information and submit it to the server. Suppose we have a file named "form.php" with the following code:

<!DOCTYPE html>
<html>
<head>
    <title>表单提交页面</title>
</head>
<body>
    <h2>请填写以下信息:</h2>
    <form method="post" action="process.php">
        <label>姓名:</label><br>
        <input type="text" name="name"><br><br>
        <label>年龄:</label><br>
        <input type="text" name="age"><br><br>
        <input type="submit" value="提交">
    </form>
</body>
</html>

In the above code, we create a form containing name and age input boxes, and set the form's submission method to POST, submit to "process.php" page for processing.

Next, we need to create a PHP file for processing POST parameters and jumping to the page. Suppose we create a file named "process.php" with the following code:

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $name = $_POST['name'];
    $age = $_POST['age'];
    
    // 在这里可以对接收到的参数进行处理,比如插入数据库操作等
    
    // 然后跳转到另一个页面
    header("Location: result.php?name=$name&age=$age");
    exit();
} else {
    echo "无效的请求";
}
?>

In the above code, we first obtain the submitted name and age parameters through the $_POST array, and then we can further process these parameters, such as inserting into the database operation, etc. Next, we use the header function to jump to another page named "result.php" and pass the name and age as GET parameters.

Finally, we create a page to display the results. Suppose we create a file named "result.php" with the following code:

<!DOCTYPE html>
<html>
<head>
    <title>处理结果页面</title>
</head>
<body>
    <h2>处理结果:</h2>
    <?php
    $name = isset($_GET['name']) ? $_GET['name'] : '未知';
    $age = isset($_GET['age']) ? $_GET['age'] : '未知';
    
    echo "姓名:$name<br>";
    echo "年龄:$age<br>";
    ?>
</body>
</html>

In the above code, we Get the passed name and age parameters through the $_GET array and display them on the page.

Through the above code examples, we learned how to use POST parameters to transfer data and jump to the page in PHP. This is very useful for handling form submission in actual development. I hope readers can master it and use it flexibly.

The above is the detailed content of PHP practical tutorial: Master how to pass POST parameters and jump to the page. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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