I am trying to insert to database yii2 using ajax, but I have a problem and it does not give me any errors at all. It shows success message like it works but nothing inserts. It does not reflect in the database.
this is my controller:
public function actionThumbs()
{
$thumbs= new Thumbs;
if (Yii::$app->request->isAjax) {
$data = Yii::$app->request->post();
$thumbs->user=Yii::$app->user->identity->email;
$thumbs->topic_id=Yii::$app->getRequest()->getQueryParam('id');
$my_array = explode(":", $data['rate']);
$thumbs->rate=$my_array[0];
$thumbs->load($_POST);
$thumbs->save();
return ['blog', 'id' => Yii::$app->getRequest()->getQueryParam('id')];
}
return $this->redirect(['blog','id' => Yii::$app->getRequest()->getQueryParam('id')]);
}
this is my ajax file:
$("#mys").click(function() {
var rate = "up";
$.ajax({
type: 'POST',
url: 'index.php?r=site%2Fthumbs',
data: {
rate: rate,
_csrf: '<?=Yii::$app->request->getCsrfToken()?>'
},
success: function(rate) {
alert("test");
},
error: function (exception) {
console.log(exception);
}
});
});
my view file:
<div class="six columns">
<?= Html::Button('ups', ['class' => 'btn btn-primary',
'name' => 'mys' ,'id'=>'mys']) ?>
</div>