Home >Backend Development >PHP Problem >Example of modifying form data in PHP
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.
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['id']; $name = 'Form Name'; // 假设这是从数据库中获取的数据 $description = 'Form Description'; // 假设这是从数据库中获取的数据 ?> <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
.
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['id']; $name = $_POST['name']; $description = $_POST['description']; // TODO: 执行数据库操作,更新表单数据 // 页面重定向回表单展示页面 header('Location: show.php?id=' . $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.
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['id']; $name = 'Form Name'; // 假设这是从数据库中获取的数据 $description = 'Form Description'; // 假设这是从数据库中获取的数据 ?> <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['id']; $name = $_POST['name']; $description = $_POST['description']; // TODO: 执行数据库操作,更新表单数据 header('Location: show-form.php?id=' . $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!