Advertisement
Guest User

Untitled

a guest
Jan 30th, 2017
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.89 KB | None | 0 0
  1. <html>
  2. <head>
  3. <link rel="stylesheet" type="text/css" href="select_style.css">
  4. <script type="text/javascript" src="jquery.js"></script>
  5. <script type="text/javascript">
  6. function fetch_select(val)
  7. {
  8. $.ajax({
  9. type: 'post',
  10. url: 'fetch_data.php',
  11. data: {
  12. get_option:val
  13. },
  14. success: function (response) {
  15. document.getElementById("new_select").innerHTML=response;
  16. }
  17. });
  18. }
  19.  
  20. </script>
  21.  
  22. </head>
  23. <body>
  24. <p id="heading">Select Subject for Timesheet</p>
  25. <center>
  26. <div id="select_box">
  27. <select onChange="fetch_select(this.value);">
  28. <option>Select Stream</option>
  29. <?php
  30. $host = 'localhost';
  31. $user = 'xx';
  32. $pass = 'zzz';
  33. mysql_connect($host, $user, $pass);
  34. mysql_select_db('sample');
  35.  
  36. $select=mysql_query("select stream from streamSub group by stream");
  37. while($row=mysql_fetch_array($select))
  38. {
  39. echo "<option>".$row['stream']."</option>";
  40. }
  41.  
  42. ?>
  43. </select>
  44.  
  45. <select id="new_select">
  46. </select>
  47. </div>
  48. </center>
  49. </body>
  50. </html>
  51.  
  52. <?php
  53. if(isset($_POST['get_option']))
  54. {
  55. $host = 'localhost';
  56. $user = 'XX';
  57. $pass = 'ZZZ';
  58. mysql_connect($host, $user, $pass);
  59. mysql_select_db('sample');
  60.  
  61. $stream = $_POST['get_option'];
  62. $find=mysql_query("select subject from streamSub where stream='$stream'");
  63. while($row=mysql_fetch_array($find))
  64. {
  65. echo "<option>".$row['subject']."</option>";
  66. }
  67.  
  68. exit;
  69. }
  70. ?>
  71.  
  72. <!DOCTYPE html>
  73. <html>
  74. <head>
  75. <meta charset="utf-8">
  76. <title>Fetch Data Using Ajax</title>
  77. <style type='text/css'>
  78. .hide{
  79. display: none;
  80. }
  81. .show{
  82. display: block;
  83. }
  84. </style>
  85. </head>
  86. <body>
  87. <form>
  88. <div>
  89. <label>
  90. Stream
  91. </label>
  92. <select id="stream">
  93. <option value="">Select Stream</option>
  94. <option value="CSE">CSE</option>
  95. <option value="ECE">ECE</option>
  96. </select>
  97. </div>
  98. <div>
  99. <label>
  100. Semester
  101. </label>
  102. <select id="sem">
  103. <option value="">Select Semester</option>
  104. <option value="1">1</option>
  105. <option value="2">3</option>
  106. <option value="3">5</option>
  107. </select>
  108. </div>
  109. <div id="subjectDiv" class="hide">
  110. <label>
  111. Subject
  112. </label>
  113. <select id='subject'>
  114.  
  115. </select>
  116. </div>
  117. </form>
  118. </body>
  119. <script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.1.1.min.js"></script>
  120. <script>
  121. $(document).ready(function(){
  122. $('#sem').change(function(){
  123. $.ajax({
  124. url:'getData.php',
  125. type:'POST',
  126. data:{
  127. stream : $('#stream').val(),
  128. sem : $('#sem').val()
  129. },
  130. success:function(result){
  131. console.log(result);
  132. $('#subject').html(result);
  133. $('#subjectDiv').removeClass('hide');
  134. $('#subjectDiv').addClass('show');
  135. }
  136. });
  137. });
  138. });
  139.  
  140. <?php
  141.  
  142. $con = mysqli_connect('localhost','root','','test');
  143.  
  144. $query = "SELECT * FROM getdata WHERE stream = '".$_POST['stream']."' AND sem = ".$_POST['sem'];
  145.  
  146. $row = mysqli_query($con,$query);
  147.  
  148. echo "<option value=''>Select Subject</option>";
  149. while($data = mysqli_fetch_assoc($row)){
  150. echo "<option value='".$data['subject']."'>".$data['subject']."</option>";
  151. }
  152. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement