繁体   English   中英

通过电子邮件将PHP联系人表单错误作为随机输出

[英]Email PHP Contact Form error as random output

HTML

<div id="formwrap">
    <form method="post" id="submitform" action="submitemail.php" >
        <input type="text" class="formstyle" title="Name" name="name" />
        <input type="text" class="formstyle" title="Email" name="email" />
        <textarea name="message" title="Message"></textarea>
        <input class="formstyletwo" type="submit" value="Send">  
    </form>

PHP

<?php
    $mailto = "abcd@gmail.com"; /*PUT YOUR EMAIL IN HERE*/
    $cc = "";
    $bcc = "";
    $subject = "enquiry";
    $vname = "Website Contact Form";
    $email = $_POST['email'];
    function validateEmail($email){
        if(eregi('^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+\.[a-zA-Z]{2,4}(\.[a-zA-Z]{2,3})?(\.[a-zA-Z]{2,3})?$', $email)) return true;
        else return false;
    }
    if(strlen($_POST['name']) < 1  || strlen($_POST['message']) < 1 || validateEmail($email) == FALSE){
        if(empty($_POST['name'])){
            $emailerror .= '<li>Enter name</li>';
        }
        if(validateEmail($email) == FALSE){
            $emailerror .= '<li>Enter valid email</li>';
        }
        if(empty($_POST['message'])){
            $emailerror .= '<li>Enter message</li>';
        }
    }
    else{
        $emailerror .= "Your email has been sent successfully";
        // NOW SEND THE ENQUIRY
        $timestamp = date("F j, Y, g:ia");
        $messageproper ="\n\n" .
            "Name: " .ucwords($_POST['name']) ."\n" .
            "Email: " .ucwords($email) ."\n" .
            "Comments: " .$_POST['message'] ."\n" .
            "\n\n" ;
            $messageproper = trim(stripslashes($messageproper));
            mail($mailto, $subject, $messageproper, "From: \"$vname\" <".$_POST['e_mail'].">\nReply-To: \"".ucwords($_POST['first_name'])."\" <".$_POST['e_mail'].">\nX-Mailer: PHP/" . phpversion() );
    }
?>

<div id='emailerror'>
    <ul>
        <? echo $emailerror; ?>
    </ul>
</div>

在XAMPP服务器上运行它时,以发送按钮正下方的文本形式显示错误的输出

显示错误输出

输入名字'; } if(validateEmail($ email)== FALSE){$ emailerror。='输入有效的电子邮件'; } if(empty($ _ POST ['message'])){$ emailerror。='输入邮件'; }}其他{$ emailerror。=“您的电子邮件已成功发送”; //现在发送查询$ timestamp = date(“ F j,Y,g​​:ia”); $ messageproper =“ \\ n \\ n”。 “名称: ” 。 ucwords($ _ POST ['name'])。 “ \\ n”。 “电子邮件:”。 ucwords($ email)。 “ \\ n”。 “评论: ” 。 $ _POST ['message']。 “ \\ n”。 “ \\ n \\ n”; $ messageproper = trim(stripslashes($ messageproper)); mail($ mailto,$ subject,$ messageproper,“发件人:\\” $ vname \\“ <”。$ _ POST ['e_mail']。“> \\ nReply-To:\\”“。ucwords($ _ POST ['first_name' ])。“ \\” <“。$ _ POST ['e_mail']。”> \\ nX-Mailer:PHP /“。phpversion()); }?>

而不是对php使用短标签<? ?> <? ?>用户<?php ?>它应该修复它。

另请注意:从本地无法发送电子邮件时抛出邮件功能,并且eregi功能已弃用,请使用preg_match代替

preg_match('%^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+\.[a-zA-Z]{2,4}(\.[a-zA-Z]{2,3})?(\.[a-zA-Z]{2,3})?$%', $email)

好吧,这应该工作

<?php
$mailto = "abcd@gmail.com"; /*PUT YOUR EMAIL IN HERE*/
$cc = "";
$bcc = "";
$subject = "enquiry";
$vname = "Website Contact Form";


$errors = array();
if($_POST){

    if(!isset($_POST['name']) || $_POST['name'] == "")
        $errors[] = "Please enter your name.";

    if(isset($_POST['email']) && $_POST['email'] != ""){
        if(!preg_match('%^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+\.[a-zA-Z]{2,4}(\.[a-zA-Z]{2,3})?(\.[a-zA-Z]{2,3})?$%', $_POST['email']))
            $errors[] = "Not valid email";
    }else
        $errors[] = "Please enter your email";

    if(!isset($_POST['message']) || $_POST['message'] =="" ){
        $errors[] = "Please enter the message.";
    }

    if(!$errors){
        //send here your email...
        echo "email sended";
    }
}



?>
 <div id='emailerror'>
    <ul>
        <?php 
            foreach($errors as $err){
                echo "<li>$err</li>";
            }
        ?>
    </ul>
</div>

如前所述,使用完整标签。 另外,在附加到$ emailerror变量之前,您应该声明它,例如:

$emailerror = "";

在代码的开头。

暂无
暂无

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

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