1

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

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

JS/阿贾克斯:

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:

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

?>

非常感谢任何帮助:)

4

1 回答 1

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.";
}

?>
于 2013-09-16T20:51:37.210 回答