Home  >  Article  >  Backend Development  >  How to develop a simple form builder and fill-in function using PHP

How to develop a simple form builder and fill-in function using PHP

王林
王林Original
2023-09-20 09:34:491614browse

How to develop a simple form builder and fill-in function using PHP

How to use PHP to develop a simple form generator and fill-in function

In website development, forms are one of the most common and important functions. Through forms, we can collect user input information and then perform a series of data processing. As a powerful server-side language, PHP provides a wealth of functions and class libraries, making it easier and more efficient to develop form functions.

This article will use a simple example to introduce how to use PHP to develop form generator and fill-in functions. We will explain it in two parts, namely form generation and form filling.

1. Form generation

Form generation mainly includes two aspects: the generation of HTML form elements and the processing of form submission.

  1. Generation of HTML form elements

In PHP, we can use the echo statement to output HTML code. Using this feature, we can use some loops and conditional statements to generate multiple form elements.

The following is a simple example to generate a form containing text input boxes, radio button boxes and drop-down menus:

<?php
echo '<form method="post" action="submit.php">'; // form元素开始

echo 'Name: <input type="text" name="name"><br>'; // 文本输入框

echo 'Gender: <input type="radio" name="gender" value="male"> Male '; // 单选框
echo '<input type="radio" name="gender" value="female"> Female<br>';

echo 'Favorite fruit: '; // 下拉菜单
echo '<select name="fruit">';
echo '<option value="apple">Apple</option>';
echo '<option value="banana">Banana</option>';
echo '<option value="orange">Orange</option>';
echo '</select><br>';

echo '<input type="submit" value="Submit">'; // 提交按钮

echo '</form>'; // form元素结束
?>

The above code will output a form containing text input boxes, radio button boxes, Form with drop-down menu and submit button, and point the form's submission address to submit.php. You can customize the elements and styles of the form according to your needs.

  1. Processing of form submission

When the user clicks the submit button of the form, we need to process the submitted data. This includes obtaining user-entered values, validating data, and executing specific business logic.

We can process it in the target page of the form submission (such as submit.php). The following is a simple example to save the data submitted by the form to a file:

<?php
if($_SERVER["REQUEST_METHOD"] == "POST") {
    $name = $_POST["name"];
    $gender = $_POST["gender"];
    $fruit = $_POST["fruit"];

    $data = "Name: " . $name . "
"
          . "Gender: " . $gender . "
"
          . "Favorite fruit: " . $fruit . "
";

    $file = fopen("data.txt", "a"); // 以追加方式打开文件
    fwrite($file, $data);
    fclose($file);

    echo "Data saved successfully.";
}
?>

The above code first determines whether the request method is POST, and if so, obtains the submitted data through the $_POST array. Then concatenate the data into a string and use the file operation function to save the data to the data.txt file.

Related notes:

  • Pay attention to the name attribute of the form element, which corresponds to the key value in the $_POST array.
  • Pay attention to the use of file operation functions, such as fopen, fwrite and fclose.

2. Filling out the form

Filling out the form mainly includes reading existing data and displaying it in the form, as well as submitting and saving the user's updated content.

  1. Read existing data and fill in the form

We can use the file function to read the data in the file and assign it to the corresponding form element .

The following is a simple example to read previously saved data into the form:

<?php
$data = file_get_contents("data.txt");

list($name, $gender, $fruit) = explode("
", $data);

echo '<form method="post" action="submit.php">'; // form元素开始

echo 'Name: <input type="text" name="name" value="' . $name . '"><br>'; // 文本输入框

echo 'Gender: <input type="radio" name="gender" value="male" ' . ($gender == 'male' ? 'checked' : '') . '> Male '; // 单选框
echo '<input type="radio" name="gender" value="female" ' . ($gender == 'female' ? 'checked' : '') . '> Female<br>';

echo 'Favorite fruit: '; // 下拉菜单
echo '<select name="fruit">';
echo '<option value="apple" ' . ($fruit == 'apple' ? 'selected' : '') . '>Apple</option>';
echo '<option value="banana" ' . ($fruit == 'banana' ? 'selected' : '') . '>Banana</option>';
echo '<option value="orange" ' . ($fruit == 'orange' ? 'selected' : '') . '>Orange</option>';
echo '</select><br>';

echo '<input type="submit" value="Submit">'; // 提交按钮

echo '</form>'; // form元素结束
?>

The above code first uses the file_get_contents function to read the data in the data.txt file, and uses explode The function splits the data by rows. Then allocate the data to the corresponding form elements through loops and conditional statements, and use the value and checked attributes to implement default selection and filling in of saved data.

  1. Submit and save data

The submission and saving of the form is consistent with the previous logic, just put the code in the same file.

<?php
if($_SERVER["REQUEST_METHOD"] == "POST") {
    $name = $_POST["name"];
    $gender = $_POST["gender"];
    $fruit = $_POST["fruit"];

    $data = "Name: " . $name . "
"
          . "Gender: " . $gender . "
"
          . "Favorite fruit: " . $fruit . "
";

    $file = fopen("data.txt", "w"); // 以覆盖方式打开文件
    fwrite($file, $data);
    fclose($file);

    echo "Data saved successfully.";
}
?>

The above code is consistent with the previous code for form submission processing, except that the file opening mode is changed to "w" to overwrite the original data.

The above is an introduction to using PHP to develop a simple form generator and fill-in function. Through the sample code in this article, you can learn how to generate HTML form elements, handle form submission and fill in existing data. Of course, there will be more complex verification and business logic processing in actual development, which needs to be expanded and improved according to actual needs. I hope this article will be helpful to your study and practice!

The above is the detailed content of How to develop a simple form builder and fill-in function using 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