PHP表单不处理文本字段输入

时间:2014-05-29 17:41:04

标签: php wordpress forms

我在WordPress页面上有一个非常基本的注册表单,用于记录姓名,电子邮件和电话号码;然后使用我过去多次使用的PHP脚本处理输出。

提交表单时,用户将进入登录页面,发送管理员通知,并将数据转储到csv文件。问题是输入的值没有填充到它们应该的位置,因此电子邮件看起来像这样:

A new user has submitted a form:

First Name :
Last Name :
Email :
Phone : 

Sent 05/29/14, 12:18:28 PM, from 68.51.108.161 (Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.114 Safari/537.36).

同样,csv文件没有表单输入数据,只有分隔符和元数据:

"","","","","Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.114 Safari/537.36","68.51.108.161","2014-05-29","13:29:34"

根本不会发送用户电子邮件。

以下是表格:

<div class="form-body">
    <form action="http://domain.com/register.php" method="post">
        <div class="form-upper">
            <h5 class="text-white left">First</h5>
            <div class="input-holder">
                <input name="FirstName" type="text" />
            </div>
        </div>
        <div class="form-upper">
            <h5 class="text-white left">Last</h5>
            <div class="input-holder">
                <input name="LastName" type="text" />
            </div>
        </div>
        <div class="form-upper">
            <h5 class="text-white left">Email</h5>
            <div class="input-holder">
                <input name="Emails" type="text" />
            </div>
        </div>
        <div class="form-upper">
            <h5 class="text-white left">Phone</h5>
            <div class="input-holder">
                <input name="Phones" type="text" />
            </div>
        </div>
        <div id="form-button">
            <h5><button type="submit">SEND</button></h5>
        </div>
    </form>
</div>

以下是剧本:

<?php

error_reporting(E_ERROR | E_WARNING | E_PARSE);
ini_set('track_errors', true);

function DoStripSlashes($fieldValue)
{

    // temporary fix for PHP6 compatibility - magic quotes deprecated in PHP6

    if (function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc()) {
        if (is_array($fieldValue)) {
            return array_map('DoStripSlashes', $fieldValue);
        }
        else {
            return trim(stripslashes($fieldValue));
        }
    }
    else {
        return $fieldValue;
    }
}

function FilterCChars($theString)
{
    return preg_replace('/[\x00-\x1F]/', '', $theString);
}

function ProcessTABCVSField($theString, $textSeparator)
{
    if ($textSeparator == 'tab') {
        $theString = preg_replace('/\t/', ' ', $theString);
    }

    $theString = preg_replace('/\"/', '""', $theString);
    return $theString;
}

if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
    $clientIP = $_SERVER['HTTP_X_FORWARDED_FOR'];
}
else {
    $clientIP = $_SERVER['REMOTE_ADDR'];
}

$FTGFirstName = DoStripSlashes($_POST['FirstName']);
$FTGLastName = DoStripSlashes($_POST['LastName']);
$FTGEmails = DoStripSlashes($_POST['Emails']);
$FTGPhones = DoStripSlashes($_POST['Phones']);
$validationFailed = false;

// Include message in error page and dump it to the browser

if ($validationFailed === true) {
    $errorPage = '<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8" /><title>Error</title></head><body><!--VALIDATIONERROR--></body></html>';
    $errorPage = str_replace('<!--FIELDVALUE:FirstName-->', $FTGFirstName, $errorPage);
    $errorPage = str_replace('<!--FIELDVALUE:LastName-->', $FTGLastName, $errorPage);
    $errorPage = str_replace('<!--FIELDVALUE:Emails-->', $FTGEmails, $errorPage);
    $errorPage = str_replace('<!--FIELDVALUE:Phones-->', $FTGPhones, $errorPage);
    $errorList = @implode("<br />\n", $FTGErrorMessage);
    $errorPage = str_replace('<!--VALIDATIONERROR-->', $errorList, $errorPage);
    if (count(array_filter($FTGErrorMessage)) > 0) {
        foreach($FTGErrorMessage as $key => $message) {
            $ErrorMessage.= trim(str_replace("'", "\'", $message)) . '\n';
        }

        $alertJSErrorMessage = "window.alert('" . $ErrorMessage . "');";
        $onloadPattern = '/(<body[^>]+onload=[\"]*)"([^>]*)>/i';
        if (preg_match($onloadPattern, $errorPage)) {
            $replacementPattern = '\1"' . $alertJSErrorMessage . '\2>';
        }
        else {
            $onloadPattern = '/(<body[^>]*)>/i';
            $replacementPattern = '\1 onload="' . $alertJSErrorMessage . '">';
        }

        $errorPage = preg_replace($onloadPattern, $replacementPattern, $errorPage);
    }

    echo $errorPage;
}

if ($validationFailed === false) {

    // Email to Form Owner

    $emailSubject = FilterCChars("Website Registration");
    $emailBody = "A new user has submitted a form:\n" . "\n" . "First Name : $FTGFirstName\n" . "Last Name : $FTGLastName\n" . "Email : $FTGEmails\n" . "Phone : $FTGPhones\n" . "\n" . "Sent " . date('m/d/y') . ", " . date('h:i:s A') . ", from $clientIP (" . $_SERVER['HTTP_USER_AGENT'] . ").";
    $emailTo = 'Someone <myemail@foo.bar>,Another <hisemail@bar.foo>';
    $emailFrom = FilterCChars("$FTGEmails");
    $emailHeader = "From: $emailFrom\n" . "MIME-Version: 1.0\n" . "Content-type: text/plain; charset=\"ISO-8859-1\"\n" . "Content-transfer-encoding: 7bit\n";
    mail($emailTo, $emailSubject, $emailBody, $emailHeader);

    // ====================================================
    // Dump field values to a text file=
    // ====================================================

    $fileDump = 'registrations.txt';
    unset($dumpHeader);
    if (!file_exists($fileDump)) {
        $dumpHeader = sprintf("\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\"", ProcessTABCVSField('FirstName', kTextDumpFieldSeparator) , ProcessTABCVSField('LastName', kTextDumpFieldSeparator) , ProcessTABCVSField('Emails', kTextDumpFieldSeparator) , ProcessTABCVSField('Phones', kTextDumpFieldSeparator) , "HTTP_USER_AGENT", "CLIENT_IP", "DATE", "TIME");
        $dumpHeader.= "\n";
    }

    $fileHandle = @fopen($fileDump, 'a');
    if ($fileHandle === false) {
        echo '<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8" /><title>Error</title></head><body><b>Text Dump Error</b>: Cannot write to the text file: <b>' . $fileDump . '</b><br />. Script will quit now.</body></html>';
        if (ini_get('track_errors')) {
            echo '<b>PHP Error</b>: ' . $php_errormsg;
        }

        exit;
    }

    $dumpRecord = sprintf("\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\"", ProcessTABCVSField($FTGFirstName, kTextDumpFieldSeparator) , ProcessTABCVSField($FTGLastName, kTextDumpFieldSeparator) , ProcessTABCVSField($FTGEmails, kTextDumpFieldSeparator) , ProcessTABCVSField($FTGPhones, kTextDumpFieldSeparator) , $_SERVER['HTTP_USER_AGENT'], $clientIP, date('Y-m-d') , date('H:i:s'));
    $dumpRecord = str_replace("\n", "\\n", $dumpRecord);
    $dumpRecord = str_replace("\r", "\\r", $dumpRecord);
    $dumpRecord = $dumpRecord . "\n";
    if (strlen($dumpHeader) > 0) {
        fwrite($fileHandle, $dumpHeader);
    }

    fwrite($fileHandle, $dumpRecord);
    fclose($fileHandle);

    // Redirect user to success page

    header("Location: http://www.domain.com/thank-you/");
}

?>

这里有什么问题?

1 个答案:

答案 0 :(得分:0)

您的PHP没有任何问题。

我的猜测是你的表单中有错误...

而不是:

<button type="submit">SEND</button>

尝试:

<input type="submit" value="SEND">

在PHP中调试发布的值:

print_r($_POST);

并报告您获得的内容(修改前后)。