yii 接收ajax数据库,Ajax保存到yii2中的数据库(Ajax save to database in yii2)

Ajax保存到yii2中的数据库(Ajax save to database in yii2)

我试图用ajax插入数据库yii2,但是我有一个问题,它根本不会给我任何错误。 它显示成功消息,因为它工作但没有插入。 它不反映在数据库中。

这是我的控制器:

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')]);

}

这是我的ajax文件:

$("#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);

}

});

});

我的观点文件:

= Html::Button('ups', ['class' => 'btn btn-primary',

'name' => 'mys' ,'id'=>'mys']) ?>

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:

= Html::Button('ups', ['class' => 'btn btn-primary',

'name' => 'mys' ,'id'=>'mys']) ?>

原文:https://stackoverflow.com/questions/39758532

更新时间:2020-01-28 17:45

最满意答案

尝试使用

$thumbs->save(false);

如果插入了值..然后检查模型中的验证..规则..

假条件表示没有验证..该值保存在分贝中,而不执行任何验证规则

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

相关问答

所以,我能解决我的问题。 我所做的事情在下面 分配给我的表列email_transaction的foreign keys来自不同的表,因此首先我删除了所有的FK's ,然后再次从同一个表中添加它们。 此外,我已将所有FK字段设置为NOT NULL 。 在服务器端,我已经删除了模型并重新生成了它。 插入所有记录并成功插入。 foreach ($result as $result_set) {

$email_setup_id = $result_set['setup_id'];

...

Redis DB可以声明为缓存组件或数据库连接或两者 。 当它被声明为缓存组件( 使用yii / redis / cache )时,可以在该组件中访问它以存储键/值对,如此处所示 。 $cache = Yii::$app->cache;

// try retrieving $data from cache

$data = $cache->get($key);

// store $data in cache so that it can be retrieved next time

$cache-

...

你应该改变表格结构。 在那里你要存储persian文本,那么Collation应该是utf8_unicode_ci 下面是快照 You should change the table structure. Where you want to store persian text there Collation should be utf8_unicode_ci below is the snapshot

很可能您的模型未通过验证。 您可以使用$form_model->save(false)关闭if,但最好知道它为什么没有验证。 做这个: if (!$form_model->validate()) {

return $this->render('index', [

'form_model' => $form_model

]);

}

Most likely your model fails validation. You can turn if off u

...

如果我没有记错yii2当你可以尝试将值设置为NULL时,不要为null值进行字符串转换 $model->field = NULL;

If i remember correctly yii2 don't make a string conversion for null value when you can try setting the value to NULL $model->field = NULL;

你需要添加你的模型初始化$m=new YourModel(); 在foreach循环内的elseif($this->ogpCreated($item->MSN)) ,这就是为什么它只保存一条记录 foreach ($record as $item){

if($this->isSaved($item->MSN)){

return false;

}

else if($this->ogpCreated($item->MSN)){

$m= new YourModel();

...

您可以尝试使用临时验证 。 $validator = new \himiklab\yii2\recaptcha\ReCaptchaValidator;

$validator->secret = '...';

if ($validator->validate($entered_recaptcha_code, $error)) {

// ok

} else {

echo $error;

}

我之前没有尝试过,可能还需要一些额外的配置。 You can try to use ad h

...

根据您的代码, for_date在beforeValidate运行后仍然处于日期格式,因为该行: $this->for_date = date('d/M/Y', $this->for_date);

删除这条线,它应该工作。 然而,您仍然有问题,例如格式化日期或某人输入无效日期,例如30/02/2015 。 我会建议创建一个单独的属性,例如for_date_display并for_date_display添加日期规则。 然后在beforeSave将此日期转换为时间戳并将for_date设置为该值,

...

问题 ajax中的问题是$_FILES细节不会在异步请求中发送。 当我们提交没有Ajax请求的填充表单并在PHP的后端进行调试时 echo '

';

print_r($_FILES);

print_r($_POST);

echo '

';

die;

那么我们会成功获取$_FILES和$_POST数据。 但是当我们在ajax请求中调试同样的东西时,我们只获得$_POST值,并且我们将$_FILES作为NULL 。 这导致我们得出结论, $_FILES数据不是由我们的上述代码

...

尝试使用 $thumbs->save(false);

如果插入了值..然后检查模型中的验证..规则.. 假条件表示没有验证..该值保存在分贝中,而不执行任何验证规则 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 valu

...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值