私はAJAXを初めて使うので、チュートリアルを見つけてPHPコードに合わせてハッキングしました。これは "好きな"ボタンのチュートリアルで、コード内では "好き"の合計をデータベースに追加することで問題なく動作します。AJAXレスポンスが得られません
「好き」をクリックすると、好き嫌いの数が(ページ上で)更新されないという問題があります。
元のチュートリアルのコードはうまくいきますので、明らかに私は何か愚かなことをしました。ここで
はindex.phpを
function cwRating(id,type,target){
$.ajax({
type:'POST',
url:'rating.php',
data:'bt_id='+id+'&type='+type,
success:function(msg){
if(msg == 'err'){
alert('Some problem occured, please try again.');
}else{
$('#'+target).html(msg);
}
}
});
}
のための私のコードだと、これは、それはなどを示しなど数えるスポットです:
<span class="like" onClick="cwRating(<?php echo $row_bt['bt_id']; ?>,1,'like_count<?php echo $row_bt['bt_id']; ?>')"></span>
<span class="counter" id="like_count<?php echo $row_bt['bt_id']; ?>"><?php echo $row_bt['like_num']; ?></span>
これは全体rating.phpページです:
include_once("likes.php");
$likes = new Likes();
if($_POST['bt_id']){
//previous tutorial data
$prev_record = $likes->get_rows($_POST['bt_id']);
//previous total likes
$prev_like = $prev_record['like_num'];
//calculates the numbers of like or dislike
if($_POST['type'] == 1){
$like = ($prev_like + 1);
$return_count = $like;
}else{
$like = $prev_like;
}
//store update data
$data = array('like_num'=>$like,'like_date'=>date("Y-m-d H:i:s"));
//update condition
$condition = array('bt_id'=>$_POST['bt_id']);
//update tutorial like dislike
$update = $likes->update($data,$condition);
//return like or dislike number if update is successful, otherwise return error
echo $update?$return_count:'err';
}
私は応答を.counterクラスに入れたいと思っています。それは、調整が必要なAJAXコードだと仮定しています。
あなたのajax呼び出しでエラーを返してください。成功:function(){}、エラー:function(err){console.log( 'error'、err); } ' –
私はそれを追加しましたが、何も得られません。 – Ironank
してください、もし成功していれば、成功関数を返すようにしてください。これを試してください。成功:function(msg){console.log(msg); } ' –