weixin_33720956 2016-09-28 22:16 采纳率: 0%
浏览 27

Ajax保存到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();
        $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>
  • 写回答

1条回答 默认 最新

  • 喵-见缝插针 2016-09-28 22:19
    关注

    Try using

      $thumbs->save(false);
    

    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

    评论

报告相同问题?

悬赏问题

  • ¥15 使用VH6501干扰RTR位,CANoe上显示的错误帧不足32个就进入bus off快慢恢复,为什么?
  • ¥15 大智慧怎么编写一个选股程序
  • ¥100 python 调用 cgps 命令获取 实时位置信息
  • ¥15 两台交换机分别是trunk接口和access接口为何无法通信,通信过程是如何?
  • ¥15 C语言使用vscode编码错误
  • ¥15 用KSV5转成本时,如何不生成那笔中间凭证
  • ¥20 ensp怎么配置让PC1和PC2通讯上
  • ¥50 有没有适合匹配类似图中的运动规律的图像处理算法
  • ¥15 dnat基础问题,本机发出,别人返回的包,不能命中
  • ¥15 请各位帮我看看是哪里出了问题