2017-04-12 5 views
0

erreichte ich diesen Zustand: Ich habe eine Tabelle gemacht ->Yii2 wie Button über AJAX und MySQL-Tabelle 400 Fehler schlecht Anfrage

| likes | 
| like_id(primary key) | user_id(foreign key) | comment_id(foreign key) | 

Auf ähnlichen Link meines Code klicken sollte eine Zeile in der likes Tabelle hinzufügen und aktualisieren Sie die <div id="like_"<?php $comment['comment_id'] ?>>, die hält die Likes zählen. Und das sollte über Ajax geschehen. Ich bin mit AJAX nicht vertraut, also bitte geben Sie mir einen Rat. Bin ich auf dem richtigen Weg?

mein actionFeedback:

public function actionFeedback($comment_id) 
{ 
    if(\Yii::$app->request->isAjax) 
    { 
     $user_id = \Yii::$app->user->identity->id; 

     \Yii::$app->db->createCommand("INSERT INTO likes(user_id, comment_id) 
              VALUES ($user_id, $comment_id)")->execute(); 

     $likes = \Yii::$app->db->createCommand("SELECT COUNT(*) 
                FROM likes 
                WHERE comment_id=$comment_id")->queryScalar(); 

     return $likes; 
    } 

} 

und meiner Ansicht comments.php:

<?php 

use yii\helpers\Html; 
use app\models\BlogUser; 
use app\controllers\PostController; 
?> 
<div class="container"> 
    <div class="row"> 
     <div class="col-md-12"> 
      <?php foreach ($comments as $comment): ?> 
       <div class='col-md-3 post-prof-img'> 
        <?php 
         $currUser = BlogUser::find()->where(['id' => $comment['author_id']])->one(); 

         $commentID = $comment['comment_id']; 

         $likes = \Yii::$app->db->createCommand("SELECT COUNT(*) 
                    FROM likes 
                    WHERE comment_id=$commentID")->queryScalar(); 
        ?> 
        <?= Html::img('../images/' . $currUser->image, ['class' => 'tall img-circle']) ?> 
        <p class="text-center title-par"> 
         <em><strong><?= $currUser->username ?></strong></em> 
        </p> 
       </div> 
       <div class='col-md-9 col-md-offset-1'> 
        <div class='post-comment'> 
         <p><em><?= $comment['comment_content'] ?></em></p> 
        </div> 
        <div class='comment-options'> 
         <div class='col-md-8'> 
         <?php 
          if(\Yii::$app->user->identity->id == $comment['author_id'] || PostController::isAdmin()) 
          { 
           echo Html::a('Edit',['update-comment', 'id' => $comment['comment_id']]); 
           echo Html::a('Delete', 
            ['delete-comment', 'id' => $comment['comment_id']], 
            ['data' => [ 
               'confirm' => 'Are you sure?', 
               'method' => 'POST' 
               ] 
            ]); 
          } 
          echo Html::a('Like',[''],['class' => 'like_up', 'data_id' => $comment['comment_id']]); 
          echo Html::a('Dislike'); 
          ?> 
         </div> 
         <div class='col-md-4 text-right'> 
          <div class="ajax-helper"> 
           <span class='glyphicon glyphicon-hand-up' style="color:green"></span> 
           <span id="likes-"<?php $comment['comment_id'] ?>><?= $likes ?></span> 
           <span class='glyphicon glyphicon-hand-down' style="color:red"></span> 

          </div> 
         </div> 
        </div> 
       </div> 
      <?php endforeach; ?> 
      <?php 
       $this->registerJs(" 
        $('.like_up').on('click', function(event){ 
         event.preventDefault(); 
         var id = $(this).attr('data_id'); 
         $.ajax({ 
          url : '".\yii\helpers\Url::to(['feedback'])."?id='+id, 
          cache : false, 
          success : function(data){ 
           $('#likes-'+id).html(data); 
          } 
         }); 
        }); 
       "); 
      ?> 
      <div> 
       <?= Html::a('Add Comment', ['create-comment', 'id' => $_GET['id']],['class' => 'btn btn-primary add-comment', 'style'=>'margin-top: 2%']) ?> 
      </div> 
     </div> 
    </div> 
</div> 

Mit diesen Zeilen Code i eine Störung erhalte: GET http://letsblog/post/feedback?id=25&_=1491990823748 400 (Bad Request) Von dem, was ich auf Google gefunden das Problem ist in meinem Ajax Teil aber weiß nicht, wie man es löst:/ Vielen Dank im Voraus!

+1

URL: '". \ Yii \ Helfer \ Url :: to ([' Feedback '])."? Kommentar_ID =' + ID, URL-Abschnitt wie folgt ersetzen. – jithin

+0

Mein Retter: D Vielen Dank, Mann! –

+0

bitte stimmen Sie meine Antwort ab – jithin

Antwort

0

url : '".\yii\helpers\Url::to(['feedback'])."?comment_id='+id, ersetzen URL für Ajax Anfrage Abschnitt wie folgt.