SO中文参考
首页
(current)
程序语言
c
java
python
c++
go
javascript
swift
c#
操作系统
linux
ubuntu
centos
unix
数据库
oracle
mysql
mongodb
postgresql
框架
node.js
angular
react-native
avalon
django
twisted
hadoop
.net
移动开发
android
ios
搜索
将 HTML 表单导出到 CSV 文件测试</desc> <question vote="0"> <p>我从<a href="https://stackoverflow.com/questions/48259204/how-to-export-data-entry-in-html-form-to-csv-file">旧帖子</a>借用了一些代码来尝试实现这一目标,但它没有按预期工作。有两个问题。首先是 html:</p> <pre><code><!DOCTYPE html> <html> <title>Test Form</title> <head> </head> <body> <form name="xyz_form" action="proces.php" method="post"> <section class="border-bottom"> <div class="content"> <h3>ID Number</h3> <div class="form-control-group"> <div class="form-control form-control-number"> <input type="number" name="id_number" id="id_number"> </div> </div> <h3>First Name</h3> <div class="form-control-group"> <div class="form-control form-control-text"> <input type="text" name="first_name" id="first_name"> </div> </div> </div><!--.content--> <section class="data-capture-buttons one-buttons"> <div class="content"> <input type="submit" value="Submit"> </div> </section><!--.data-capture-buttons--> </form> </body> </html> </code></pre> <p>和 PHP 代码:</p> <pre><code><?php $fieldA = $_POST['id_number']; $fieldB = $_POST['first_name']; $keys = array($fieldA,$fieldB); $csv_line = $keys; foreach( $keys as $key ){ array_push($csv_line,'' . $_GET[$key]); } $fname = 'file_to_write_to.csv'; $csv_line = implode(',',$csv_line); if(!file_exists($fname)){$csv_line = '\r\n' . $csv_line;} $fcon = fopen($fname,'a'); $fcontent = $csv_line; fwrite($fcon,$csv_line); fclose($fcon); ?> </code></pre> <p>表单运行并写入 CSV 文件,但在运行时它会抛出两个警告:</p> <pre><code>Warning: Undefined array key 3 in E:\XAMPP\App\xampp\htdocs\test_form\proces.php on line 8 Warning: Undefined array key "Third" in E:\XAMPP\App\xampp\htdocs\test_form\proces.php on line 8 </code></pre> <p>(3 和“Third”是表格中需要的两个值。)</p> <p>现在它写入 CSV,但格式不正确。</p> <pre><code>1,First,,2,Second,,3,Third,, </code></pre> <p>它在两个值后面添加了一个额外的逗号并且无法识别 对于新线路。</p> <p>我尝试过将引号从单引号更改为双引号等。我还测试了按照原帖中的建议将第 6 行 <pre><code>$csv_line = $keys;</code></pre> 更改为 <pre><code>$csv_line = array();</code></pre>,但这并不能解决问题。</p> <p>有好心人可以帮我解决这个问题吗?谢谢。</p> </question> <answer tick="true" vote="1"> <p>@ADyson 线索是使用 <pre><code>fputcsv</code></pre> 的提示。我需要额外的参数 eol: 将默认行尾从 <pre><code>\n</code></pre> 更改为 <pre><code>\r\n</code></pre></p> <p>这是我的 PHP。</p> <pre><code><?php $fieldA = $_POST["id_number"]; $fieldB = $_POST["first_name"]; $keys = array($fieldA, $fieldB); $file_pointer = fopen("file_to_write_to.csv","a"); fputcsv($file_pointer, $keys, eol: "\r\n"); fclose($file_pointer); ?> </code></pre> </answer> </body></html>
问题描述
投票:0
回答:0
php
html
export-to-csv
最新问题
如何查看 Foursquare 场地的签到情况?
使用 win32gui 并关闭 QApplication 中的嵌入式应用程序时捕获“保存更改”对话框?
Gradle 构建因内存不足而失败:Java 堆空间错误
为什么我的字体在某些电脑上不显示?但我可以在不同的网站上查看我未安装的其他字体?
在 ASP.NET Core 6 中使用 TempData 时出现 ERR_HTTP2_PROTOCOL_ERROR
如何降级flutter sdk(频道没有之前录制的版本)
总是有警告消息,而且当我输入正确的学生编号时,它总是显示访问被拒绝
搜索所有小于4MB的目录
已解决:调试错误的 AVL 树“插入”操作
如何删除内容中的多余空格?
MyRazorPage.BuildRenderTree(RenderTreeBuilder):找不到合适的方法来覆盖(CS0115)
场景构建器无法显示使用 Java 17 编译的自定义 TableView
coverlet.collector 和 coverlet.msbuild 之间的区别?
Cygwin 错误:“child_info_fork::abort:加载到不同的地址:”
Chart.js:如何隐藏标签的某些文本?
如何在 VScode 中从同步中删除特定文件或提交到 github?
从 <Signal2D, title: , dimensions: (10|4096, 4096)>
如何在一部智能手机上模拟信标并在第二部智能手机上检测它并生成自动通知(全部使用现有应用程序)
如何在 React 中首次渲染时对博客进行初始排序?
Selenium Chromedriver 失败并出现堆栈跟踪错误消息
© www.soinside.com 2019 - 2024. All rights reserved.