AJAX页面不符合成功标准... JSON返回类型错误....无论如何,始终显示“有错误”

时间:2014-10-14 14:39:14

标签: php jquery ajax json

**大家好。我创建了一个AJAX页面,用于验证表单并显示错误。但问题是无论它总是显示什么都有错误**

联系表单:

    <form id="contact-form" method="post" action="">
  <fieldset>
    <label for="name" class="fullname">
        <span>Full name:</span>
        <input type="text" name="fullname" id="fullname">
    </label>
    <label for="email" class="email">
      <span>Email:</span>
      <input type="email" name="email" id="email">
    </label>
    <label for="name1" class="message">
      <span>Message:</span> 
      <textarea name="name1" id="name1"></textarea>
    </label>
    <div class="btns">
        <a class="button" onClick="document.getElementById('contact-form').reset()">Clear</a>
        <a class="button" name="submit" id="submit">Send</a>                                                
    </div>            
</form>

我写的AJAX程序是

<script>
    $(document).ready(function(){           
        $('#submit').click(function() {  
            $('#contact-form').hide(0);
            $('#message').hide(0);              
            $.ajax({
                type : 'POST',
                url : 'post.php',
                dataType : 'json',
                data: {
                    email : $('#email').val(),
                    name1 : $('#name1').val(),
                    name  : $('#fullname').val()
                },
                success : function(data){
                    $('#waiting').hide(500);
                    $('#message').removeClass().addClass((data.error === true) ? 'error' : 'success')
                        .text(data.msg).show(500);
                    if (data.error === true)
                        $('#contact-form').show(500);
                },
                error : function(XMLHttpRequest, textStatus, errorThrown) {
                    $('#waiting').hide(500);
                    $('#message').removeClass().addClass('error')
                        .text('There was an error.').show(500);
                    $('#contact-form').show(500);
                }
            });             
            return false;
        });
    });

</script>

最后验证的PHP代码是

<?php
    $return['error'] = false;        
    while (true) {
        if (empty($_POST['email'])) {
            $return['error'] = true;
            $return['msg'] = 'You did not enter you email.';
            break;
        }        
        if (empty($_POST['name'])) {
            $return['error'] = true;
            $return['msg'] = 'You did not enter you name.';
            break;
        }        
        if (empty($_POST['name1'])) {
            $return['error'] = true;
            $return['msg'] = 'You did not enter you message.';
            break;
        }        
        break;
    }        
    if (!$return['error'])
        $return['msg'] = 'You\'ve entered: ' . $_POST['email'] . ' as email, ' . $_POST['name'] . ' as name and ' . $_POST['email'] . ' as url.';
     echo json_encode($return);
?>

如果添加了用于电子邮件检查和长度检查的额外验证器,那将会很有帮助。谢谢提前购买。

1 个答案:

答案 0 :(得分:0)

更改输入类型文本或编写valide电子邮件

  <input ng-model="t.unsubscribeEmail" type="text" class="textboxfields" placeholder="Enter email address">

或在

中写下valide电子邮件
   <input ng-model="t.unsubscribeEmail" type="email" class="textboxfields" placeholder="Enter email address">

use this link