HTML 表单未输出到 CSV 文件(或显示正确的错误消息)
我在创建可导出到 PHP 中的 .CSV 文件的表单时遇到问题。我为 HTML 创建了一个小提琴,位于:
我使用 PHP 编码,因此我无法真正在 JSFiddle 上显示完整代码,因为它不支持 PHP,但这是我的 PHP 代码:
<?php
if($_POST['formSubmit'] == "Submit")
{
$errorMessage = "";
if(empty($_POST['brandname']))
{
$errorMessage .= "<li>Please enter a business/brand name.</li>";
}
if(empty($_POST['firstname']))
{
$errorMessage .= "<li>Please enter your first name.</li>";
}
$varBrand = $_POST['brandname'];
$varFName = $_POST['firstname'];
$varLName = $_POST['lastname'];
$varEmail = $_POST['email'];
$varSite = $_POST['website'];
if(empty($errorMessage))
{
$fs = fopen("mydata.csv","a");
fwrite($fs,$varBrand . ", " . $varFName . ", " . $varLName . ", " . $varEmail . ", " . $varSite . "\n");
fclose($fs);
exit;
}
}
?>
当我单击“提交”时,它成功转到“thankyou.php”(在表单操作中设置) )但我不明白为什么它没有发布正确的错误消息或在点击时填写我的“mydata.csv”文件。可能是视觉语法错误?如果您需要更多信息,请告诉我,我知道这有点令人困惑,因为 PHP 与 Fiddle 是分开的。
I'm having trouble creating a form that exports to a .CSV file in PHP. I created a fiddle for the HTML which is here:
I'm coding in PHP so I can't really show the full code on JSFiddle since it can't support the PHP but here's my PHP code:
<?php
if($_POST['formSubmit'] == "Submit")
{
$errorMessage = "";
if(empty($_POST['brandname']))
{
$errorMessage .= "<li>Please enter a business/brand name.</li>";
}
if(empty($_POST['firstname']))
{
$errorMessage .= "<li>Please enter your first name.</li>";
}
$varBrand = $_POST['brandname'];
$varFName = $_POST['firstname'];
$varLName = $_POST['lastname'];
$varEmail = $_POST['email'];
$varSite = $_POST['website'];
if(empty($errorMessage))
{
$fs = fopen("mydata.csv","a");
fwrite($fs,$varBrand . ", " . $varFName . ", " . $varLName . ", " . $varEmail . ", " . $varSite . "\n");
fclose($fs);
exit;
}
}
?>
When I click Submit it successfully goes to 'thankyou.php' (which is set in the form action) but I can't figure out why it's not posting the correct error messages or filling in my 'mydata.csv' file upon click. Possibly it's a sight syntax error? Let me know if you need any more info, I know this is kind of confusing seeing as the PHP is separated from the Fiddle.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有几点需要注意:
1)使用 $_SERVER['REQUEST_METHOD'] 检查提交类型绝对可靠 - 该值始终被设置,并且如果正在执行发布,则始终为 POST。检查特定表单字段(例如提交按钮)是很麻烦且不可靠的。
2) 使用 fputcsv() 写入 csv 文件。 PHP 将为您完成所有繁重的工作,您只需向函数提供要写入的数据数组
3) 注意
or die(...)
结构,它检查打开/写入文件的失败。假设文件可用/可写是不可靠的,并且会在将来的某个时候困扰您。处理“外部”资源时,始终进行错误处理。A few things of note:
1) using $_SERVER['REQUEST_METHOD'] to check for submit type is absolutely reliable - that value is always set, and will always be POST if a post is being performed. Checking for a particular form field (e.g. the submit button) is hacky and unreliable.
2) Using fputcsv() to write out to a csv file. PHP will do all the heavy work for you and you jus tprovide the function an array of data to write
3) Note the
or die(...)
constructs, which check for failures to open/write to the file. Assuming that a file is available/writeable is unreliable and will bite you at some point in the future. When dealing with "external" resources, always have error handling.