HTML表单不输出到CSV文件(或显示正确的错误信息) [英] HTML Form Not Outputting To CSV File (Or Displaying Correct Error Messages)

查看:144
本文介绍了HTML表单不输出到CSV文件(或显示正确的错误信息)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法创建一个导出到PHP中的.CSV文件的表单。我为这里的HTML创建了一个小提琴:



http: //jsfiddle.net/tqs6g/



我使用PHP进行编码,因此无法在JSFiddle上显示完整的代码,因为它无法显示支持PHP,但这里是我的PHP代码:

 <?php 
if($ _ POST ['formSubmit'] ==Submit)
{
$ errorMessage =;

if(empty($ _ POST ['brandname']))
{
$ errorMessage。=< li>请输入一个商家/品牌名称<李>中;
}
if(empty($ _ POST ['firstname']))
{
$ errorMessage。=< li>请输入您的名字< / 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。\\\
);
fclose($ fs);
出口;
}
}
?>

当我点击提交时,它会成功转到'thankyou.php'(它在表单动作),但我不明白为什么它没有发布正确的错误消息或点击时填写我的'mydata.csv'文件。可能这是一个视觉语法错误?让我知道如果你需要更多的信息,我知道这是有点混淆,因为PHP从小提琴中分离出来。 解决方案

$ {code><?php

if($ _SERVER ['REQUEST_METHOD'] =='POST'){//更好的方法检查POSt
...验证的东西...
$ data = array();
$ data [] = $ _POST ['brandname'];
$ data [] = $ _POST ['firstname'];
等...
if(empty($ errrorMessage)){
$ fs = fopen('mydata.csv','a')或die(无法打开输出文件);
fputcsv($ fs,$ data)或die(无法写入文件);
fclose($ fs);
exit();
} else {
echo $ errormessage;


一些注意事项:



1)使用$ _SERVER ['REQUEST_METHOD']检查提交类型是否绝对可靠 - 该值始终设置,并且如果正在执行帖子,则始终为POST。检查一个特定的表单域(例如提交按钮)是hacky和unreliable。

2)使用fputcsv()写出一个csv文件。 PHP将为您完成所有繁重的工作,并且您提供了一个数组数组来写入数据的函数

3)注意或die(...)构造,它检查打开/写入文件的失败。假设文件可用/可写是不可靠的,并且在将来的某个时刻会咬你。处理外部资源时,总是有错误处理。


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:

http://jsfiddle.net/tqs6g/

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.

解决方案

<?php

if ($_SERVER['REQUEST_METHOD'] == 'POST') { // better method to check for a POSt
    ... validation stuff ...
    $data = array();
    $data[] = $_POST['brandname'];
    $data[] = $_POST['firstname'];
    etc...
    if (empty($errrorMessage)) {
        $fs = fopen('mydata.csv', 'a') or die("Unable to open file for output");
        fputcsv($fs, $data) or die("Unable to write to file");
        fclose($fs);
        exit();
    } else {
       echo $errormessage;
    }
}

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.

这篇关于HTML表单不输出到CSV文件(或显示正确的错误信息)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆