arinze arinze - 1 year ago 172
Ajax Question

Ajax save to database in yii2

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();
$my_array = explode(":", $data['rate']);
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";
type: 'POST',
url: 'index.php?r=site%2Fthumbs',
data: {
rate: rate,
_csrf: '<?=Yii::$app->request->getCsrfToken()?>'
success: function(rate) {
error: function (exception) {

my view file:

<div class="six columns">
<?= Html::Button('ups', ['class' => 'btn btn-primary',
'name' => 'mys' ,'id'=>'mys']) ?>

Answer Source

Try using


if the value are inserted .. then check for your validation .. rule in model ..

the false condition mean no validation .. the value are saved in db without performing any validation rule

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download