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条回答 默认 最新

  • duanmei4362 2015-07-19 10:06
    关注

    based on comments in question:

    $(document).ready(function(){
    
        $('#submit').click(function(){
    
            $.post("contactSend.php", $("#mycontactform").serialize(),  function(response) {
                if (response == 'Your email was sent!') {
                    $('#contactMessageStatus').append(response);
                    $('#contactMessageStatus').addClass("contactSuccess");
                    $('html, body').animate({
                        scrollTop: $("#contactMessageStatus").offset().top
                     }, 2000);
                    $('#contactMessageStatus').html(response);
                    $('#contactMessageStatus').delay(5500).fadeOut(400);
                }
                else {
                    // whatever error handling you want goes here
                }
            });
            return false;
        });
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 蓝桥oj3931,请问我错在哪里
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染