繁体   English   中英

通过脚本发送电子邮件到PHP服务器端验证

[英]E-mail to PHP server side validation in script

经常使用此站点作为参考点,但确实要花一些代码。

我正在构建一个回调表单,我最初构建了客户端验证(javascript),但很快意识到我需要服务器端php验证,因此尝试将其添加到脚本中。 它的功能是正常的,除了我收到电子邮件时没有显示任何信息。 我真的不明白为什么$ name不能像我不使用验证前那样通过信息获取信息。 代码如下:

PHP:

<?php

if (isset($_POST['Form'])) {
    import_request_variables("p", "z");
    $missingfields = array();
    $required = array("Name"=>"Name", "Company"=>"Company");

    while (list($var, $val) = each($required)) {
        if (isset($zForm[$var]) && $zForm[$var] != '') {
            // check this value further here
        } else {
            $missingfields[$var] = $val;
        }
    }

    if (count($missingfields)) {
        print "You missed out one or more fields:<br />";

        while(list($var, $val) = each($missingfields)) {
            print $val . "<br />";
        print "Please click back on your browser and enter the required fields.<br />";
        }
    } else {
        /* Subject and E-mail Variables */

$emailSubject = 'Call-back Request';
$webMaster = 'hello@microsoft.com';

/* Gathering Data Variables */

$name = $_POST['Form[Name]'];
$email = $_POST['email'];
$telephone = $_POST['telephone'];
$company = $_POST['company'];
$message = $_POST['message'];

$body = <<<EOD
<br><hr><br>
Name: $name <br>
Email: $email <br>
Telephone: $telephone <br>
Company: $company <br>
Message: $message <br>
EOD;

$headers = "From: $email\r\n";
$headers .= "Content-type: text/html\r\n";
$success = mail($webMaster, $emailSubject, $body, $headers);

/* Results rendered as HTML */

$theResults = <<<EOD
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 
<head>
    <title>Thankyou</title>
    <link href="style.css" rel="stylesheet" type="text/css" media="screen" />  
</head>
<body>
    <div id="content">
        <div id="contactthankyouheader">
            <h2>Thankyou for Contacting Us</h2></br>
        </div>
        <div id="contactthankyou">
            <p>Someone will be in contact with you shortly.</p>
        </div>
        <div id="contactthankyou2">
            <p>Please click the link below to return to the homepage.     
 </p>
        </div>
        <div id="contactthankyoulink">
            <a href="index.htm"><h4>Homepage</h4></a>
        </div>
    </div>
</body>
</html>
EOD;
echo "$theResults";
    }
}

?>

HTML:

<html>
<form method="post" action="callback-function.php">
Name: <input type="text" name="Form[Name]" /> (required)<br />
Company: <input type="text" name="Form[Company]" /> (required) <br />
Age: <input type="text" name="Form[Age]" /><br /><br />
Languages known:<br />
<input type="checkbox" name="Form[Languages][]" value="PHP" checked="checked">    
PHP</input>
para><input type="checkbox" name="Form[Languages][]" value="CPP"> C++</input>
<input type="checkbox" name="Form[Languages][]" value="Delphi"> Delphi</input>
<input type="checkbox" name="Form[Languages][]" value="Java"> Java</input>
<input type="submit" />
</form>
</html>

Form中的表单作为数组发送。 因此,您应该将其视为数组:

$name = $_POST['Form']['Name'];
$company = $_POST['Form']['Company'];

并且您的表单上缺少EmailMessage ..等。

Languages也是一个数组,您可以将implode用于列表:

$Languages = implode(',', $_POST['Languages']);

暂无
暂无

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

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