populating dropdown using jquery

populating dropdown using jquery

Hi, I am new to jquery. I am trying to populate second drop down based on the value selected on first drop down. I am able to implement up to this point. The form have a continue button. If the user click continue button next page is loaded which have a back button. If the user clicks on back button previously selected values should be automatically selected. I am able to re-select first drop down but second drop down is not getting populated as it is happening on change event of first drop down. Can you please tell me how to modify my code to achieve this.

HTML CODE
  1. <?php 
  2. require_once('../includes/initialize.php');
  3. if (isset($_SERVER['HTTP_REFERER'])){
  4. $source = $_SERVER['HTTP_REFERER'];
  5. preg_match("/http(.*)?\/(.*)/", $source, $matches);
  6. }
  7. if (isset($_POST['submit'])){
  8. $registeration = $_SESSION['registration'];
  9. $registeration['company_category'] = trim($_POST['category']);
  10. $registeration['company_subcategory'] = trim($_POST['subcategory']);
  11. $_SESSION['registration'] = $registeration;
  12. redirect_to('register_step3.php');
  13. }
  14. $company_cats = Companycategory::find_all();
  15. ?>
  16. <!doctype html>
  17. <html>
  18. <head>
  19. <meta charset="utf-8">
  20. <title>Untitled Document</title>
  21. <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
  22. <script>
  23. $(document).ready(function() {
  24.     $("#category").change(function () {   
  25.       var category_id = $(this).val();
  26.        $.ajax({
  27.             type: "POST", 
  28.             url: "../includes/get_company_subcategory_ajax.php?category=" + category_id, 
  29.             dataType: "json",
  30.             success: function(data){
  31.                 //Clear options corresponding to earlier option of first dropdown
  32.                 $('select#subcategory').empty(); 
  33.                 $('select#subcategory').append('<option value="0">Select Option</option>');
  34.                 //Populate options of the second dropdown
  35.                 $.each( data, function(i, val){    
  36.                     $('select#subcategory').append('<option value="'+ val.id +'">'+ val.subcategory +'</option>');
  37.                 });
  38.                 $('select#subcategory').focus();
  39.             },
  40.             beforeSend: function(){
  41.                 $('select#subcategory').empty();
  42.                 $('select#subcategory').append('<option value="0">Loading...</option>');
  43.             },
  44.             error: function(){
  45.                 $('select#subcategory').attr('disabled', true);
  46.                 $('select#subcategory').empty();
  47.                 $('select#subcategory').append('<option value="0">No Options</option>');
  48.             }
  49.         })  
  50.     }); 
  51. });
  52. </script>
  53. </head>
  54. <body>
  55. <h1>Add Your Business</h1>
  56. <form action="register_step2.php" method="post" name="register_step2">
  57.   <table border="0" cellspacing="0" cellpadding="0" width="690">
  58.     <tr>
  59.       <td colspan="2"><p><strong>Search Category</strong></p></td>
  60.     </tr>
  61.     <tr>
  62.     </tr>
  63.     <tr>
  64.       <td>Category:</td>
  65.       <td>
  66.         <select name="category" id="category">
  67.         <option value="0">Select Option</option>
  68.           <?php foreach($company_cats as $company_cat) {?> 
  69.             <option value="<?php echo $company_cat->id;?>"<?php if(isset($_SESSION['registration']['company_category']) && isset($matches[2]) && $matches[2]!="" && $company_cat->id==$_SESSION['registration']['company_category']){echo('selected');}?>><?php echo $company_cat->category;?></option>  
  70.             <?php } ?>  
  71.           </select>
  72.       </td>
  73.     </tr>
  74.     <tr>
  75.       <td>SubCategory:</td>
  76.       <td>
  77.           <select name="subcategory" id="subcategory">
  78.         </select>
  79.      </td>
  80.     </tr>
  81.   </table>
  82.   <p>
  83.     <input type="button" name="button" id="button" value="Back" onClick="window.location = 'register.php';">
  84.     <input type="submit" name="submit" id="submit" value="Continue">
  85.   </p>
  86. </form>
  87. </body>
  88. </html>