Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Ajax Auto Suggest v.2 - potrzebuje pomocy
Forum PHP.pl > Forum > XML, AJAX > AJAX
Shocter
Witam, potrzebuje pomocy z tym skryptem http://www.brandspankingnew.net/archive/20...suggest_v2.html Otóż zainstalowałem go wszystko działa tak jak chciałem tylko potrzebuje jednej rzeczy. Kiedy wyświetlają się wyniki to po kliknięciu zostają przepisane do pola tekstowego. Ja natomiast potrzebuję aby po kliknięciu w wynik przeniosło do jakiejś strony. Byłbym bardzo wdzięczny gdyby ktoś mógł go przerobić. Z góry dziękuję za pomoc.
sowiq
http://www.brandspankingnew.net/specials/a...tocomplete.html
Cytat
In the JSON example above a callback function is passed to the autoSuggest instance. It is called when the user selects an entry, and inserts the entry id into a hidden field (visible for this example).


Cytat
Options
The options object can contain the following properties:
...
callback | Function | A function taking one argument: an object
Shocter
Gdzie mogę wstawić link?
phpion
Dostałeś podpowiedź. Zaglądając do źródła strony widzisz:
[JAVASCRIPT] pobierz, plaintext
  1. callback: function (obj) { document.getElementById('testid').value = obj.id; }
[JAVASCRIPT] pobierz, plaintext

Pogłówkuj trochę i zmień sobie callbacka na przekierowanie na adres (document.location.href).
Shocter
Zrobiłem coś takiego i nadal nie działa, proszę wytłumacz mi to dokładniej. Jestem laikiem jeśli chodzi o js i php.

[JAVASCRIPT] pobierz, plaintext
  1. callback: function (obj) { document.getElementById('document.location.href').value = obj.id; }
[JAVASCRIPT] pobierz, plaintext


lub tak

[JAVASCRIPT] pobierz, plaintext
  1. callback: function (obj) { document.location.href('testid').value = obj.id; }
[JAVASCRIPT] pobierz, plaintext


Chciałbym coś takiego, że po kliknięciu w wynik wyszukania przekierowywało mnie na np. kontry.polskilol.pl/ahri, a nie dopisywało ? i resztę linku. Da się to przerobić aby w taki sposób działało?
sowiq
Ok, najważniejsze, że coś tam próbujesz.

Zerknij:
[JAVASCRIPT] pobierz, plaintext
  1. callback: function (obj){
  2. // kod tutaj wykona się po wybraniu czegoś z listy podpowiedzi.
  3. // "obj" to prawdopodobnie jeden element odpowiedzi z serwera
  4. }
[JAVASCRIPT] pobierz, plaintext


Ja bym to zrobił jakoś tak:
[JAVASCRIPT] pobierz, plaintext
  1. callback: function (obj){
  2. var url = "/moj_url/"; // początek Twojego URL
  3. url += obj.id; // część pobrana z podpowiedzi z serwera (tutaj ID elementu)
  4. window.location.href = url; // przekierowanie przeglądarki na nowy URL
  5. }
[JAVASCRIPT] pobierz, plaintext
Shocter
Super wielkie dzięki, jeszcze tylko jedno pytanko. Kiedy klikam w link to przekierowuje na adres.pl/1, powiedz mi gdzie ja mogę zmienić id lub zrobić tak, żeby nie przekierowywało na id tylko na nazwę wyszukania.
np. wyszukam Aatrox to żeby przekierowało na adres.pl/aatrox
sowiq
Podejrzewam, że po stronie PHP budujesz jakąś tablicę, którą później kodujesz do JSON. Czyli coś w rodzaju:

  1. $response = array(
  2. array('id' => 1, 'title' => 'Title 1'),
  3. array('id' => 2, 'title' => 'Title 2'),
  4. );
  5. echo json_encode($response);


Musisz do każdego elementu dodać jeszcze pole name (lub np. URL). Więc będzie to wyglądało podobnie:
  1. $response = array(
  2. array('id' => 1, 'title' => 'Title 1', 'name' => 'title_1'),
  3. array('id' => 2, 'title' => 'Title 2', 'name' => 'title_2'),
  4. );
  5. echo json_encode($response);


A później w JS:
[JAVASCRIPT] pobierz, plaintext
  1. // ZAMIAST:
  2. url += obj.id;
  3.  
  4. // ROBISZ:
  5. url += obj.name;
[JAVASCRIPT] pobierz, plaintext
Shocter
Ok, tylko w którym miejscu dokładnie mam wkleić kod php

  1. <?php
  2.  
  3. /*
  4. note:
  5. this is just a static test version using a hard-coded countries array.
  6. normally you would be populating the array out of a database
  7.  
  8. the returned xml has the following structure
  9. <results>
  10. <rs>foo</rs>
  11. <rs>bar</rs>
  12. </results>
  13. */
  14.  
  15. $aUsers = array(
  16. "Aatrox",
  17. "Altman, Alisha",
  18. "Archibald, Janna",
  19. "Auman, Cody",
  20. "Bagley, Sheree",
  21. "Ballou, Wilmot",
  22. "Bard, Cassian",
  23. "Bash, Latanya",
  24. "Beail, May",
  25. "Black, Lux",
  26. "Bloise, India",
  27. "Blyant, Nora",
  28. "Bollinger, Carter",
  29. "Burns, Jaycob",
  30. "Carden, Preston",
  31. "Carter, Merrilyn",
  32. "Christner, Addie",
  33. "Churchill, Mirabelle",
  34. "Conkle, Erin",
  35. "Countryman, Abner",
  36. "Courtney, Edgar",
  37. "Cowher, Antony",
  38. "Craig, Charlie",
  39. "Cram, Zacharias",
  40. "Cressman, Ted",
  41. "Crissman, Annie",
  42. "Davis, Palmer",
  43. "Downing, Casimir",
  44. "Earl, Missie",
  45. "Eckert, Janele",
  46. "Eisenman, Briar",
  47. "Fitzgerald, Love",
  48. "Fleming, Sidney",
  49. "Fuchs, Bridger",
  50. "Fulton, Rosalynne",
  51. "Fye, Webster",
  52. "Geyer, Rylan",
  53. "Greene, Charis",
  54. "Greif, Jem",
  55. "Guest, Sarahjeanne",
  56. "Harper, Phyllida",
  57. "Hildyard, Erskine",
  58. "Hoenshell, Eulalia",
  59. "Isaman, Lalo",
  60. "James, Diamond",
  61. "Jenkins, Merrill",
  62. "Jube, Bennett",
  63. "Kava, Marianne",
  64. "Kern, Linda",
  65. "Klockman, Jenifer",
  66. "Lacon, Quincy",
  67. "Laurenzi, Leland",
  68. "Leichter, Jeane",
  69. "Leslie, Kerrie",
  70. "Lester, Noah",
  71. "Llora, Roxana",
  72. "Lombardi, Polly",
  73. "Lowstetter, Louisa",
  74. "Mays, Emery",
  75. "Mccullough, Bernadine",
  76. "Mckinnon, Kristie",
  77. "Meyers, Hector",
  78. "Monahan, Penelope",
  79. "Mull, Kaelea",
  80. "Newbiggin, Osmond",
  81. "Nickolson, Alfreda",
  82. "Pawle, Jacki",
  83. "Paynter, Nerissa",
  84. "Pinney, Wilkie",
  85. "Pratt, Ricky",
  86. "Putnam, Stephanie",
  87. "Ream, Terrence",
  88. "Rumbaugh, Noelle",
  89. "Ryals, Titania",
  90. "Saylor, Lenora",
  91. "Schofield, Denice",
  92. "Schuck, John",
  93. "Scott, Clover",
  94. "Smith, Estella",
  95. "Smothers, Matthew",
  96. "Stainforth, Maurene",
  97. "Stephenson, Phillipa",
  98. "Stewart, Hyram",
  99. "Stough, Gussie",
  100. "Strickland, Temple",
  101. "Sullivan, Gertie",
  102. "Swink, Stefanie",
  103. "Tavoularis, Terance",
  104. "Taylor, Kizzy",
  105. "Thigpen, Alwyn",
  106. "Treeby, Jim",
  107. "Trevithick, Jayme",
  108. "Waldron, Ashley",
  109. "Wheeler, Bysshe",
  110. "Whishaw, Dodie",
  111. "Whitehead, Jericho",
  112. "Wilks, Debby",
  113. "Wire, Tallulah",
  114. "Woodworth, Alexandria",
  115. "Zaun, Jillie"
  116. );
  117.  
  118.  
  119. $aInfo = array(
  120. "Cos tam cos tam",
  121. "Buckinghamshire",
  122. "Cambridgeshire",
  123. "Cheshire",
  124. "Cornwall",
  125. "Cumbria",
  126. "Derbyshire",
  127. "Devon",
  128. "Dorset",
  129. "Durham",
  130. "East Sussex",
  131. "Essex",
  132. "Gloucestershire",
  133. "Hampshire",
  134. "Hertfordshire",
  135. "Kent",
  136. "Lancashire",
  137. "Leicestershire",
  138. "Lincolnshire",
  139. "Norfolk",
  140. "Northamptonshire",
  141. "Northumberland",
  142. "North Yorkshire",
  143. "Nottinghamshire",
  144. "Oxfordshire",
  145. "Shropshire",
  146. "Somerset",
  147. "Staffordshire",
  148. "Suffolk",
  149. "Surrey",
  150. "Warwickshire",
  151. "West Sussex",
  152. "Wiltshire",
  153. "Worcestershire",
  154. "Durham",
  155. "East Sussex",
  156. "Essex",
  157. "Gloucestershire",
  158. "Hampshire",
  159. "Hertfordshire",
  160. "Kent",
  161. "Lancashire",
  162. "Leicestershire",
  163. "Lincolnshire",
  164. "Norfolk",
  165. "Northamptonshire",
  166. "Northumberland",
  167. "North Yorkshire",
  168. "Nottinghamshire",
  169. "Oxfordshire",
  170. "Shropshire",
  171. "Somerset",
  172. "Staffordshire",
  173. "Suffolk",
  174. "Surrey",
  175. "Warwickshire",
  176. "West Sussex",
  177. "Wiltshire",
  178. "Worcestershire",
  179. "Durham",
  180. "East Sussex",
  181. "Essex",
  182. "Gloucestershire",
  183. "Hampshire",
  184. "Hertfordshire",
  185. "Kent",
  186. "Lancashire",
  187. "Leicestershire",
  188. "Lincolnshire",
  189. "Norfolk",
  190. "Northamptonshire",
  191. "Northumberland",
  192. "North Yorkshire",
  193. "Nottinghamshire",
  194. "Oxfordshire",
  195. "Shropshire",
  196. "Somerset",
  197. "Staffordshire",
  198. "Suffolk",
  199. "Surrey",
  200. "Warwickshire",
  201. "West Sussex",
  202. "Wiltshire",
  203. "Worcestershire",
  204. "Durham",
  205. "East Sussex",
  206. "Essex",
  207. "Gloucestershire",
  208. "Hampshire",
  209. "Hertfordshire",
  210. "Kent",
  211. "Lancashire",
  212. "Leicestershire",
  213. "Lincolnshire",
  214. "Norfolk",
  215. "Northamptonshire",
  216. "Northumberland",
  217. "North Yorkshire",
  218. "Nottinghamshire"
  219. );
  220.  
  221.  
  222. $input = strtolower( $_GET['input'] );
  223. $len = strlen($input);
  224.  
  225.  
  226. $aResults = array();
  227.  
  228. if ($len)
  229. {
  230. for ($i=0;$i<count($aUsers);$i++)
  231. {
  232. // had to use utf_decode, here
  233. // not necessary if the results are coming from mysql
  234. //
  235. if (strtolower(substr(utf8_decode($aUsers[$i]),0,$len)) == $input)
  236. $aResults[] = array( "id"=>($i+1) ,"value"=>htmlspecialchars($aUsers[$i]), "info"=>htmlspecialchars($aInfo[$i]) );
  237.  
  238. //if (stripos(utf8_decode($aUsers[$i]), $input) !== false)
  239. // $aResults[] = array( "id"=>($i+1) ,"value"=>htmlspecialchars($aUsers[$i]), "info"=>htmlspecialchars($aInfo[$i]) );
  240. }
  241. }
  242.  
  243.  
  244.  
  245.  
  246.  
  247. header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
  248. header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified
  249. header ("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
  250. header ("Pragma: no-cache"); // HTTP/1.0
  251.  
  252.  
  253.  
  254. if (isset($_REQUEST['json']))
  255. {
  256. header("Content-Type: application/json");
  257.  
  258. echo "{\"results\": [";
  259. $arr = array();
  260. for ($i=0;$i<count($aResults);$i++)
  261. {
  262. $arr[] = "{\"id\": \"".$aResults[$i]['id']."\", \"value\": \"".$aResults[$i]['value']."\", \"info\": \"\"}";
  263. }
  264. echo implode(", ", $arr);
  265. echo "]}";
  266. }
  267. else
  268. {
  269. header("Content-Type: text/xml");
  270.  
  271. echo "<?xml version=\"1.0\" encoding=\"utf-8\" ?><results>";
  272. for ($i=0;$i<count($aResults);$i++)
  273. {
  274. echo "<rs id=\"".$aResults[$i]['id']."\" info=\"".$aResults[$i]['info']."\">".$aResults[$i]['value']."</rs>";
  275. }
  276. echo "</results>";
  277. }
  278. ?>
sowiq
Trochę debilny ten kawałek kodu, bo tworzy odpowiedź w kompletnie nieczytelny sposób. Ale trudno.

Tutaj jest tworzona odpowiedź w formacie JSON:
  1. $arr[] = "{\"id\": \"".$aResults[$i]['id']."\", \"value\": \"".$aResults[$i]['value']."\", \"info\": \"\"}";


A tutaj XML:
  1. echo "<rs id=\"".$aResults[$i]['id']."\" info=\"".$aResults[$i]['info']."\">".$aResults[$i]['value']."</rs>";


W obu wariantach wstawia się pole "id", "value" oraz "info". Analogicznie dodaj tam swoje pole.
Shocter
Zrobiłem coś takiego i nie wyświetla mi wyników wyszukiwani, jest napisane "No results"
  1. array('id' => 1, 'title' => 'Title 1', 'name' => 'title_1');
,

i coś takiego ale wtedy jak klikam na jakis wynik to mnie przekierowuje na adresstrony.pl/undefined
  1. $arr[] = "{\"id\": \"".$aResults[$i]['id']."\", \"value\": \"".$aResults[$i]['name']."\", \"value\": \"".$aResults[$i]['value']."\", \"info\": \"\"}";
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2024 Invision Power Services, Inc.