0

try to write code for dynamic dependent select using jquery ajax in symfony1.4 form templates. I wrote code where i could take 1st fields value which is selected by the user from a dropdown list and pass it to my php function to fetch the data which is used for select the 2nd dropdown list value. i can pass the return value to ajax function from php function but i cant select my dropdown list value using this value

here i gave my code. please give suggestion.

Code:

_form.php

<script type="text/javascript">
    jQuery(document).ready(function() {
    $("#application_program_institution_program_id").change(function(){
            var id=$(this).val();
            if(id != '')  {
              $.ajax({
                    type: "POST",
                    url: '<?php echo url_for('Application/Program'); ?>'+ '?id=' + id,      
                    cache: false,
                    success: function(data)
                    {
                          alert(data);// its show my returned value

                         $("#application_campus_campus_id").val(data);// but it does not select the value of dropdown list.

                    }   
                });
            }
        });
    });

</script>

Applicationaction class

public function executeProgram(sfWebRequest $request) {
        $id = intval($request->getParameter('id'));
        $campusList = InstitutionCampus::getCampus($id);
        return $this->renderPartial('result', array('campusList' => $campusList));

    }

getCampus Code:

 public static function getCampus($id) {
         $resultset = Doctrine_Query::create()
                    ->select('ip.id as id, ip.program_code as title, ic.id as ip, ic.campus_name as campusname, icp.institution_campus_id')
                    ->from('InstitutionCampusProgram icp')
                    ->innerJoin('icp.InstitutionProgram ip')
                    ->innerJoin('ip.Institution i')
                    ->innerJoin('icp.InstitutionCampus ic')
                    ->where('ip.all_campus = ?', '0')
                    ->andWhere('i.institution_code = ?', CodeUtil::UttaraUniversity)
                    ->andWhere('icp.institution_program_id =?', $id)
                    ->execute();
             foreach ($resultset as $prog){
                 $result = $prog->ip;
             }


        return $result;         
     }

}

_result.php code:

<?php echo $campusList; ?>   
Vao Tsun
  • 47,234
  • 13
  • 100
  • 132
Chaity
  • 27
  • 1
  • 6
  • You should take a look to this solution on [how to populate an select from an ajax request](http://stackoverflow.com/a/1745745/569101). You just have to adapt it a bit for symfony (nothing hard). – j0k May 27 '13 at 07:36
  • I think that @Chaity doesn't need to populate the ` looks like and what is the value of `data` (what you get in the alert box? – Michal Trojanowski May 27 '13 at 08:22
  • try to change your alert with a console.log(). Sometimes alerts brake something – ilSavo May 27 '13 at 15:11

1 Answers1

2

well, the above should work, but try this as well:

<script type="text/javascript">
jQuery(document).ready(function() {
$("#application_program_institution_program_id").change(function(){
    var id=$(this).val();
        if(id != '')  {
        var r =  $.ajax({
                type: "POST",
                url: '<?php echo url_for('Application/Program'); ?>'+ '?id=' + id,      
            });
            $("#application_campus_campus_id").val(r);
        }
    });
});

Also change your getCampus.php code last line from return $result to return $result[0], if single value is returned, else keep in mind that you are returning an array and you should have to change php array to javascript array which you can do using json_encode.