Dropdown dependent values to be fetch from multiple models using ajax in Yii

Posted by newuser on Stack Overflow See other posts from Stack Overflow or by newuser
Published on 2012-07-05T12:00:31Z Indexed on 2012/07/06 3:16 UTC
Read the original article Hit count: 325

Filed under:
|
|
|

I searched all the documentation over Yii but not got the answer of it.So I came here finally. I have the following schema

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   |     |                     |                |
+------------------+--------------+------+-----+---------------------+----------------+

I have made models and CRUD for the both models.In models my relation is like this

In Students.php the relation is like

  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'),
    );
  }

In Schools.php the relation is like

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'),
    );
  }

Now I made the two models rendered in a single page so that I can enter all the respective fields in a single form.

 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'); ?>

Now for this piece of code I got all the student name from the Student model. So my problem is when I am getting the student name from the dropdown list and going to select a student it will also fetch all the respective values of the student to be rendered in the _form.php without click on save button.So that user don't have to put it again manually. I think ajax and json encode will work here but don't know how to make them work here.

[Update]

Here is StudentsController code

 public function actionDisCoor() {
    $model = School::model()->findByPk($_POST['Students']['student_id']);
    $data=CHtml::listData($data,'id','name');
    foreach($data as $value=>$name)
    {
      echo CHtml::tag('option',array('value'=>$value),CHtml::encode($name),true);
    }
  }

Here is _form.php code for Students

  <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>

After all that when I saw in firebug I got the error.Here is the screen shot enter image description here

© Stack Overflow or respective owner

Related posts about php

Related posts about mysql