Ajax验证发布错误

时间:2014-02-10 10:26:35

标签: javascript php jquery ajax

 <?php
   sleep(2);
   ob_start();

    $CategoryName = secure($_POST['cat-name']);  
    ...

    $humancheck = $_POST['humancheck'];
        $honeypot = $_POST['honeypot'];
           if ($honeypot == 'http://' && empty($humancheck)) {  
          $error_message = '';  
            if (empty($CategoryName)) 
        {
             $error_message .= "<p>Please Enter Name.</p>";            
        }   


      if (!empty($error_message)) 
        {
                    $return['error'] = true;
                    $return['msg'] = "<h3>Opps Error Here.</h3>".$error_message;                    
                    echo json_encode($return);
                    exit();
        }

        else {
                  header('Content-type: application/json');
                  $result = mysql_query("INSERT INTO xxx SET xx = '".$xxx."',xx = '".$xxx."',xxx = '".$xxx."',xxx = '".$xxx."'") or die(mysql_error());
                  $_SESSION['Info'] = 'Added success';

                         $return['error'] = false;
                         $return['msg'] = "<p>Wait...</p>"; 
                         echo json_encode($return);
              }

            }


?>

相同PHP页面我的表格

<form role="form" id="add" action="functions.php?type=add" method="post" enctype="multipart/form-data">
     <input type="text" class="form-control" name="cat-name"id="cat-name" placeholder="name">
      ...
     <input type="hidden" name="honeypot" id="honeypot" value="http://" />            
     <input type="hidden" name="humancheck" id="humancheck" class="clear" value="" />
        <button type="submit" id="submit" class="btn btn-primary" data-loading-text="Loading...">Add</button>
        <button type="button" onClick="parent.location='kategoriler.php'" class="btn btn-danger">Cancel</button>
      </form>

AJAX功能

function submitForm(formData) {

    $.ajax({    
        type: 'POST',
        url: 'functions.php?type=add',      
        data: formData,
        dataType: 'json',
        cache: false,
        timeout: 7000,
        success: function(data) {           
            $('#response').removeClass().addClass((data.error === true) ? 'alert alert-danger' : 'alert alert-success').html(data.msg).fadeIn('fast');  
        if ($('#response').hasClass('alert alert-success')) {
                        window.location.href = "kategoriler.php";
                    }

        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
        $('#response').removeClass().addClass('error').html('<p>There was an<strong> ' + errorThrown +'</strong> error due to a<strong> ' + textStatus +'</strong> condition.</p>').fadeIn('fast');         
        },              
        complete: function(XMLHttpRequest, status) {            
            $('form')[0].reset();
        }
    }); 
};

此外,我有ajax验证,当我发送表单验证工作,但SubmitForm不起作用。有一个

SyntaxError: Unexpected token < error due to a parser error condition.

有此错误,但表单值添加数据库。

2 个答案:

答案 0 :(得分:1)

试试这个

header('Content-type: application/json');

当您以json格式发送数据时,需要在php文件中提及此标头。

答案 1 :(得分:0)

检查php的json_encode是否正确完成工作(发布响应示例将有所帮助)。可能不是它,但我的第一个猜测是你的JSON响应也没有在你发回的HTML字符串中包含引号。像这样:

{
    error: false,
    msg: <p>Wait...</p>
}

什么时候应该

 {
    error: false,
    msg: '<p>Wait...</p>'
 }