1

I'm pretty new to using php and I'm currently trying to set up a personal site. I've got a contact form in the page at the bottom that I can't seem to get to work and send myself an email of the message the client writes. The form has the following html:

    <form method="post">
                <div class="form-group">
                    <label for="name">Your name:</label>
                    <input type="text" id="name" class="form-control" placeholder="Your name" />

                </div>
                <div class="form-group">
                    <label for="email">Your email:</label>
                    <input type="email" id="email" class="form-control"   placeholder="Your email"/>

                </div>
                <div class="form-group">
                    <label for="comment">Message</label>
                    <textarea class="form-control" id="comment"></textarea>

                </div>
                <input type="button" class="btn btn-warning" value="submit" id="sendEmail"/>
            </form>

and I'm using the to following JavaScript to put the form into a php file and send out an email to myself. Here in the JavaScript:

<script>
$(".contentContainer").css("min-height",$(window).height());

$("result").hide();

$("#sendEmail").click(function(){

    var name = $("#name").val();
    var email = $("#email").val();
    var comment = $("#comment").val();

    $.post("validate.php",
    {postname:name,postemail:email,postcomment:comment},
    function(data){

        $("#result").html(data).fadeIn();
    });
});

</script>

and here is the php file that I'm using:

    <?php
$error = NULL;
    if (!$_POST['postname']) {
        $error="<br />Please enter your name";
    }
    if (!$_POST['postemail']) {
        $error.="<br />Please enter your email address";
    }
    if (!$_POST['postcomment']) {
        $error.="<br />Please enter a comment";
    }
    if ($_POST['postemail']!="" AND !filter_var($_POST['postemail'], FILTER_VALIDATE_EMAIL)) { 
        $error.="<br />Please enter a valid email address";
    }
    if($error !=NULL){
        $result='<div class="alert alert-danger"><strong>There were error(s)
        in your form:</strong>'.$error.'</div>';
    } else{
        if (mail("PERSONAL EMAIL", "Comment from website", "Name: ".$_POST['postname ']."
        Email: ".$_POST['postemail']." 
        Comment: ".$_POST['postcomment'])) {

            //echo $_POST['postname']

            $result='thank you!';
        } else{
            $result='There was an error sending your message! Try again later!';
        }
    }
?>

A couple of things I've noticed: when I replace the 'PERSONAL EMAIL', I don't receive an email when I test out the contact form on my website. However, when I uncomment the echo $_POST['postname'] right after the mail statement (I interpreted it as mail=true so mail sent) then the variable echos back to my website. However, the $result variable doesn't appear in the #result div like I intend. What am I dong wrong? Is there an error I've made that I need to know for future reference? This concept between html <-> php is still kind of new to me!

abrhoda
  • 11
  • 2

3 Answers3

2

First of all when mail function returns TRUE it does mean only that a message was accepted for delivery. It does not mean that it was or will be delivered or even sent out of server. I would also check SPAM folder because it's pretty often that this method of sending mail from php script lands you into spam. It just does not use SMTP and real box to send out mails.

I would reccomend using PHPMailer to send a message through real mail box. It also gives you some information about errors while sending email.

https://github.com/PHPMailer/PHPMailer

Second problem is that you don't echo back $result. That's why it is not sent to JS.

Zbyszek
  • 1,420
  • 18
  • 23
0

First, you have to write a preventDefault() sentence in your jQuery code to attend the click on the button sendEmail, otherwise, the script make a form submit.

Is a good practice validate emails, etc, etc in the client side, using jQuery.

Another good practice is make secure that you are indexing righ values on $_POST variables, using isset($_POST['name']) in PHP.

dennitorf
  • 309
  • 1
  • 4
  • 13
  • Input with type="button" should not submit form. A – Zbyszek Sep 12 '15 at 03:53
0

You have a syntax error on email line: you should change AND for &&. And instead of using hardcoded post data, you should use $("#formId").serialize(), for data value.

DirtyBit
  • 16,613
  • 4
  • 34
  • 55
Luís Assunção
  • 771
  • 1
  • 8
  • 20
  • In this case AND is fine. The difference between AND and && is that && has higher precedence than = but AND does not. So $a = $b && $c is not the same as $a = $b AND $c, in the latter it's like ($a = $b) && $c. – Zbyszek Sep 12 '15 at 03:56