如何在 yii 框架中使用 ajax 从表单获取数据到控制器
How to get data from form to controller using ajax in yii framework
嗨,我是 yii 框架的新手。我正在根据第一个下拉菜单创建两个动态下拉菜单,我想显示第二个下拉菜单。而且我想对这两个下拉菜单进行验证。那么如何将 id 从表单传递到控制器以及如何验证它呢?请帮助我,我的表单代码是
echo CHtml::activeDropDownList($model,'classid',CHtml::listData(Classdetails::model()->findAll(array("condition"=>"classid >0 and School_Id='$School' and Status=1","order"=>"classid")),'classid','classname'),
array(
'empty'=>'--Select a Class--',
'ajax' => array(
'type'=>'POST', //request type
'url'=>CController::createUrl('Ptempnotification/Dynamiccities'),
'update'=>'#pid',
)));
echo $form->error($model,'classid');
echo $form->labelEx($model,'pid');
echo CHtml::dropDownList('pid','', array());
echo $form->error($model,'pid');
我的控制器代码是
public function actionDynamiccities()
{
$data = Puserprofile::model()->findAllByAttributes(array('classid' => $_POST['classid']));
$data=CHtml::listData($data,'pid','username');
foreach($data as $value=>$name)
{
echo CHtml::tag('option', array('value'=>$value),CHtml::encode($name),true);
}
}
此处正在调用 dynamiccities 函数,但 $_POST['classid']
正在传递 null。我正在使用 yii 框架 1.1
请这样试,
echo CHtml::activeDropDownList($model,'classid',CHtml::listData(Classdetails::model()->findAll(array("condition"=>"classid >0 and School_Id='$School' and Status=1","order"=>"classid")),'classid','classname'),
array(
'empty'=>'--Select a Class--',
'ajax' => array(
'type'=>'POST', //request type
'url'=>CController::createUrl('Ptempnotification/Dynamiccities'),
'data'=>array('classid'=>'js: $(this).val()'),
'update'=>'#pid',
)));
嗨,我是 yii 框架的新手。我正在根据第一个下拉菜单创建两个动态下拉菜单,我想显示第二个下拉菜单。而且我想对这两个下拉菜单进行验证。那么如何将 id 从表单传递到控制器以及如何验证它呢?请帮助我,我的表单代码是
echo CHtml::activeDropDownList($model,'classid',CHtml::listData(Classdetails::model()->findAll(array("condition"=>"classid >0 and School_Id='$School' and Status=1","order"=>"classid")),'classid','classname'),
array(
'empty'=>'--Select a Class--',
'ajax' => array(
'type'=>'POST', //request type
'url'=>CController::createUrl('Ptempnotification/Dynamiccities'),
'update'=>'#pid',
)));
echo $form->error($model,'classid');
echo $form->labelEx($model,'pid');
echo CHtml::dropDownList('pid','', array());
echo $form->error($model,'pid');
我的控制器代码是
public function actionDynamiccities()
{
$data = Puserprofile::model()->findAllByAttributes(array('classid' => $_POST['classid']));
$data=CHtml::listData($data,'pid','username');
foreach($data as $value=>$name)
{
echo CHtml::tag('option', array('value'=>$value),CHtml::encode($name),true);
}
}
此处正在调用 dynamiccities 函数,但 $_POST['classid']
正在传递 null。我正在使用 yii 框架 1.1
请这样试,
echo CHtml::activeDropDownList($model,'classid',CHtml::listData(Classdetails::model()->findAll(array("condition"=>"classid >0 and School_Id='$School' and Status=1","order"=>"classid")),'classid','classname'),
array(
'empty'=>'--Select a Class--',
'ajax' => array(
'type'=>'POST', //request type
'url'=>CController::createUrl('Ptempnotification/Dynamiccities'),
'data'=>array('classid'=>'js: $(this).val()'),
'update'=>'#pid',
)));