作者:zccst
[size=large][b]一、前端提交的三种方式[/b][/size]
前面已经提出,表单提交一共只有三种方式。
1,前端原生html
(1)原生html标签
首先,直接使用html标签的input,a等;
其次,使用css进行布局。
再次,使用js或jquery方式验证
最后,提交(get/post/ajax,目前较多使用jquery提交)。
注:该种方式由于较灵活,使用最多。
(2)原生form
js或jquery方式验证,yii方式提交。
相对灵活
批注:如果仅仅使用表单自带的onsubmit验证不够,也可以采用第一种办法。此时就不要使用form自带的submit按钮了。
2,前端使用yii自带widget
(3)yii方式验证、提交。
此时用到yii的表单验证功能。需要model+controller+view配合使用。详见:yii指南。
Yii方式验证、提交
1,首先,在model的rules(){return array();}设置验证规则
2,其次,在view里使用errorMessage,label,field,error
<?php echo $form->labelEx($model,'author'); ?>
<?php echo $form->textField($model,'author',array('size'=>60,'maxlength'=>128)); ?>
<?php echo $form->error($model,'author'); ?>
3,最后,在controller里
使用
$model = new Post;
$model->attributes=$_POST['Post'];
yii判断提交方式
Yii::app()->request->isPostRequest
[b]效率比较:[/b]
(1)效率最差;
(2)效率中等;
(3)效率最高。
[size=large][b]二、Yii在后端接收前端传来值的两种方式[/b][/size]
1,通过二维数组$_POST['ClassName']
如果前端的表单是关于某一个model的,可以用此方法。使用较多。
例如:
$model->attributes=$_POST['Post'];
$comment->attributes=$_POST['Comment'];
还可以添加:$comment->post_id=$this->id;
2,获取get或post请求的值$this->request->getParam('key');
(1)form表单提交
前端:
<form>
<input type="text" name="belongs_to" />
<input type="submit" value="提交" />
</form>
后端:
$this->request->getParam('belongs_to');
(2)jquery的post提交
$this->request->getParam('key1');
如果您觉得本文的内容对您的学习有所帮助,您可以微信:
[img]http://dl2.iteye.com/upload/attachment/0109/0668/fb266dfa-95ca-3d09-b41e-5f04a19ba9a1.png[/img]
[size=large][b]一、前端提交的三种方式[/b][/size]
前面已经提出,表单提交一共只有三种方式。
1,前端原生html
(1)原生html标签
首先,直接使用html标签的input,a等;
其次,使用css进行布局。
再次,使用js或jquery方式验证
最后,提交(get/post/ajax,目前较多使用jquery提交)。
注:该种方式由于较灵活,使用最多。
(2)原生form
js或jquery方式验证,yii方式提交。
相对灵活
<form ... onSubmit="return validationFunc">
...
</form>
<script type="text/javascript">
function validationFunc(){
//验证逻辑
}
</script>
批注:如果仅仅使用表单自带的onsubmit验证不够,也可以采用第一种办法。此时就不要使用form自带的submit按钮了。
2,前端使用yii自带widget
(3)yii方式验证、提交。
//1,自带blog登陆表单
<?php
$form=$this->beginWidget('CActiveForm', array(
'id'=>'login-form',
'enableClientValidation'=>true,
'clientOptions'=>array(
'validateOnSubmit'=>true,
),
));
?>
//2,上传用户头像时
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'user-form',
'enableAjaxValidation'=>false,
'htmlOptions'=>array('enctype'=>'multipart/form-data'),
)); ?>
//3,看到的一个例子
<?php $form=$this->beginWidget('CActiveForm',array(
'id'=>'login-from',
'enableAjaxValidation'=>true,
'action'=>'array('site/login'),
)
)
?>
此时用到yii的表单验证功能。需要model+controller+view配合使用。详见:yii指南。
Yii方式验证、提交
1,首先,在model的rules(){return array();}设置验证规则
2,其次,在view里使用errorMessage,label,field,error
<?php echo $form->labelEx($model,'author'); ?>
<?php echo $form->textField($model,'author',array('size'=>60,'maxlength'=>128)); ?>
<?php echo $form->error($model,'author'); ?>
3,最后,在controller里
使用
$model = new Post;
$model->attributes=$_POST['Post'];
yii判断提交方式
Yii::app()->request->isPostRequest
[b]效率比较:[/b]
(1)效率最差;
(2)效率中等;
(3)效率最高。
[size=large][b]二、Yii在后端接收前端传来值的两种方式[/b][/size]
1,通过二维数组$_POST['ClassName']
如果前端的表单是关于某一个model的,可以用此方法。使用较多。
例如:
$model->attributes=$_POST['Post'];
$comment->attributes=$_POST['Comment'];
还可以添加:$comment->post_id=$this->id;
2,获取get或post请求的值$this->request->getParam('key');
(1)form表单提交
前端:
<form>
<input type="text" name="belongs_to" />
<input type="submit" value="提交" />
</form>
后端:
$this->request->getParam('belongs_to');
(2)jquery的post提交
$this->request->getParam('key1');
如果您觉得本文的内容对您的学习有所帮助,您可以微信:
[img]http://dl2.iteye.com/upload/attachment/0109/0668/fb266dfa-95ca-3d09-b41e-5f04a19ba9a1.png[/img]