使用最新版本的jquery发布序列化数据

时间:2013-06-16 08:26:42

标签: jquery

我有这个函数将序列化数据发布到插入脚本。使用最新版本的Jquery,它不再起作用了。 Js中的哪个部分是问题?

<div id="addCommentContainer">
    <form id="addCommentForm" method="post" action="">
            <textarea name="msg" id="msg" cols="82" title="Your comment" rows="2">Your comment...</textarea><br />
            <input type="text" name="author" title="name" value="Mark" id="author" /><br />
            <div id="personal">
            <input type="text" name="city" id="city" title="city (optional)" value="" /><br />
            <input type="text" name="email" id="email" title="e-mail (optional)" value="" /><br />
            <input type="text" name="url" id="url" title="website (optional)" value="" />
            <input type="hidden" id="cam_id" class="hd" name="cam_id" value="125879" />
            </div>
            <input type="submit" id="submit" value="Comment" />
    </form>
</div>

$("#addCommentForm").submit(function(a){
      a.preventDefault();
      if(working) return false;
      working=true;
      $("#submit").val("Working..");$("span.error").remove();
      $.post("/comment/insert.php",$(this).serialize(),function(a){
           $loading.show();working=false;$("#submit").val("Submit");
           if(a.status){
              $(a.html).hide().insertBefore("#addCommentContainer").slideDown();
              $("#msg").val("")
           }
           else{
               $.each(a.errors,function(a,b){$("label[for="+a+"]").append('<span class="error">'+b+"</span>")})
           }
      },"json")
 });




public static function validate(&$arr)
    {

        $errors = array();
        $data   = array();

        if(!($data['email'] = filter_input(INPUT_POST,'email',FILTER_VALIDATE_EMAIL)))
        {

        }

        if(!($data['url'] = filter_input(INPUT_POST,'url',FILTER_VALIDATE_URL)))
        {
            $url = '';
        }

        if(!($data['msg'] = filter_input(INPUT_POST,'msg',FILTER_CALLBACK,array('options'=>'Comment::validate_text'))))
        {
            $errors['msg'] = 'Please enter a comment.';
        }

        if(!($data['cam_id'] = filter_input(INPUT_POST,'cam_id',FILTER_CALLBACK,array('options'=>'Comment::validate_text'))))
        {

        }

        if(!($data['city'] = filter_input(INPUT_POST,'city',FILTER_CALLBACK,array('options'=>'Comment::validate_text'))))
        {

        }

        if(!($data['author'] = filter_input(INPUT_POST,'author',FILTER_CALLBACK,array('options'=>'Comment::validate_text'))))
        {
            $errors['author'] = 'Please enter a name.';
        }

        if(!empty($errors)){
            $arr = $errors;
            return false;
        }

        foreach($data as $k=>$v){
            $arr[$k] = mysql_real_escape_string($v);
        }

        $arr['email'] = strtolower(trim($arr['email']));
        return true;
    }

    private static function validate_text($str)
    {

        if(mb_strlen($str,'utf8')<1)
        // return false;

        $str = nl2br(htmlspecialchars($str));
        $str = str_replace(array(chr(10),chr(13)),'',$str);

        return $str;
    }
}


$arr = array();
$validates = Comment::validate($arr);

1 个答案:

答案 0 :(得分:0)

你应该使用

$('#addCommentForm').serialize()

而不是

$(this).serialize();

<强>更新

在传递到serialized data中的post method之前控制js

同时检查php page。您在POST data收到的内容, 使用

print_r($_POST);