Php 要使用ajax从多个模型中获取的下拉相关值
我在Php 要使用ajax从多个模型中获取的下拉相关值,php,mysql,yii,Php,Mysql,Yii,我在Yii上搜索了所有文档,但没有找到答案。所以我终于来了。 我有以下模式 Table Schools +------------------+--------------+------+-----+---------------------+----------------+ | Field | Type | Null | Key | Default | Extra | +------------------+--
Yii
上搜索了所有文档,但没有找到答案。所以我终于来了。
我有以下模式
Table Schools
+------------------+--------------+------+-----+---------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------------+--------------+------+-----+---------------------+----------------+
| id | int(10) | NO | PRI | NULL | auto_increment |
| school_name | varchar(100) | NO | | | |
+------------------+--------------+------+-----+---------------------+----------------+
Table Students
+------------------+--------------+------+-----+---------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------------+--------------+------+-----+---------------------+----------------+
| id | int(10) | NO | PRI | NULL | auto_increment |
| school_id | int(10) | NO | FK | | |
| student_name | varchar(100) | NO | | | |
| roll_no | varchar(80) | NO | | | |
| class | varchar(20) | NO | | | | |
| subjects | varchar(100) | NO | | | |
+------------------+--------------+------+-----+---------------------+----------------+
我为这两个模型制作了模型和CRUD
。在模型中,我的关系如下
在Students.php
中,关系如下
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'School' => array(self::BELONGS_TO,'Schools','school_id'),
);
}
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'student' => array(self::HAS_MANY, 'Students', 'school_id'),
);
}
在Schools.php
中,关系如下
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'School' => array(self::BELONGS_TO,'Schools','school_id'),
);
}
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'student' => array(self::HAS_MANY, 'Students', 'school_id'),
);
}
现在,我在一个页面中呈现了两个模型,这样我就可以在一个表单中输入所有相应的字段
In the _form.php file of Students I have made some change in student_name like this
<div class="row">
<?php echo $form->labelEx($model,'student_name'); ?>
<?php echo $form->dropdownList($model,'student_name', CHtml::listData(Students::model()->findAll(), 'id', 'student_name'), array('empty'=>array('Select'=>'--Select One---'))); ?>
<?php echo $form->error($model,'student_name'); ?>
这里是\u form.php
学生代码
<div class="row">
<?php echo $form->labelEx($model,'student_name'); ?>
<?php $List = CHtml::listData(Students::model()->findAll(), 'id', 'student_name');
?>
<?php echo $form->dropdownList($model,'student_name',$List,
array('onChange'=>CHtml::ajax(array(
'url' => CController::createUrl('DisCoor'),
'type' => 'POST',
'update'=>'#school_id',
)),'style'=>'width:180px;'
)
)?>
<?php echo $form->error($model,'student_name'); ?>
</div>
当我在firebug中看到这些之后,我得到了错误。这是屏幕截图
这很容易。请阅读。希望它能回答你所有的疑问
你也可以这样做
在下面的示例中,关键是我调用了onChange
,它使表单提交
<?php echo
$List = CHtml::listData(Students::model()->findAll(), 'id', 'student_name');
$form->dropdownList($model,'student_name',$List,
array('onChange'=>
CHtml::ajax(array(
'url' => CController::createUrl('DisCoor'),
'type' => 'POST',
'update'=>'#school_id',
)),'style'=>'width:180px;'
)
)?>
现在,“学校id”是需要更新的下拉列表
我已经给了你所需要的一切
祝您好运谢谢您的快速回复。我已经登录了该wiki。该wiki是两个相互依赖的下拉列表。但我希望获取在_form.php页面的下拉列表中选中的特定记录的值。您能按照那里的要求用我的两个模型名称编辑您的答案吗。对不起,我有点抱歉还有一件事,你在控制器中提到的代码应该是
actionCreate
,或者我必须再次为此创建一个函数。你必须创建另一个操作来响应AJAX
调用。我用现在的知识做了大部分工作。现在由你来尝试并发布你的努力。我们都很忙,但我们想帮助你,所以你必须自己做你的一部分。询问是否有不清楚的地方。我将您的代码与_form.php文件一起使用,并在控制器中使用了actiondiscor()
来使用控制器中要使用的代码。当我选择选项[student name]时,它显示了一个错误,可以通过firefox控制台看到。该错误类似于“NetworkError:500 PHP错误-http://localhost/SchoolApps/index.php?r=students/DisCoor“
。错误是因为我认为该链接中没有创建此类文件。那么如何解决此问题?
$model = School::model()->findByPk($_POST['Jobs']['student_id']);
$data=CHtml::listData($data,'id','name');
foreach($data as $value=>$name)
{
echo CHtml::tag('option',
array('value'=>$value),CHtml::encode($name),true);
}