用Js验证,php和ajax发送电子邮件

时间:2013-09-16 18:17:08

标签: php ajax

我有一个简单的“联系我”表格。 用html(当然)制作,在Javascript中验证比使用Ajax请求并将其全部发送到php文件,其中表单应该再次验证而不是发送它。 (我只知道一点php,所以我希望就是这样!)

“成功”或“不提交”消息在html中并由ajax / js显示。 所以我的问题很简单:如何将身体发送到我的个人电子邮件,包括php验证? 我告诉你我的代码:

JS / AJAX:

myForm.submitForm = function(){
var formData = {
    name: myForm.name.val(),
    email: myForm.email.val(),
    text: myForm.text.val(),
};

$.ajax({
    url: 'contact.php',
    method: 'post',
    //dataType: 'json';
    success: myForm.submitSuccess,
    error: myForm.submitError
})
};

myForm.submitSuccess = function(data, textStatus, jqXHR){
console.log(data, textStatus, jqXHR);
myForm.succMess.html(data.message);
myForm.succMess.show();
};

myForm.errorSuccess = function(jqXHR, textStatus, errorThrown){
console.log(jqXHR, textStatus, errorThrown);
myForm.errorMess.html(textStatus);
myForm.errorMess.show();
};

腓:

<?php
   $nome = $_POST['name'];
   $email = $_POST['email'];
   $text = nl2br($_POST['text']);

    //mail() I guess i should use this one but how?!    

    //check if inputs are empty
if(!empty($nome) and !empty($email) and !empty($text)){
    $mail_to = "myemail@gmail.com";
    $mail_from = $email;
    $mail_subject = "Email sent by $nome";
    $mail_body = "$nome: $email. $text";

?>

非常感谢任何帮助:)

1 个答案:

答案 0 :(得分:1)

以下是我用来通过Ajax和jQuery发送邮件的方法。

HTML表单

<!DOCTYPE html>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">

$(document).ready(function(){

    $('#submit').click(function(){
        $('#success').hide(1);
        $.post("email_ajax.php", $("#contact").serialize(),  function(response) {
            $('#success').html(response);
            $('#success').show(1000);
        });
        return false;
    });
});
</script>

<style>

html {
/*    height: 100%; */

height:auto;
}
body {
    background:#000;
/*  background: url(bg.png);
    background-repeat:repeat;*/
    margin: 0px;
    padding: 0px;
    height: 100%;
    color: #fff;
    font-family: Proxima, sans-serif;;
}

#empty {
    display:block;
    clear:both;
    height:150px;
    width:auto;
    background:none;
    border:none;
}

#contact ul{
    margin-left:10px;
    list-style:none;
}

#contact ul li{
    margin-left:0px;
    list-style:none;
}
</style>
</head>

<body>
<form id="contact" action="" method="post">
<ul>
    <li>
        <label for="name">Name:</label><br>
        <input id="name" type="text" name="name"  width="250" size="35" required/>
    </li>
    <li>
        <label for="email">Email:</label><br>
        <input id="email" type="text" name="email" width="250" size="35" required/>
    </li>
<br><br>
    <li>
        <label for="message">Message:</label><br>
        <textarea id="message" name="message" rows="6" cols="40" required></textarea>
    </li>
    <li><input type="button" value=" SEND " id="submit" /><input type="reset" value="Reset" name="reset">
<div id="success" style="color: yellow;"></div></li>
</ul>
</form>
</body>
</html>

email_ajax.php

注意:$to = 'email@example.com';更改为您的电子邮件地址。

<?php
if((empty($_POST['name'])) || (empty($_POST['email'])) || (empty($_POST['message']))){
die("<b>ERROR!</b> All fields must be filled.");
}

$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$name = strtolower($name);
$name = ucwords($name);

$to = 'email@example.com';
$subject = 'Website message from: '.$name;
$message = 'FROM: '.$name." \nEmail: ".$email."\nMessage: \n".$message;
$headers = 'From: email@example.com' . "\r\n";

if (filter_var($email, FILTER_VALIDATE_EMAIL)) { 
mail($to, $subject, $message, $headers); 
echo "Thank you! Your email was sent $name.";
echo "<br>";
echo "This is the email you entered: <b>$email</b>";
}else{
// echo var_dump($_POST);
echo "<b>ERROR!</b> Invalid E-mail. Please provide a valid email address. Example: myEmail@example.com";
echo "<br>";
echo "The email <b>$email</b> you entered, is not valid.";
}

?>