Home  >  Article  >  Backend Development  >  Example of modifying form data in PHP

Example of modifying form data in PHP

PHPz
PHPzOriginal
2023-04-10 09:35:26974browse

In website development, forms are one of the essential components. During the processing of the form, there may be situations where the form data needs to be modified. This article will introduce an example of using PHP to modify form data.

  1. Create a modification form page

First, we need to create a form page containing the data to be modified. In this page, you need to display the form data that needs to be modified and provide a blank form that can be modified. The following is a sample page:

<!DOCTYPE html>
<html>
<head>
    <title>修改表单数据</title>
</head>
<body>
    <h1>修改表单数据</h1>
    <?php
        // 获取要修改的数据
        $id = $_GET[&#39;id&#39;];
        $name = &#39;Form Name&#39;; // 假设这是从数据库中获取的数据
        $description = &#39;Form Description&#39;; // 假设这是从数据库中获取的数据
    ?>
    <form method="post" action="update.php">
        <label for="name">表单名称:</label>
        <input type="text" name="name" value="<?php echo $name; ?>">
        <br>
        <label for="description">表单描述:</label>
        <textarea name="description"><?php echo $description; ?></textarea>
        <br>
        <input type="hidden" name="id" value="<?php echo $id; ?>">
        <input type="submit" value="保存">
    </form>
</body>
</html>

In the above example, use PHP to obtain the ID of the data to be modified from the GET request, and then query the data corresponding to the ID from the database and display it in the form. It is worth noting that in order for the form to submit data correctly, the submission address of the form needs to be specified on the page as update.php.

  1. Handling form submission

When a user submits a modified form, PHP needs to be used to process the form submission. The following is an example form processing page:

<?php
    // 获取要修改的数据
    $id = $_POST[&#39;id&#39;];
    $name = $_POST[&#39;name&#39;];
    $description = $_POST[&#39;description&#39;];

    // TODO: 执行数据库操作,更新表单数据

    // 页面重定向回表单展示页面
    header(&#39;Location: show.php?id=&#39; . $id);
    exit;
?>

In the above example, the modified form data is first obtained from the POST request, and then the database operation is performed (a TODO placeholder is used here to represent the actual actions required). In order to allow users to see the updated form data after modification, the page redirects back to the display page through the header function, and passes the form ID as a parameter to the display page.

  1. Complete Example

Using the above form page and form processing page, we can modify the form data. The following is a complete example:

<!-- update-form.php -->
<!DOCTYPE html>
<html>
<head>
    <title>修改表单数据</title>
</head>
<body>
    <h1>修改表单数据</h1>
    <?php
        $id = $_GET[&#39;id&#39;];
        $name = &#39;Form Name&#39;; // 假设这是从数据库中获取的数据
        $description = &#39;Form Description&#39;; // 假设这是从数据库中获取的数据
    ?>
    <form method="post" action="update-form-action.php">
        <label for="name">表单名称:</label>
        <input type="text" name="name" value="<?php echo $name; ?>">
        <br>
        <label for="description">表单描述:</label>
        <textarea name="description"><?php echo $description; ?></textarea>
        <br>
        <input type="hidden" name="id" value="<?php echo $id; ?>">
        <input type="submit" value="保存">
    </form>
</body>
</html>
// update-form-action.php
<?php
    $id = $_POST[&#39;id&#39;];
    $name = $_POST[&#39;name&#39;];
    $description = $_POST[&#39;description&#39;];

    // TODO: 执行数据库操作,更新表单数据

    header(&#39;Location: show-form.php?id=&#39; . $id);
    exit;
?>

The above is an example of PHP implementing form data modification. We can use this example to learn how to handle modifications to form data and how to transfer form data between pages.

The above is the detailed content of Example of modifying form data in PHP. 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