【问题标题】:PHP form to csv file but showing only commasPHP表单到csv文件但只显示逗号
【发布时间】:2014-03-06 02:06:42
【问题描述】:

这是第一次需要将表单数据导出为特定文件夹中的 csv 文件。表单正在运行并导出 csv,但导出的 csv 文件仅显示逗号,没有任何信息。关于如何解决这个问题的任何想法?非常感谢任何建议。

<?php
include($_SERVER["DOCUMENT_ROOT"] . "/includes-new/common.php");

$GLOBALS["bodyID"] = "layout21";
$GLOBALS["layout"] = " layout21";

$GLOBALS["pageTitle"] = "info";
$GLOBALS["tab"] = "info";
$GLOBALS["section"] = "info";
$GLOBALS["page"] = "default";

$query = strip_tags($_GET["query"]);
$form  = "document.contact.";
$focus = ".focus();";



if($query != null){

    if(is_numeric($query)){
        $zip = strtoupper(substr(trim($query), 0, 5));
    } else {
        $pieces = explode(",", $query);
        $city = ucwords(trim($pieces[0]));
        $state = strtoupper(substr(trim($pieces[1]), 0, 2));
    }

} else {

    $city = ucwords(strip_tags($_GET["city"]));
    $state = strtoupper(substr(strip_tags($_GET["state"]), 0, 2));
    $zip = strtoupper(substr(strip_tags($_GET["zip"]), 0, 5));

}


if($_POST["Submit"]){

    $fname = $_POST["fname"];                       //required
    $lname = $_POST["lname"];                       //required
    $email = strtolower($_POST["email"]);           //required
    $phone = $_POST["phone"];                       //required
    $preferredContact = $_POST["preferredContact"]; //required
    $signature = $_POST["signature"];                       //required



    if(!$fname){
        $errorField = "fname";
        $requiredFields[] = $errorField;
        $errorMessage[] = "Please enter your first name";
        $errorOnload[] = $form . $errorField . $focus;
    }

    if(!$lname){
        $errorField = "lname";
        $requiredFields[] = $errorField;
        $errorMessage[] = "Please enter your last name";
        $errorOnload[] = $form . $errorField . $focus;
    }

    if(!$email){
        $errorField = "email";
        $requiredFields[] = $errorField;
        $errorMessage[] = "Please enter your email address";
        $errorOnload[] = $form . $errorField . $focus;
    } else {
        if(!validate_email($email)){
            $errorField = "email";
            $requiredFields[] = $errorField;
            $errorMessage[] = "Please enter a valid email address";
            $errorOnload[] = $form . $errorField . $focus;
        }
    }

    if(!$phone){
        $errorField = "phone";
        $requiredFields[] = $errorField;
        $errorMessage[] = "Please enter your phone number";
        $errorOnload[] = $form . $errorField . $focus;
    } else {
        if(strlen($phone) < 10){
            $errorField = "phone";
            $requiredFields[] = $errorField;
            $errorMessage[] = "Please enter a valid phone number with area code";
            $errorOnload[] = $form . $errorField . $focus;
        }
    }

    if($preferredContact == "null"){
        $errorField = "preferredContact";
        $requiredFields[] = $errorField;
        $errorMessage[] = "Please select your preferred contact method";
        $errorOnload[] = $form . $errorField . $focus;
    }

    if(!$signature){
        $errorField = "signature";
        $requiredFields[] = $errorField;
        $errorMessage[] = "Please enter your full name";
        $errorOnload[] = $form . $errorField . $focus;
    }

    if ($errorField == null) {

        if (isCaptchaValid() == false) {
            // the security code was incorrect
            $errorField = CAPTCHA_ID;
            $requiredFields[] = $errorField;
            $errorMessage[] = "Please enter a valid security code";
            $errorOnload[] = $form . $errorField . $focus;
        }

    }

    if($errorField == null){

        $to_name = "email@me.com";
        $to_email = "email@me.com";
        $subject = "some subject";

        $interestedProducts = "";
        while(list($key,$val) = @each($products)){
            $interestedProducts .= "$val, ";
        }

        if($interestedProducts != null){
            $interestedProducts = substr($interestedProducts, 0, -2);
        }

        $message = "";
        $message .= "some message sent\n\n";
        // $message .= "Current Customer:  $customer\n";
        $message .= "First Name:  $fname\n";
        $message .= "Last Name:  $lname\n";
        $message .= "Email Address:  $email\n";
        $message .= "Phone:  $phone\n";
        $message .= "Preferred Contact Method:  $preferredContact\n";
                //$message .= "\n\n--------------------------------------------------------------------------------------------\nThis message was submitted through the website";

        send_email($to_name, $to_email, "$fname $lname", $email, "Website -  " . $subject, $message, $today, "false", "false");


        $fname = "";
        $lname = "";
        $email = "";
        $phone = "";
        $preferredContact = "";
        $signature = "";


        // Unique "Thank you" page.
        header("Location: thanks.php");
        exit;
    }

} else {
    $errorOnload[] = $form . "customer" . $focus;
}

if($GLOBALS["focusRequiredField"]){
    $GLOBALS["onload"] = $errorOnload[0];

}

getTemplateHeader();
getTemplateColumn("1", "start");
getTemplateSidebar("default");

?>

<h1>
    <?php echo $GLOBALS["pageTitle"]; ?>
</h1>

<? echo displayErrorMsg($errorMessage); ?>
<? echo displaySuccessMsg($successMessage); ?>

<p>
    body copy</p>

<p><strong>copy</strong></p>

<p>You have selected the <strong><?php echo getVisitorCity(); ?></strong> location.</p>

<div class="form-container">
    <form action="<? echo $GLOBALS["self"]; ?>" name="contact"
        method="post">
        <fieldset>

            <h4 style="margin-top: 15px;">Contact Information</h4>

            <div>
                <label for="fname"
                <? checkRequiredField("fname", $requiredFields); ?>>First Name: <em>*</em>
                </label> <input id="fname" type="text" name="fname"
                    value="<? echo $fname; ?>" size="30" maxlength="75"
                    <? checkRequiredField("fname", $requiredFields); ?> />
            </div>

            <div>
                <label for="lname"
                <? checkRequiredField("lname", $requiredFields); ?>>Last Name: <em>*</em>
                </label> <input id="lname" type="text" name="lname"
                    value="<? echo $lname; ?>" size="30" maxlength="75"
                    <? checkRequiredField("lname", $requiredFields); ?> />
            </div>

            <div>
                <label for="email"
                <? checkRequiredField("email", $requiredFields); ?>>Email Address:
                    <em>*</em>
                </label> <input id="email" type="text" name="email"
                    value="<? echo $email; ?>" size="30" maxlength="75"
                    <? checkRequiredField("email", $requiredFields); ?> />
            </div>

            <div>
                <label for="phone"
                <? checkRequiredField("phone", $requiredFields); ?>>Phone Number: <em>*</em>
                </label> <input id="phone" type="text" name="phone"
                    value="<? echo $phone; ?>" size="20" maxlength="20"
                    <? checkRequiredField("phone", $requiredFields); ?> />
            </div>

            <div>
                <label for="preferredContact"
                <? checkRequiredField("preferredContact", $requiredFields); ?>>Preferred
                    Contact: <em>*</em>
                </label> <select name="preferredContact" id="preferredContact"
                <? checkRequiredField("preferredContact", $requiredFields); ?>>
                    <option value="null"
                    <? if($preferredContact == "null"){ echo ' selected="selected"'; } ?>>[Select
                        Contact Method]</option>
                    <option value="Email"
                    <? if($preferredContact == "Email"){ echo ' selected="selected"'; } ?>>Email</option>
                    <option value="Phone"
                    <? if($preferredContact == "Phone"){ echo ' selected="selected"'; } ?>>Phone</option>
                </select>
            </div>


            <h4 style="margin-top: 15px;">Sign here using electronic signature</h4>

            <div>
                <label for="signature"
                <? checkRequiredField("signature", $requiredFields); ?>>Full Name: <em>*</em>
                </label> <input id="signature" type="text" name="signature" alt="i.e. Jane Marie Doe"
                    value="<? echo $signature; ?>" size="50" maxlength="75"
                    <? checkRequiredField("signature", $requiredFields); ?> />
            </div>




            <p>&nbsp;</p>
            <?php echo displayCaptcha($requiredFields); ?>
            <p>&nbsp;</p>

            <div>
                <label class="nopointer">&nbsp;</label> <input type="submit"
                    name="Submit" value="Submit Inquiry"
                    onclick="this.value='<? echo $GLOBALS["processing_text"]; ?>';" />
            </div>

        <?php   if(count($errorMessage) > 0)
{
    $numErrors = count($errorMessage); 
    echo("<p>There was " . $numErrors . "an error:</p>\n");
    echo("<ul>" . $errorMessage . "</ul>\n");
}
else
{
    $fs = fopen("/tmp/mydata.csv","a");
    fwrite($fs,$varfname . ", " . $varlname . ", " . $varemail . ", " . $varphone . ", " . $varpreferredContact . ", " . $varsignature ."\n");
    fclose($fs);

    exit;
}
?>


            <? echo buildRequiredMessage(); ?>

        </fieldset>
    </form>
</div>

<?php
$GLOBALS["disclosures"][] = array("", "");
getTemplateColumn("1", "end");

getTemplateNavigation($GLOBALS["tab"]);
getTemplateFooter();
?>

【问题讨论】:

  • 如果你要问生成csv,你真的认为有必要展示10英里的html和验证码吗?而且如果你想将数据输出到一个csv文件,你不认为你应该使用实际定义的变量吗?在你的代码中搜索$varlname,看看它出现了多少次(以及在哪里)......
  • 请遵循此建议:stackoverflow.com/help/mcve 发布的大部分代码都是错误的
  • 因为(最可能的原因),fwrite() 函数中的所有变量都不正确。例如,您有$varfname,但您可能正试图从$fname = $_POST["fname"]; 等处获取它。
  • 或者只是打开寄存器全局变量(傻笑)
  • 将其更改为fwrite($fs,$fname . ", " . $lname . ", " . $email . ", " . $phone . ", " . $preferredContact . ", " . $signature ."\n");,它应该像骡子一样踢。我会把它作为答案,但我没有将蠕虫作为深夜小吃。但是,你说出这个词,我就像弗林一样。

标签: php file csv export-to-csv


【解决方案1】:

因为(也是最可能的原因),fwrite() 函数中的所有变量都不正确。

例如,您有$varfname,但它似乎与$fname = $_POST["fname"]; 等有关。

改成:

fwrite($fs,$fname . ", " . $lname . ", " . $email . ", " . $phone . ", " . $preferredContact . ", " . $signature ."\n");

编辑(已测试)

为了保留它们原来的位置,有些部分已经用/*...*/注释掉了,并且被放在了别处。

fwrite()函数已复制到mail()函数下,其他已注释掉。

以下内容对我有用,包括我原始答案的一部分。

<?php
include($_SERVER["DOCUMENT_ROOT"] . "/includes-new/common.php");

$GLOBALS["bodyID"] = "layout21";
$GLOBALS["layout"] = " layout21";

$GLOBALS["pageTitle"] = "info";
$GLOBALS["tab"] = "info";
$GLOBALS["section"] = "info";
$GLOBALS["page"] = "default";

$query = strip_tags($_GET["query"]);
$form  = "document.contact.";
$focus = ".focus();";

if($query != null){

    if(is_numeric($query)){
        $zip = strtoupper(substr(trim($query), 0, 5));
    } else {
        $pieces = explode(",", $query);
        $city = ucwords(trim($pieces[0]));
        $state = strtoupper(substr(trim($pieces[1]), 0, 2));
    }

} else {

    $city = ucwords(strip_tags($_GET["city"]));
    $state = strtoupper(substr(strip_tags($_GET["state"]), 0, 2));
    $zip = strtoupper(substr(strip_tags($_GET["zip"]), 0, 5));

}


if($_POST["Submit"]){

    $fname = $_POST["fname"];                       //required
    $lname = $_POST["lname"];                       //required
    $email = strtolower($_POST["email"]);           //required
    $phone = $_POST["phone"];                       //required
    $preferredContact = $_POST["preferredContact"]; //required
    $signature = $_POST["signature"];                       //required

    if(!$fname){
        $errorField = "fname";
        $requiredFields[] = $errorField;
        $errorMessage[] = "Please enter your first name";
        $errorOnload[] = $form . $errorField . $focus;
    }

    if(!$lname){
        $errorField = "lname";
        $requiredFields[] = $errorField;
        $errorMessage[] = "Please enter your last name";
        $errorOnload[] = $form . $errorField . $focus;
    }

    if(!$email){
        $errorField = "email";
        $requiredFields[] = $errorField;
        $errorMessage[] = "Please enter your email address";
        $errorOnload[] = $form . $errorField . $focus;
    } else {
        if(!validate_email($email)){
            $errorField = "email";
            $requiredFields[] = $errorField;
            $errorMessage[] = "Please enter a valid email address";
            $errorOnload[] = $form . $errorField . $focus;
        }
    }

    if(!$phone){
        $errorField = "phone";
        $requiredFields[] = $errorField;
        $errorMessage[] = "Please enter your phone number";
        $errorOnload[] = $form . $errorField . $focus;
    } else {
        if(strlen($phone) < 10){
            $errorField = "phone";
            $requiredFields[] = $errorField;
            $errorMessage[] = "Please enter a valid phone number with area code";
            $errorOnload[] = $form . $errorField . $focus;
        }
    }

    if($preferredContact == "null"){
        $errorField = "preferredContact";
        $requiredFields[] = $errorField;
        $errorMessage[] = "Please select your preferred contact method";
        $errorOnload[] = $form . $errorField . $focus;
    }

    if(!$signature){
        $errorField = "signature";
        $requiredFields[] = $errorField;
        $errorMessage[] = "Please enter your full name";
        $errorOnload[] = $form . $errorField . $focus;
    }

    if ($errorField == null) {

        if (isCaptchaValid() == false) {
            // the security code was incorrect
            $errorField = CAPTCHA_ID;
            $requiredFields[] = $errorField;
            $errorMessage[] = "Please enter a valid security code";
            $errorOnload[] = $form . $errorField . $focus;
        }

    }

    if($errorField == null){

        $to_name = "email@me.com";
        $to_email = "email@me.com";
        $subject = "some subject";

        $interestedProducts = "";
        while(list($key,$val) = @each($products)){
            $interestedProducts .= "$val, ";
        }

        if($interestedProducts != null){
            $interestedProducts = substr($interestedProducts, 0, -2);
        }

        $message = "";
        $message .= "some message sent\n\n";
        // $message .= "Current Customer:  $customer\n";
        $message .= "First Name:  $fname\n";
        $message .= "Last Name:  $lname\n";
        $message .= "Email Address:  $email\n";
        $message .= "Phone:  $phone\n";
        $message .= "Preferred Contact Method:  $preferredContact\n";
                //$message .= "\n\n--------------------------------------------------------------------------------------------\nThis message was submitted through the website";

        send_email($to_name, $to_email, "$fname $lname", $email, "Website -  " . $subject, $message, $today, "false", "false");
        
        $fs = fopen("/tmp/mydata.csv","a");
        fwrite($fs,$varfname . ", " . $varlname . ", " . $varemail . ", " . $varphone . ", " . $varpreferredContact . ", " . $varsignature ."\n");
        fclose($fs);

    // exit;

/*
        $fname = "";
        $lname = "";
        $email = "";
        $phone = "";
        $preferredContact = "";
        $signature = "";
*/

        // Unique "Thank you" page.
        header("Location: thanks.php");
        exit;
    }

} else {
    $errorOnload[] = $form . "customer" . $focus;
}

if($GLOBALS["focusRequiredField"]){
    $GLOBALS["onload"] = $errorOnload[0];

}

getTemplateHeader();
getTemplateColumn("1", "start");
getTemplateSidebar("default");

?>

<h1>
    <?php echo $GLOBALS["pageTitle"]; ?>
</h1>

<? echo displayErrorMsg($errorMessage); ?>
<? echo displaySuccessMsg($successMessage); ?>

<p>
    body copy</p>

<p><strong>copy</strong></p>

<p>You have selected the <strong><?php echo getVisitorCity(); ?></strong> location.</p>

<div class="form-container">
    <form action="<? echo $GLOBALS["self"]; ?>" name="contact"
        method="post">
        <fieldset>

            <h4 style="margin-top: 15px;">Contact Information</h4>

            <div>
                <label for="fname"
                <? checkRequiredField("fname", $requiredFields); ?>>First Name: <em>*</em>
                </label> <input id="fname" type="text" name="fname"
                    value="<? echo $fname; ?>" size="30" maxlength="75"
                    <? checkRequiredField("fname", $requiredFields); ?> />
            </div>

            <div>
                <label for="lname"
                <? checkRequiredField("lname", $requiredFields); ?>>Last Name: <em>*</em>
                </label> <input id="lname" type="text" name="lname"
                    value="<? echo $lname; ?>" size="30" maxlength="75"
                    <? checkRequiredField("lname", $requiredFields); ?> />
            </div>

            <div>
                <label for="email"
                <? checkRequiredField("email", $requiredFields); ?>>Email Address:
                    <em>*</em>
                </label> <input id="email" type="text" name="email"
                    value="<? echo $email; ?>" size="30" maxlength="75"
                    <? checkRequiredField("email", $requiredFields); ?> />
            </div>

            <div>
                <label for="phone"
                <? checkRequiredField("phone", $requiredFields); ?>>Phone Number: <em>*</em>
                </label> <input id="phone" type="text" name="phone"
                    value="<? echo $phone; ?>" size="20" maxlength="20"
                    <? checkRequiredField("phone", $requiredFields); ?> />
            </div>

            <div>
                <label for="preferredContact"
                <? checkRequiredField("preferredContact", $requiredFields); ?>>Preferred
                    Contact: <em>*</em>
                </label> <select name="preferredContact" id="preferredContact"
                <? checkRequiredField("preferredContact", $requiredFields); ?>>
                    <option value="null"
                    <? if($preferredContact == "null"){ echo ' selected="selected"'; } ?>>[Select
                        Contact Method]</option>
                    <option value="Email"
                    <? if($preferredContact == "Email"){ echo ' selected="selected"'; } ?>>Email</option>
                    <option value="Phone"
                    <? if($preferredContact == "Phone"){ echo ' selected="selected"'; } ?>>Phone</option>
                </select>
            </div>


            <h4 style="margin-top: 15px;">Sign here using electronic signature</h4>

            <div>
                <label for="signature"
                <? checkRequiredField("signature", $requiredFields); ?>>Full Name: <em>*</em>
                </label> <input id="signature" type="text" name="signature" alt="i.e. Jane Marie Doe"
                    value="<? echo $signature; ?>" size="50" maxlength="75"
                    <? checkRequiredField("signature", $requiredFields); ?> />
            </div>




            <p>&nbsp;</p>
            <?php echo displayCaptcha($requiredFields); ?>
            <p>&nbsp;</p>

            <div>
                <label class="nopointer">&nbsp;</label> <input type="submit"
                    name="Submit" value="Submit Inquiry"
                    onclick="this.value='<? echo $GLOBALS["processing_text"]; ?>';" />
            </div>

        <?php   if(count($errorMessage) > 0)
{
    $numErrors = count($errorMessage); 
    echo("<p>There was " . $numErrors . "an error:</p>\n");
    echo("<ul>" . $errorMessage . "</ul>\n");
}


/* original placement for fwrite() function commented out */

/*
else
{
    $fs = fopen("/tmp/mydata.csv","a");
    fwrite($fs,$varfname . ", " . $varlname . ", " . $varemail . ", " . $varphone . ", " . $varpreferredContact . ", " . $varsignature ."\n");
    fclose($fs);

    exit;
}
*/
?>


            <? echo buildRequiredMessage(); ?>

        </fieldset>
    </form>
</div>

<?php
$GLOBALS["disclosures"][] = array("", "");
getTemplateColumn("1", "end");

getTemplateNavigation($GLOBALS["tab"]);
getTemplateFooter();
?>

【讨论】:

    猜你喜欢
    • 2013-01-24
    • 1970-01-01
    • 1970-01-01
    • 2017-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-08
    相关资源
    最近更新 更多