以下は、ajax を使用してデータベース バックエンドに投稿するコメント フォームのスクリプトです。コメントをリアルタイムで表示するためのコールバックがあります (ページの更新なし)。
コールバック処理は json -string 関数 (insert.php の一部) によってトリガーされます。
echo json_encode(array('status'=>1,'html'=>$insertedComment->markup()));
ただし、javascript はそれに応答しません。ここで何が問題なのですか?
/* Listening for the submit event of the form: */
$('#addCommentForm').submit(function(e){
e.preventDefault();
/* Sending the form fileds to submit.php: */
$.post('/comments/insert.php',$(this).serialize(),function(msg){
if(msg.status){
$('#body').val('');
}
else {
/*
/ If there were errors, loop through the
/ msg.errors object and display them on the page
/*/
}
},'json');
});