dqj5046 2015-07-19 09:55
浏览 36
已采纳

获取else语句并将其添加到AJAX调用函数(响应)

I have the following code that works great except for one small thing. If an invalid email is entered the else statement of the php comes into the response and with my current styling class for the response, it looks like it was approved.

So, I was wondering if there is anywhere I can create an else statement with my current code and pull the else response from php. If so, how could I do it?

 <div id="contactMessageStatus"></div>
    <div class="contactForm">
        <form action="" method="post" id="mycontactform" >
            <input type="text" class="inputbar" name="name" placeholder="Full Name" required>
            <input type="email" class="inputbaremail" name="email" placeholder="Email" required>
            <textarea rows="4" cols="50" name="message" class="inputbarmessage" placeholder="Message" required></textarea>
            <input type="button" class="contactButton" value="Send Message" id="submit">
        </form>
    </div>

AJAX

<script>
    $(document).ready(function(){

    $('#submit').click(function(){

    $.post("contactSend.php", $("#mycontactform").serialize(),  function(response) {
    $('#contactMessageStatus').append(response);
    $('#contactMessageStatus').addClass("contactSuccess");
    $('html, body').animate({
        scrollTop: $("#contactMessageStatus").offset().top
     }, 2000);
    $('#contactMessageStatus').html(response);
    $('#contactMessageStatus').delay(5500).fadeOut(400);
    });
    return false;
    });
    });
</script>

PHP file to show the echoed responces:

<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];

$to = 'contact@example.com';
$subject = 'SFL Contact Form Submitted';
$message = 'FROM: '.$name. ' ' . ' Email: '.$email. ' ' . 'Message: '.$message;
$headers = 'From: contact@example.com' . "
";

if (filter_var($email, FILTER_VALIDATE_EMAIL)) { // this line checks that we have a valid email address
mail($to, $subject, $message, $headers); //This method sends the mail.
echo "Your email was sent!"; // success message
}else{
echo "Invalid Email, please provide an correct email.";
}
?>
  • 写回答

1条回答

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥20 boost电路升压频率和占空比选择
      • ¥15 找个人工智能的大佬咨询一些毕设问题
      • ¥15 需要教程:GD32F450I移植无系统lwip
      • ¥15 中文语料库检索构式 求助
      • ¥15 关于naver点击量信息
      • ¥15 Linux gcc升级问题
      • ¥100 如何封禁域名,让app打不开呢?
      • ¥15 通过安卓命令如何打开控制中心
      • ¥15 Nginx 配合Django 丢样式
      • ¥15 求问Python学生成绩管理系统的问题