Advertisement
Guest User

Untitled

a guest
Apr 1st, 2013
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.02 KB | None | 0 0
  1.  
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5. <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  6. <title>Dynamically populate select by AJAX result - jsFiddle demo</title>
  7.  
  8. <script type='text/javascript' src='http://code.jquery.com/jquery-1.8.3.js'></script>
  9. <link rel="stylesheet" type="text/css" href="/css/normalize.css">
  10.  
  11.  
  12. <link rel="stylesheet" type="text/css" href="/css/result-light.css">
  13.  
  14.  
  15.  
  16. <script type='text/javascript' src="http://ajax.cdnjs.com/ajax/libs/json2/20110223/json2.js"></script>
  17.  
  18.  
  19. <style type='text/css'>
  20.  
  21. </style>
  22.  
  23.  
  24.  
  25. <script type='text/javascript'>//<![CDATA[
  26. $(window).load(function(){
  27. // The data that the service should return
  28. // JSFiddle will echo it back for us on that URL
  29. var doctors = {
  30. success: true,
  31. doctors: [
  32. {
  33. id: 71,
  34. name: "George"
  35. },
  36. {
  37. id: 72,
  38. name: "James"
  39. }
  40. ]
  41. }
  42. var doctors1 = {
  43. success: true,
  44. doctors: [
  45. {
  46. id: 71,
  47. name: "Georgess"
  48. },
  49. {
  50. id: 72,
  51. name: "Jamessss"
  52. }
  53. ]
  54. }
  55. var doctors2 = {
  56. success: true,
  57. doctors: [
  58. {
  59. id: 71,
  60. name: "Samm"
  61. },
  62. {
  63. id: 72,
  64. name: "MArk"
  65. }
  66. ]
  67. }
  68.  
  69. // This is what your JSON from PHP should look like
  70.  
  71.  
  72.  
  73. // Bind change function to the select
  74. jQuery(document).ready(function() {
  75. jQuery("#services").change(onServiceChange);
  76. });
  77.  
  78. function onServiceChange()
  79. {
  80. var serviceId = jQuery(this).val();
  81. if(serviceId=='1'){
  82. var jsonDoctors = JSON.stringify(doctors);
  83. console.log(jsonDoctors);
  84. }else if(serviceId=='2'){
  85. var jsonDoctors = JSON.stringify(doctors1);
  86. console.log(jsonDoctors);
  87. }else{
  88. var jsonDoctors = JSON.stringify(doctors2);
  89. console.log(jsonDoctors);
  90. }
  91. $.ajax({
  92. url: '/echo/json/',
  93. type: 'post',
  94. data: {
  95. serviceId: serviceId,
  96. json: jsonDoctors // jsFiddle echos this back to us
  97. },
  98. success: onServicesRecieveSuccess,
  99. error: onServicesRecieveError
  100. });
  101. }
  102.  
  103. function onServicesRecieveSuccess(data)
  104. {
  105. // Target select that we add the states to
  106. var jTargetSelect = jQuery("#doctors");
  107.  
  108. // Clear old states
  109. jTargetSelect.children().remove();
  110.  
  111. // Add new states
  112. jQuery(data.doctors).each(function(){
  113. jTargetSelect.append('<option value="'+this.id+'">'+this.name+'</option>');
  114. });
  115. }
  116.  
  117. function onServicesRecieveError(data)
  118. {
  119. alert("Could not get services. Please try again.");
  120. }
  121. });//]]>
  122.  
  123. </script>
  124.  
  125.  
  126. </head>
  127. <body>
  128. <select id="services">
  129. <option>Click to select a service</option>
  130. <option value="1">Service 1</option>
  131. <option value="2">Service 2</option>
  132. <option value="3">Service 3</option>
  133. </select>
  134.  
  135. <hr>
  136.  
  137. <select id="doctors">
  138. </select>
  139.  
  140. </body>
  141.  
  142.  
  143. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement