i am trying to insert data to a database table using ajax in yii2. but i don't really know how to go about it i want to send the post data to the controller the problem seems to come from the ajax script .it is meant to show me the value of rate which is 'up'. instead the alert show me.
this is my view class
<?php
use yiiwidgetsListView;
use yiidataArrayDataProvider;
use appmodelsMyProfile;
use appmodelsLikeDiscussion;
use yiiwidgetsActiveForm;
use commonmodelsTopic;
use commonmodelsThumbs;
use commonmodelsComment;
use commonmodelsCandidate;
use yiiwidgetsPjax;
use yiihelpersHtml;
use frontendassetsAppAsset;
$this->title = 'My Yii Application';
?>
<button id="save" name="save" class="readmore">up</button>
this is my js file.
var fac= document.getElementById("save");
fac.onclick = function fun()
{
$.ajax({
url: '/index.php?r=site%2Fblog',
type: 'post',
data:{"rate": 'up'},
success:function(data){
alert(data);
}
});
}
this is my contoller class
public function actionBlog()
{
$thumbs= new Thumbs();
if(isset($_POST['rate'])){
$thumbs->user=Yii::$app->user->identity->email;
$thumbs->topic_id = 1;
$thumbs->rate = $_POST['rate'];
$thumbs->load($_POST);
$thumbs->save();
}
return $this->render('blog');
}
this is what i get as the alert
Aucun commentaire:
Enregistrer un commentaire