简体   繁体   English

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

[英]PHP Form Not Processing Text Field Inputs

I have a pretty basic signup form on WordPress page that captures a name, email, and phone number; 我在WordPress页面上有一个非常基本的注册表单,其中包含姓名,电子邮件和电话号码; and then process the output using a PHP script that I've used several times in the past. 然后使用我过去使用过几次的PHP脚本处理输出。

When submitting the form, the user is taken to the landing page, the admin notification is sent, and the data is dumped to the csv file. 提交表单时,用户将被带到登录页面,发送了管理通知,并将数据转储到csv文件中。 The problem is that the inputted values are not populating where they are supposed to, so the email looks like this: 问题在于输入的值没有填充到应有的位置,因此电子邮件如下所示:

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).

Likewise, the csv file has no form input data, just the separators and the meta data: 同样,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"

The user email is not sent out at all. 完全不发送用户电子邮件。

Here is the form: 形式如下:

<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>

And here's the script: 这是脚本:

<?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/");
}

?>

What is the problem here? 这里有什么问题?

Nothing is wrong with your PHP. 您的PHP没问题。

My guess is that there is an error in your form... 我的猜测是您的表格有误...

Instead of: 代替:

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

Try: 尝试:

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

And in PHP debug the posted values with: 在PHP中,使用以下命令调试发布的值:

print_r($_POST);

and report what you get (before and after the modification). 并报告您所得到的(修改前后)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM