我有一个视图,其中有a
标记按钮。在单击按钮时,ajax
请求将发送到给定的action view controller
,并且在action view
中,某些值将保存在数据库中。之后我将视图重定向到另一个视图。
我做了什么?
我的观点
<a href="<?= URL::toRoute(['ogpheader/viewsetpdf', 'id'=>$model->id])?>" name="redirect" class="btn btn-primary" id="myid">Set PDF</a>
//this is the a tag button
在同一视图中调用Ajax
<?php
$url = Url::toRoute(['/ogpheader/viewsetpdf','id'=>$model->id]);
$script = <<< JS
$(document).ready(function () {
$('#myid').on('click',function(e) {
e.preventDefault();
var strValue = "";
$('input[name="selection[]"]:checked').each(function() {
if(strValue!="")
{
strValue = strValue + " , " + this.value;
}
else
strValue = this.value;
});
//alert(strValue);
$.ajax({
url: '$url',
type: 'POST',
dataType: 'json',
data: {data:strValue},
success: function(data) {
alert(data);
},
});
})
});
JS;
$this->registerJs($script, static::POS_END);
?>
我的操作控制器
if(Yii::$app->request->isAjax && Yii::$app->request->post())
{
$data = explode(',',$_POST['data']);
foreach($data as $value)
{
$m = new Ogpdetail;
$m -> load(Yii::$app->request->post());
$m->ogp_id = $ogp_id;
$m->created_at = date('Y-m-d h:i:s');
$m->meter_id = $value;
$m->meter_serial = \common\models\Meters::idTomsn($value);
if($m->save())
{
$model->status = Ogpheader::$status_titles[1];
$model->update();
//echo "All data is saved";
//exit();
}
else{
$this->renderAjax('viewcreated');
}
}
//print_r($data);
}
else{
$this->renderAjax('viewcreated');
}
$query = "SELECT DISTINCT ogpd.`meter_serial` AS 'Meter_Serial_Number', IFNULL(ogpd.`remarks`,'') AS 'Remarks' FROM `ogp_detail` ogpd
INNER JOIN `ogp_header` ogph ON ogpd.`ogp_id` = ogph.`id`";
$count = Yii::$app->db->createCommand("SELECT COUNT(DISTINCT ogpd.`meter_serial`) FROM `ogp_detail` ogpd
INNER JOIN `ogp_header` ogph ON ogpd.`ogp_id` = ogph.`id`")->queryScalar();
$dataProvider = new SqlDataProvider([
'sql' => $query,
'totalCount' => $count,
'pagination' => [
'pageSize' => 5,
],
]);
return $this->redirect('viewsetpdf',[
'dataProvider' => $dataProvider,
'model' => $this->findModel($id),
'id' => $model->id
]);
当我点击我的Set PDF
按钮时,我收到Not Found (#404)
。所有记录都保存到我想要的表格中。
URL
也是http://localhost:225/inventory-web/backend/web/ogpheader/viewcreated/viewsetpdf
,但应该是http://localhost:225/inventory-web/backend/web/ogpheader/viewsetpdf
我不知道问题是什么,我坚持下去。
任何帮助都将受到高度赞赏。
答案 0 :(得分:1)
您正在生成路线,而不是网址。尝试替换它:
<a href="<?= URL::toRoute(['ogpheader/viewsetpdf', 'id'=>$model->id])?>" name="redirect" class="btn btn-primary" id="myid">Set PDF</a>
有了这个:
Html::a('Set PDF', ['ogpheader/viewsetpdf', 'id'=>$model->id], ['id' => 'myid', 'name'=>'redirect', 'class'='btn btn-primary'])