Home > Backend Development > PHP Problem > How to convert form data into txt file using PHP

How to convert form data into txt file using PHP

PHPz
Release: 2023-04-05 16:16:02
Original
1222 people have browsed it

PHP, as a popular server-side scripting language, plays a very important role in Web development. Among them, processing form data is one of the important applications of PHP. This article will introduce how to use PHP to convert form data into txt files to facilitate subsequent data processing.

1. HTML form

To process form data in PHP, you must first have an HTML form. For convenience, we use a simple form as an example here. The code is as follows:

<form method="post" action="submit.php">
  <label>姓名:</label><input type="text" name="name"/><br/>
  <label>年龄:</label><input type="text" name="age"/><br/>
  <label>性别:</label><input type="radio" name="sex" value="male"/>男
                      <input type="radio" name="sex" value="female"/>女
                      <input type="radio" name="sex" value="other"/>其他<br/>
  <label>爱好:</label><input type="checkbox" name="hobby[]" value="reading"/>阅读
                      <input type="checkbox" name="hobby[]" value="writing"/>写作
                      <input type="checkbox" name="hobby[]" value="swimming"/>游泳<br/>
  <input type="submit" name="submit" value="提交"/>
</form>
Copy after login

The above form includes four fields: name, age, gender, and hobbies, and has a submit button. Field types include text boxes, radio buttons and check boxes, among which the hobby field is multi-select. The method attribute of the form is set to "post" and the action attribute is set to "submit.php", indicating that the form data is submitted to the submit.php page for processing.

2. PHP processing form data

Next, you need to obtain the form data in the submit.php page and process it. The code is as follows:

<?php
  if(isset($_POST["submit"])){
    $name = $_POST["name"];
    $age = $_POST["age"];
    $sex = $_POST["sex"];
    $hobby = $_POST["hobby"];

    $text = "姓名:".$name."\r\n";
    $text .= "年龄:".$age."\r\n";
    $text .= "性别:".$sex."\r\n";
    $text .= "爱好:".implode(",", $hobby)."\r\n";

    $filename = "data.txt";
    $file = fopen($filename, "w");
    fwrite($file, $text);
    fclose($file);
  }
?>
Copy after login

The above code first determines whether the submit button is clicked through the isset function, and if so, continues execution. Then, obtain each field value in the form through the $_POST superglobal array and concatenate them into a string. Among them, carriage return and line feed characters are added after each field value to facilitate differentiation in the txt file.

Next, a file name variable $filename is defined and its value is set to "data.txt". Then, open the file through the fopen function and set the file's opening mode to "w" (ie, write mode). Next, use the fwrite function to write the spliced ​​string into the file, and finally use the fclose function to close the file handle.

3. Result Verification

After completing the above steps, we can run the submit.php file on the server to test whether the form data can be successfully converted into a txt file. After execution, a file named "data.txt" will be generated on the server, and its content should be consistent with the form data. Among them, carriage return characters and line feed characters are used to distinguish each field value.

4. Summary

This article introduces how to use PHP to convert form data into a txt file. In practical applications, this method can help developers process form data more conveniently. Of course, in actual applications, the data processing requirements will be more complex, and we can continue to expand the code to meet more needs. For example, we can use loops to traverse to process multiple forms. In short, PHP, as a popular server-side scripting language, can play a great role in web development. Interested readers can continue to learn more.

The above is the detailed content of How to convert form data into txt file using 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