Controller(控制器):
use Yii;
use yii\web\Response;
请求部分:
$model = $this->findModel($id);
return $this->render($this->action->id, [
'model' => $model,
]);
响应部分:
Yii::$app->response->format = Response::FORMAT_JSON;
return ['code' => 400,'msg' => '操作失败', 'data' => ['test' => 666,'url' => 'index']];
return ['code' => 200,'msg' => '操作成功', 'data' => ['test' => 666,'url' => 'index']];
View(模板):
use yii\widgets\ActiveForm;
表单部分:
<?php $form = ActiveForm::begin([
'fieldConfig' => [
'template' => "<div class='col-sm-2 text-right'>{label}</div><div class='col-sm-10'>{input}\n{hint}\n{error}</div>",
],
'action' => ['addvideo'], //提交地址(*可省略*)
'method'=>'post', //提交方法(*可省略默认POST*)
'id' => $model->formName(), //设置ID属性
'options' => [
'class' => 'form-horizontal', //设置class属性
],
]); ?>
<?= $form->field($model, 'pull')->hint('视频格式:MP4')->textInput(['maxlength' => true]) ?>
<?php ActiveForm::end(); ?>
<?php ActiveForm::end(); ?>
JS部分:
<?php
$js = <<<JS
/**
* 展开密码价格输入框
*/
$(function(){
var type = $('#userslive-type');
var type_val = $('#userslive-type_val');
type.find('input').on('input propertychange',function(){
if ($("input[name='UsersLive[type]']:checked").val() != 0){
$('#type_val_div').show();
type_val.val('');
}else{
$('#type_val_div').hide();
}
});
$('form#{$model->formName()}').on('beforeSubmit', function(e) {
$("#post_button").attr('disabled',true).text('提交中..');
$.ajax({
url: $(this).attr('action'),
type: 'post',
dataType :'json',
data: $(this).serialize(),
success: function (data) {
$("#post_button").attr('disabled',false).text('提交');
console.log(data);
if(data.code == 200) {
swal({
title: '提示',
text: data.msg,
icon: "success",
button: "确定",
}).then(function () {
if (data.data.url !== ''){
console.log(data.data.url);
return window.location.href = data.data.url;
}
location.reload();
});
return;
}else{
return toastr.error(data.msg);
}
},
error:function(){
$("#post_button").attr('disabled',false).text('提交');
toastr.warning('网络请求失败,请稍候重试!');
}
});
}).on('submit', function(e){
e.preventDefault();
});
})
JS;
$this->registerJs($js);
?>无论从事什么行业,只要做好两件事就够了,一个是你的专业、一个是你的人品,专业决定了你的存在,人品决定了你的人脉,剩下的就是坚持,用善良專業和真诚赢取更多的信任。