【问题标题】:Inputting from html to csv via php通过 php 从 html 输入到 csv
【发布时间】:2022-02-01 21:56:34
【问题描述】:

所以我在尝试从 html 导出 csv 并发现 php 是最好的方法,并且实际上有一个专用的 php 函数。但是我在输入时遇到了问题,我对 php 还很陌生,所以这不是我的强项,所以我只是从他们的网站上复制了 php 的示例以使其正常工作。但是它不起作用,可能是因为我不确定 html 或 php 的问题。

另外请注意,一旦我得到这个工作,我是否能够使用 $_post['content'] 获取输入并将其插入到 $keys 数组中,这与输入“字符串”的工作方式相同

<?php
/*
    $name = $_POST['name']
    $country = $_POST['country']
    $email = $_POST['email']



$keys = array($name, $country, $email);



    $csv_line = array();
    foreach($keys as $key){
        array_push($csv_line,'' . $_GET[$key]);
    }
    $data = 'data.csv';
    $csv_line = implode(',',$csv_line);
    if(!file_exists($data)){$csv_line = "\r\n" . $csv_line;}
    $fcon = fopen($data,'a');
    $fcontent = $csv_line;
    fwrite($fcon,$csv_line);
    fclose($fcon);




    echo file_get_contents($data);
    */
    $list = array (
        array('aaa', 'bbb', 'ccc', 'dddd'),
        array('123', '456', '789'),
        array('"aaa"', '"bbb"')
    );
    
    $fp = fopen('data.csv', 'w');
    
    foreach ($list as $fields) {
        fputcsv($fp, $fields);
    }
    fclose($fp);
?>
@import "shortcuts.scss";

*{
    padding: 0;
    margin: 0;
}
body{
    form{
        @include flex-pos(column);
        input{
            margin: 30px auto;
            font-size: 200%;
        }
        button{
            @include box-props(block, grey, 100px, 300px, 20px);
            cursor: pointer;
            margin-top: 3em;
        }
    }
}
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>CSV test</title>
<link href="styles.css" rel="stylesheet" type="text/css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>
    <form action="process.php" method="post">
        <input type="text" placeholder="name" id="name" name="name">
        <input type="text" placeholder="country" id="country" name="country">
        <input type="text" placeholder="email" id="email" name="email">
        <button type="submit">Submit</button>
    </form>
</body>
<script src="functions.js"></script>
</html>

加载资源失败:服务器响应状态为 405 () VM9:2762 crbug/1173575,非 JS 模块文件已弃用。

【问题讨论】:

  • “不工作”不起作用,作为问题描述。 解释它到底是如何不工作的。代码目前不关心可能通过任何形式提交的任何值,这很清楚,不是吗?您有 尝试 访问 POST 参数的脚本的前半部分,已注释掉 ...
  • 加载资源失败:服务器响应状态为 405 () VM9:2762 crbug/1173575,非 JS 模块文件已弃用。这就是控制台中的响应
  • 405 Method Not Allowed 通常是当您尝试将数据发布到网络服务器认为是“静态”资源的地方时得到的。你确定你的配置正确,以在以.php 开头的文件中执行 PHP 代码吗?
  • 至于注释掉的变量,我现在不太在意,只要它有效,我应该能够将输入放入数组中,但是上面评论中提到的问题是破坏提交。
  • 什么问题?未注释的代码运行良好并创建了一个小 csv 文件。再说一遍,您的问题到底是什么?

标签: php html forms sass export-to-csv


【解决方案1】:

我假设您希望注释代码正常工作,并且您希望每次用户提交表单时都在 csv 文件中添加一行

有一个专门用来帮助创建 CSV 文件的函数,See fputcsv()

    // it would be advisable to check HERE that all the 3 $_POST values actually existed before proceeding


    $titles = ['name', 'country', 'email'];
    $filename = 'data.csv';

    if(!file_exists($filename)) {
        // write the title line as this is the first time we did this
        $fp = fopen($filename, 'w');
        fputcsv($fp, $titles);
        fputcsv($fp, [$_POST['name'], $_POST['country'], $_POST['email']);
    } else {
        // add a new line to the bottom of the file
        $fp = fopen($filename, 'a');
        fputcsv($fp, [$_POST['name'], $_POST['country'], $_POST['email']);
    }
    
    fclose($fp);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多