0

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>

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 ="\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() ); } ?>

GYaN
  • 2,327
  • 4
  • 19
  • 39
Ad-vic
  • 519
  • 4
  • 18

3 Answers3

4

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)
Sabri Aziri
  • 4,084
  • 5
  • 30
  • 45
  • it removed all the error, but after clicking on send button, I am not getting any output – Ad-vic Nov 04 '13 at 21:43
  • basically nothing happens on clicking on send button, no error message as "enter name" or anything – Ad-vic Nov 04 '13 at 21:48
2

Ok so this should work

<?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>
Sabri Aziri
  • 4,084
  • 5
  • 30
  • 45
  • works cool..gives proper output. will implement the email part to make it work..thanks – Ad-vic Nov 04 '13 at 22:14
  • I am glad to heard that, just run it from server the code coz from local it won't work also make sure that your host provider support SMTP otherwise from local you can use google gmail libraries for testing, its works prefect for me, refer to this http://stackoverflow.com/questions/712392/send-email-using-gmail-smtp-server-from-php-page – Sabri Aziri Nov 04 '13 at 22:30
0

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.

George
  • 280
  • 3
  • 10