Get Widget

Thursday, July 24, 2014

Populate/change list of value in dropdown/select box via ajax

 <script>  
 /* to populate list of value in dropdown via ajax */  
 function populateSectionsList(id)  
 {  
      var idval = id;       
      var setOptTemp = "<option value='%d'>%s</option>";  
      var html = "<option value=''>-- Select --</option>";  
      $.ajax({  
           type:'post',  
           url:'http://localhost/myproject/ajaxurl',  
           data:{'id':idval, 'action':edit},  
           error:function(){  
                alert('You have error. Please try again');  
           },  
           success:function(data){  
                var optionList = $.parseJSON(data);  
                $.each(optionList, function(i, optDetails){  
                     html += setOptTemp.replace('%d', optDetails.id).replace('%s', optDetails.name);  
                });  
                $('#dropid').html(html).show();                 
           }  
      });  
 }  
 </script>  
 //myprojet/ajaxurl.php  
 <?php  
 foreach($dropListArr as $value)  
 {  
      $dataArr[] = array('id' => $value['id'], 'name' => $value['textname']);  
 }  
 ?>  

No comments:

Post a Comment

 
follow me on Twitter
Get Widget