PHP表单到csv文件,但仅显示逗号

PHP表单到csv文件,但仅显示逗号,php,file,csv,export-to-csv,Php,File,Csv,Export To Csv,这是第一次需要将表单数据导出到特定文件夹中的csv文件中。表单正在运行并导出csv,但导出csv文件仅显示逗号,不显示任何信息。有没有关于如何解决这个问题的想法?如有任何建议,我们将不胜感激 <?php include($_SERVER["DOCUMENT_ROOT"] . "/includes-new/common.php"); $GLOBALS["bodyID"] = "layout21"; $GLOBALS["layout"] = " layout21"; $GLOBALS["p

这是第一次需要将表单数据导出到特定文件夹中的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();
?>
因为最可能的原因是,fwrite函数中的所有变量都不正确

例如,您有$varfname,但它似乎与$fname=$\u 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();
?>

如果你想问一下如何生成csv,你真的认为有必要显示10英里长的html和验证代码吗?如果您想将数据输出到csv文件,您不认为应该使用实际定义的变量吗?在代码中搜索$varlname并查看它出现的次数和位置…请遵循以下建议:发布的大多数代码都是错误的,因为最可能的原因是,fwrite函数中的所有变量都不正确。例如,您有$varfname,但可能正试图从$fname=$\u POST[fname]获取它;等等,或者只需将register globals打开,然后将其更改为fwrite$fs,$fname$lname$电邮$电话$首选联系人$签名。\n;它应该踢得像骡子。我会把它作为答案,但我不会把虫子当夜宵吃。但是,你说的话,我和弗林一样。