Rumah > Artikel > pembangunan bahagian belakang > Yii表单模型使用及以数组形式提交表单数据_PHP教程
按Yii文档里的描述,Yii在处理表单的一般过程是:
在刚刚的一个小项目里,想使用ajax提交表单信息并验证保存,又不想用隐藏iframe来做无刷新提交,并且action中能够用到模型类的校验方法,就想到使用表单数组提交的方式,举个例子:
form代码:
<form action='' method='post' name='form_test'> <input type='text' name='arr[]' value='1'> <input type='text' name='arr[]' value='2'> <input type='text' name='arr[]' value='3'> </form>
提交后可以直接使用 $_POST['arr'] 来获取提交的数据,$_POST['arr'] 为:
<span Array<br /></span><span ( [</span>0] =><span a [</span>1] =><span b [</span>2] =><span c )</span>
同理,如果使用以下form提交:
<form action='' method='post' name='form_test'> <input type='text' name='arr[3]' value='a'> <input type='text' name='arr[6]' value='b'> <input type='text' name='arr[8]' value='c'> </form>
$_POST['arr'] 为:
<span Array</span><span ( [</span>3] =><span a [</span>6] =><span b [</span>8] =><span c )</span>
当然也能提交二维数组:
<form action='http://127.0.0.1/zhaobolu/test.php' method='post' name='form_test'> <input type='text' name='arr[][name1]' value='a'> <input type='text' name='arr[][name2]' value='b'> <input type='text' name='arr[][name3]' value='c'> </form>
$_POST['arr'] 为:
<span Array</span><span ( [</span>0] => <span Array</span><span ( [name1] </span>=><span a ) [</span>1] => <span Array</span><span ( [name2] </span>=><span b ) [</span>2] => <span Array</span><span ( [name3] </span>=><span c ) )</span>
这里有一个问题,如果不设置第一个子数组的key,在生成数组时会将每个值顺序在arr中添加,如果想将信息保存在一个array中,添加一个key值即可,如下:
<form action='http://127.0.0.1/zhaobolu/test.php' method='post' name='form_test'> <input type='text' name='arr[a][name1]' value='a1'> <input type='text' name='arr[a][value1]' value='a2'> <input type='text' name='arr[b][name2]' value='b1'> <input type='text' name='arr[b][value2]' value='b2'> </form>
$_POST['arr'] 为:
<span Array</span><span ( [a] </span>=> <span Array</span><span ( [name1] </span>=><span a1 [value1] </span>=><span a2 ) [b] </span>=> <span Array</span><span ( [name2] </span>=><span b1 [value2] </span>=><span b2 ) )</span>
下面贴一下用ajax提交表单并且用yii表单模型验证的示例,首先是模型类部分,只有最简单的校验方法:
<?<span php </span><span class</span> LandingForm <span extends</span><span CFormModel { </span><span public</span> <span $landing_title</span><span ; </span><span public</span> <span $landing_content</span><span ; </span><span public</span> <span $landing_position</span><span ; </span><span public</span> <span function</span><span rules() { </span><span return</span> <span array</span><span ( </span><span array</span>('landing_title, landing_content', 'required'), <span array</span>('landing_position', 'default', 'value'=>''),<span ); } }</span>
发现个比较有意思的,就是模型类在设置参数校验的方法时,需要对每一个public参数都设置规则,如果有未设置规则的参数,在用$_POST中的表单值为模型赋值后,未设置规则的参数值将为空
action中获取表单提交的参数并且校验:
<span $model = new LandingForm;<br />$model</span>->attributes = <span $_POST</span>['form'<span ]; </span><span if</span>(<span $model</span>-><span validate()){ </span><span $info</span> = <span $model</span>-><span attributes; </span>...<span }<br />...</span>
最后是前端提交表单部分的代码,用的jquery:
<span var</span> info = <span new</span><span Object(); info </span>= { 'form[landing_title]'<span : landing_title, </span>'form[landing_content]'<span : landing_content, </span>'form[landing_position]'<span : landing_position, }; </span><span var</span> url = "..."<span ; $.post(url, info, </span><span function</span><span (rst){ ... });</span>