1

I have this function to post serialized data to an insert script. With the lastest version of Jquery it is not working anymore. What part in the Js is the problem?

<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);
4

1 回答 1

0

你应该使用

$('#addCommentForm').serialize()

代替

$(this).serialize();

更新

serialized data在传递到post methodin之前控制台js

还要签到php page。你收到了什么POST data,通过使用

print_r($_POST);
于 2013-06-16T08:35:55.983 回答