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 ="nn" .
            "Name: " .ucwords($_POST['name']) ."n" .
            "Email: " .ucwords($email) ."n" .
            "Comments: " .$_POST['message'] ."n" .
            "nn" ;
            $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>

ON running it on XAMPP server, I am getting wrong output in the form of text just below send button

ERROR OUTPUT DISPLAYED

Enter name'; } if(validateEmail($email) == FALSE) { $emailerror .= ' Enter valid email '; } if(empty($_POST['message'])) { $emailerror .= ' Enter message '; } } else { $emailerror .= "Your email has been sent successfully"; // NOW SEND THE ENQUIRY $timestamp = date("F j, Y, g:ia"); $messageproper ="nn" . "Name: " . ucwords($_POST['name']) . "n" . "Email: " . ucwords($email) . "n" . "Comments: " . $_POST['message'] . "n" . "nn" ; $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() ); } ?>


好,所以这应该工作

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

Instead of using short tag for php <? ?> <? ?> user <?php ?> it should fix it.

Also notice: from local you can not send emails throw mail function and the eregi function is Deprecated and instead of it use 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)

Use full tags as previously said. Also You should declare your $emailerror variable before you append to it like:

$emailerror = "";

at the beginning of your code.

链接地址: http://www.djcxy.com/p/69332.html

上一篇: PHP MAILER:根用户从信息中取代

下一篇: 将PHP联系表单错误作为随机输出发送