題名の通りです。
現在はAJAXでデータはとばせているのですが、
HTML側の切り替えができません。
非同期でボタンの部分とカウント数字を切り替えるようにしたいです。
.doneの部分で処理するものと考えております。
どなたかよろしくお願いします。
<div class="detail-point-box"> <div class="detail-point-heart"> <?php if ( $auth_id !== $val['user_id']) { if( isset( $auth_id ) ) { $score_user = $val->id; $liked_flg = false; foreach($val->points as $val2 ) { if ( $val2->user_id === $auth_id ) { $liked_flg = true; break; } else { $liked_flg = false; } } if( $liked_flg === false ) { echo $this->Form->create( '',['url' => [ 'controller' => 'points', 'action' => 'add']]); echo $this->Form->hidden('writer_id', ['value' => $val[ 'user_id' ], 'id' => 'writer_id' ]); echo $this->Form->hidden('score_id',[ 'value' => $val[ 'id' ], 'id' => 'score_id']); echo $this->Form->hidden('user_id', ['value' => $user_login, 'id' =>'user_id' ]); echo $this->Form->hidden('item_id', ['value' => $val['item_id'], 'id' =>'item_id' ]); echo $this->Html->link('<i class="far fa-heart btn-like"></i>',['action' => 'add'],['escape' => false]); echo $this->Form->end(); } else { ?></div> <div id="yaho" class="yaho"><?php echo $this->Form->create( '',['url' => [ 'controller' => 'points', 'action' => 'delete']]); echo $this->Form->hidden('writer_id', ['value' => $val[ 'user_id' ], 'id' => 'writer_id' ]); echo $this->Form->hidden('score_id',[ 'value' => $val[ 'id' ], 'id' => 'score_id']); echo $this->Form->hidden('point_id',[ 'value' => $val2[ 'id' ], 'id' => 'point_id']); echo $this->Form->hidden('user_id', ['value' => $user_login, 'id' =>'user_id' ]); echo $this->Form->hidden('item_id', ['value' => $val['item_id'], 'id' =>'item_id' ]); echo $this->Html->link('<i class="fas fa-heart btn-unlike"></i>',['action' => 'delete'],['escape' => false]); echo $this->Form->end(); } } } ?> </div> <div class="detail-point-count"> <?php if ( $val[ 'count_point' ] > 0 ) { <div class="detail-point-count"> <?php if ( $val[ 'count_point' ] > 0 ) { echo $val[ 'count_point' ]; } else { echo 0; } ?> </div> </div>
$(function(){ var csrf = $('input[name=_csrfToken]').val(); var $this = $(this); $('.btn-like').on("click", function(event) { event.preventDefault(); var val = $(this).val(); var user_id = $('#user_id').val(); var item_id = $('#item_id').val(); var score_id = $('#score_id').val(); var writer_id = $('#writer_id').val(); data= { 'user_id': user_id, 'writer_id': writer_id, 'score_id': score_id }; $.ajax ({ type: 'POST', beforeSend: function(xhr){ xhr.setRequestHeader('X-CSRF-Token', csrf); }, datatype: 'json', url: "/items/ajax_point_add/" + item_id, data: data, }) .done(function (data) { var res = $('#yaho').find('a'); $this.find().toggleClass(res); $this.find().toggleClass('fas'); $this.find().toggleClass('active'); $this.toggleClass('active'); $('detail-point-heart').html(data.count_point); }); return false; }); $('.btn-unlike').on("click", function(event) { event.preventDefault(); var val = $(this).val(); var user_id = $('#user_id').val(); var item_id = $('#item_id').val(); var score_id = $('#score_id').val(); var point_id = $('#point_id').val(); var writer_id = $('#writer_id').val(); data= { 'user_id': user_id, 'writer_id': writer_id, 'point_id': point_id, }; $.ajax ({ type: 'POST', beforeSend: function(xhr){ xhr.setRequestHeader('X-CSRF-Token', csrf); }, datatype: 'json', url: "/items/ajax_point_delete/" + item_id, data: data, }) .done(function (data) { }); return false; }); })
.done(function (data) { $this.empty(); $this.append('<i class="fas fa-heart">').val(data); });
これで表示は切り替えれるのですが、データが反映されないので、再度押しても反応を得られません!