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; ?>   
3
  • You should take a look to this solution on how to populate an select from an ajax request. You just have to adapt it a bit for symfony (nothing hard). Commented May 27, 2013 at 7:36
  • I think that @Chaity doesn't need to populate the <select> with data but to select a specific option based on the return of the AJAX call. @Charity your code should work. Did you check if you get any errors on the console? Can you paste also how exactly the <select id="application_campus_campus_id"> looks like and what is the value of data (what you get in the alert box? Commented May 27, 2013 at 8:22
  • try to change your alert with a console.log(). Sometimes alerts brake something Commented May 27, 2013 at 15:11

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.