Advertisement
Guest User

Untitled

a guest
Jul 26th, 2014
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.86 KB | None | 0 0
  1. This is my index.php code
  2. -------------------------
  3. <!DOCTYPE html>
  4. <html lang="en">
  5. <head>
  6. <meta charset="utf-8">
  7. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  8. <meta name="viewport" content="width=device-width, initial-scale=1">
  9. <title>Index</title>
  10.  
  11. <!-- Bootstrap -->
  12. <link href="css/bootstrap-3.2.min.css" rel="stylesheet">
  13. <link href="css/jquery-ui-1.10.4.custom.min.css" rel="stylesheet">
  14.  
  15.  
  16. <style>
  17.  
  18. </style>
  19.  
  20.  
  21. </head>
  22. <body>
  23.  
  24. <div class="container">
  25. <div class="row" >
  26. <form id="" class="search-form" action="result.php" method="POST">
  27. <div class="col-md-12">
  28.  
  29. <input type="text" id="txt-city" name="txt-city" placeholder="Enter City">
  30. <input type="hidden" id="txt-city-id" name="txt-city-id">
  31.  
  32. <div class="row search-input" > <!-- Nested Row -->
  33. <div class="col-md-7 search-bar">
  34. <input type="text" id="txt-keywords" placeholder="Enter Product or Service"></div>
  35.  
  36. <div class="col-md-3 search-bar">
  37. <input type="text" id="txt-area" placeholder="Enter Location"></div>
  38. <input type="hidden" id="txt-area-id" name="txt-area-id">
  39.  
  40. <div class="col-md-2 search-submit">
  41. <button id="btn-search" class="search-button">Search</button></div>
  42. </div>
  43. </div>
  44. </form>
  45. </div>
  46. </div>
  47.  
  48.  
  49.  
  50. <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js">
  51.  
  52. </script>
  53.  
  54. <script src="js/bootstrap-3.2.min.js"></script>
  55.  
  56. <script src="js/jquery-ui-1.10.4.custom.min.js"></script>
  57.  
  58.  
  59. <script type="text/javascript">
  60.  
  61.  
  62. $(document).ready(function() {
  63.  
  64. var city = [
  65. { "id": 2001, "value": "Ahmedabad" },
  66. { "id": 1269, "value": "Bangalore"},
  67. { "id": 2002, "value": "Bhopal"},
  68. { "id": 2003, "value": "Chennai" },
  69. { "id": 2004, "value": "Coimbatore"},
  70. { "id": 2005, "value": "Delhi"},
  71. { "id": 348, "value": "Hyderabad" },
  72. { "id": 2007, "value": "Indore"},
  73. { "id": 2008, "value": "Jaipur"},
  74. { "id": 2009, "value": "Kolkata" },
  75. { "id": 2010, "value": "Luknow"},
  76. { "id": 2011, "value": "Ludiana"},
  77. { "id": 2012, "value": "Mumbai" },
  78. { "id": 2013, "value": "Nagpur"},
  79. { "id": 2014, "value": "Nashik"},
  80. { "id": 2015, "value": "Pune" },
  81. { "id": 2016, "value": "Rajkot"},
  82. { "id": 2017, "value": "Surat"}
  83. ];
  84.  
  85. ***if( $("#txt-city").click() ) {
  86.  
  87. url = city;
  88. }else{
  89. url = getCity.php
  90. }***
  91.  
  92. $("#txt-city").autocomplete({
  93.  
  94. source: url,
  95. select: function(event, ui) {
  96.  
  97. $( "#txt-city-id" ).val(ui.item.id);
  98. //alert(is_city_selected);
  99. },
  100. change: function( event, ui ) {
  101.  
  102. $("#txt-city-id").val(ui.item.id);
  103.  
  104. // clear the txt-area textbox
  105. $("#txt-area").val('');
  106.  
  107. // send the city id to fetch areas in it
  108. secondary_url = "getArea2.php?filter=" + ui.item.id;
  109.  
  110. $("#txt-area").autocomplete("option", "source", secondary_url);
  111.  
  112. },
  113. minLength: 1
  114. });
  115.  
  116. //alert(global_city);
  117.  
  118. $("#txt-area").autocomplete({
  119.  
  120. source: "",
  121. change: function( event, ui ) {
  122.  
  123. $("#txt-area-id").val(ui.item.id);
  124. },
  125. minLength: 1
  126. });
  127.  
  128. }); // end of the jQuery() function
  129.  
  130. </script>
  131.  
  132. </html>
  133.  
  134. </body>
  135.  
  136. -------------------------
  137. This is getCity.php code
  138. -------------------------
  139. This code fetches the cities from cities table.
  140.  
  141. <?php
  142.  
  143. /* Connection vars here for example only. Consider a more secure method. */
  144. $dbhost = 'localhost';
  145. $dbuser = 'abc';
  146. $dbpass = 'abc123';
  147. $dbname = 'mydb1';
  148.  
  149. try {
  150. $conn = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
  151. }
  152. catch(PDOException $e) {
  153. echo $e->getMessage();
  154. }
  155.  
  156. $return_arr = array();
  157.  
  158. if ($conn){
  159. //$ac_filter = $_GET['filter'];
  160. $ac_term = $_GET['term']."%";
  161. $query = "SELECT city_id, city_name FROM tbl_cities WHERE city_name like :term
  162.  
  163. LIMIT 20";
  164.  
  165. $result = $conn->prepare($query);
  166. //$result->bindValue(":filter",$ac_filter);
  167. $result->bindValue(":term",$ac_term);
  168. $result->execute();
  169.  
  170. /* Retrieve and store in array the results of the query.*/
  171. while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
  172. $row_array['value'] = $row['city_name'];
  173. $row_array['id'] = $row['city_id'];
  174.  
  175. array_push($return_arr,$row_array);
  176. }
  177.  
  178.  
  179.  
  180. }
  181.  
  182. /* Free connection resources. */
  183. $conn = null;
  184.  
  185. /* Toss back results as json encoded array. */
  186. echo json_encode($return_arr);
  187.  
  188. ?>
  189.  
  190. --------------------------------
  191. This is getArea.php
  192. -------------------------------
  193. This code fetches the areas from areas table based on the city_id passed to it.
  194.  
  195. <?php
  196.  
  197. /* Connection vars here for example only. Consider a more secure method. */
  198. $dbhost = 'localhost';
  199. $dbuser = 'abc';
  200. $dbpass = 'abc123';
  201. $dbname = 'mydb1';
  202.  
  203. try {
  204. $conn = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
  205. }
  206. catch(PDOException $e) {
  207. echo $e->getMessage();
  208. }
  209.  
  210. $return_arr = array();
  211.  
  212. if ($conn){
  213.  
  214. $ac_filter = $_GET['filter'];
  215. $ac_term = $_GET['term']."%";
  216.  
  217. $query = "SELECT area_id, area_name FROM tbl_areas WHERE city_id = :filter AND
  218. area_name like :term LIMIT 15";
  219.  
  220. $result = $conn->prepare($query);
  221. $result->bindValue(":filter",$ac_filter);
  222. $result->bindValue(":term",$ac_term);
  223. $result->execute();
  224.  
  225. /* Retrieve and store in array the results of the query.*/
  226. while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
  227. $row_array['value'] = $row['area_name'];
  228. $row_array['id'] = $row['area_id'];
  229.  
  230. array_push($return_arr,$row_array);
  231. }
  232.  
  233. }
  234.  
  235. /* Free connection resources. */
  236. $conn = null;
  237.  
  238. /* Toss back results as json encoded array. */
  239. echo json_encode($return_arr);
  240.  
  241. ?>
  242. --------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement