Home  >  Article  >  Backend Development  >  PHP form processing: form data export and printing

PHP form processing: form data export and printing

王林
王林Original
2023-08-09 15:48:191416browse

PHP form processing: form data export and printing

PHP form processing: form data export and printing

In website development, forms are an indispensable part. When a form on the website is filled out and submitted by the user, the developer needs to process the form data. This article will introduce how to use PHP to process form data, and demonstrate how to export the data to an Excel file and print it out.

1. Form submission and basic processing

First, you need to create an HTML form for users to fill in and submit data. Let's say we have a simple feedback form with name, email, and comments. The HTML code is as follows:

<form action="process.php" method="post">
    <label for="name">姓名:</label>
    <input type="text" name="name" id="name">
    <br>
    <label for="email">邮箱:</label>
    <input type="email" name="email" id="email">
    <br>
    <label for="comment">评论:</label>
    <textarea name="comment" id="comment"></textarea>
    <br>
    <input type="submit" value="提交">
</form>

In the process.php file, we can obtain the submitted data through the $_POST super global variable and perform basic processing and verification. The following is a sample code for processing form data:

// process.php

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $name = $_POST["name"];
    $email = $_POST["email"];
    $comment = $_POST["comment"];

    // 处理数据...
    
    // 验证数据...
    
    // 将数据保存至数据库或文件...
    
    // 发送电子邮件...
    
    // 其他操作...
}

2. Export form data to Excel files

Sometimes we need to export form data to Excel files to facilitate statistics and analysis. For this we can use the PHPExcel library. First, you need to download and import the PHPExcel library. The following is a sample code for exporting form data to an Excel file:

// process.php

require_once 'PHPExcel/Classes/PHPExcel.php';

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $name = $_POST["name"];
    $email = $_POST["email"];
    $comment = $_POST["comment"];

    // 创建Excel对象
    $excel = new PHPExcel();
    
    // 设置表格标题
    $excel->getActiveSheet()->setCellValue('A1', '姓名');
    $excel->getActiveSheet()->setCellValue('B1', '邮箱');
    $excel->getActiveSheet()->setCellValue('C1', '评论');
    
    // 填充数据
    $excel->getActiveSheet()->setCellValue('A2', $name);
    $excel->getActiveSheet()->setCellValue('B2', $email);
    $excel->getActiveSheet()->setCellValue('C2', $comment);
    
    // 设置文件名和文件格式
    $filename = 'feedback_' . date('YmdHis') . '.xlsx';
    
    // 导出Excel文件
    $writer = PHPExcel_IOFactory::createWriter($excel, 'Excel2007');
    $writer->save($filename);
    
    // 下载文件
    header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
    header('Content-Disposition: attachment;filename="' . $filename . '"');
    header('Cache-Control: max-age=0');
    
    $writer->save('php://output');
    exit();
}

After the user submits the form, the server will generate an Excel file and send it to the user through HTTP header information. The file name starts with feedback_, followed by the current date and time, and finally uses the .xlsx extension.

3. Print form data

Sometimes, we need to print out form data, such as displaying it on a web page or sending it to a printer. The following is a sample code that prints form data to the browser:

// process.php

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $name = $_POST["name"];
    $email = $_POST["email"];
    $comment = $_POST["comment"];
    
    $html = '<h1>反馈详情</h1>';
    $html .= '<p><strong>姓名:</strong>' . $name . '</p>';
    $html .= '<p><strong>邮箱:</strong>' . $email . '</p>';
    $html .= '<p><strong>评论:</strong>' . $comment . '</p>';

    echo $html;
    
    // 其他打印操作...
}

After the user submits the form, the server will generate an HTML string containing the form data and display it on the browser. You can customize the style according to your needs, such as adding CSS styles or pagination printing.

The above is what this article introduces. Through the above sample code, you can learn how to use PHP to process form data and export the data to an Excel file or print it out. I hope it will be helpful to your website development work!

The above is the detailed content of PHP form processing: form data export and printing. 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