Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [HTML][PHP]$_GET
Forum PHP.pl > Forum > Przedszkole
cniak
Siema, mam problem, o funkcji $_GET czytalem duzo, a nie wiem jak zrobić więc tutaj napisałem.Mam wyszukiwarke, chce jeżeli klikne w "szukaj" otworzylo mi się to w danym divie a nie na nowej stronie bez tresci strony. Przykladem takiej wyszukiwarki moze byc nawet http://www.dobreprogramy.pl/ wpisuje tekst, klikam szukaj i w danym divie otwiera mi sie zawartosc wyszukiwania, a nie poprostu sam tekst wyszukania. Wiem, ze rozwiazaniem moze byc poprostu dania w index.php kodu

  1. if($_GET['id']=="szukaj"){
  2. include("search.php");
  3. }


a na stronie glownej umiescic tylko hiperlacze do search.php
  1. <a href="index.php?id=szukaj">wyszukiwarka</a>

Ale chce się nauczyc czego innego, szukam innych rozwiazan, ale jeszcze poczatkowych (nie wszystko odrazu biggrin.gif)

Z góry dzięki smile.gif
Crozin
Funkcja $_GET? blink.gif

Pomysł z tym kodem co tam podałeś na początku jest "dla prostych stron" wystarczający... nie wiem dlaczego musiałbyś robić jakiś odnośnik... niech po prostu formularz kieruje na ...?id=szukaj.
cniak
tak, chce aby kierowal do ?id=szukaj, ale odrazu z wyszukana fraza :/

zapomnialem dodac, ze mam wyszukiwarke JSSEARCH, podam teraz tresc searchENGINE.js

  1. // These are the available "error strings" you can change them to affect the output
  2. // of the search engine.
  3. document.writeln("<i>");
  4. ERR_NoOptions = "Błąd";
  5. ERR_NoSearchTerms = "Wpisz nazwe";
  6. ERR_NoResults = "Nie znaleziono";
  7.  
  8. document.writeln("</i>");
  9. // Performs an actual search and then returns the index number(s) in the db array
  10. // where it found this element.
  11. // keywords = the string they searched for (each space-separated word
  12. // is searched for separately)
  13. // options can be
  14. // 1 = search keywords, not description, not heading
  15. // 2 = search keywords, search description, not heading
  16. // 3 = search all
  17. function performSearch(keywords, options) {
  18. // Check to make sure they entered some search terms
  19. if (!keywords || keywords.length == 0) {
  20. return ERR_NoSearchTerms;
  21. }
  22.  
  23. // Determine where to check for the keywords entered
  24. if (options == 0) {
  25. return ERR_NoOptions;
  26. }
  27. else if (options == 1) {
  28. searchKeywords = true;
  29. searchDescription = false;
  30. searchHeading = false;
  31. }
  32. else if (options == 2) {
  33. searchKeywords = true;
  34. searchDescription = true;
  35. searchHeading = false;
  36. }
  37. else if (options == 3) {
  38. searchKeywords = true;
  39. searchDescription = true;
  40. searchHeading = true;
  41. }
  42.  
  43. // Setting up the keywords array for searching
  44. // Remove common punctuation
  45. keywords = keywords.replace("\.,'", "");
  46.  
  47. // get them all into an array so we can loop thru them
  48. // we assume a space was used to separate the terms
  49. searchFor = keywords.split(" ");
  50.  
  51. // This is where we will be putting the results.
  52. results = new Array();
  53.  
  54. // Loop through the db for potential results
  55. // For every entry in the "database"
  56. for (sDB = 0; sDB < searchDB.length; sDB++) {
  57. // For every search term we are working with
  58. for (t = 0; t < searchFor.length; t++) {
  59. // Check in the heading for the term if required
  60. if (searchHeading) {
  61. if (searchDB[sDB].heading.toLowerCase().indexOf(searchFor[t].toLowerCase()) != -1) {
  62. if (!in_array(String(sDB), results)) {
  63. results[results.length] = String(sDB);
  64. }
  65. }
  66. }
  67.  
  68. // Check in the keywords for the term if required
  69. if (searchKeywords) {
  70. if (searchDB[sDB].terms.toLowerCase().indexOf(searchFor[t].toLowerCase()) != -1) {
  71. if (!in_array(String(sDB), results)) {
  72. results[results.length] = String(sDB);
  73. }
  74. }
  75. }
  76.  
  77. // Check in the description for the term if required
  78. if (searchDescription) {
  79. if (searchDB[sDB].description.toLowerCase().indexOf(searchFor[t].toLowerCase()) != -1) {
  80. if (!in_array(String(sDB), results)) {
  81. results[results.length] = String(sDB);
  82. }
  83. }
  84. }
  85. }
  86. }
  87.  
  88. if (results.length > 0) {
  89. return results;
  90. }
  91. else {
  92. return ERR_NoResults;
  93. }
  94. }
  95.  
  96. // Constructor for each search engine item.
  97. // Used to create a record in the searchable "database"
  98. function searchOption(heading, terms, description, url) {
  99. this.heading = heading;
  100. this.terms = terms;
  101. this.description = description;
  102. this.url = url;
  103. return this;
  104. }
  105.  
  106. // Returns true or false based on whether the specified string is found
  107. // in the array.
  108. // This is based on the PHP function of the same name.
  109. // stringToSearch = the string to look for
  110. // arrayToSearch = the array to look for the string in.
  111. function in_array(stringToSearch, arrayToSearch) {
  112. for (s = 0; s < arrayToSearch.length; s++) {
  113. if (arrayToSearch[s].indexOf(stringToSearch) != -1) {
  114. return true;
  115. }
  116. }
  117. return false;
  118. }
  119.  
  120. function is_array(array) { return !( !array || (!array.length || array.length == 0) || typeof array !== 'object' || !array.constructor || array.nodeType || array.item ); }
  121.  
  122. // This function grabs a specified value from a GET-style URL
  123. // you call it with the name of the variable and it returns
  124. // the value, or false if not found.
  125. function getURLvalue(getName) {
  126. var i, pos, argname, argvalue, queryString, pairs;
  127.  
  128. // get the string following the question mark
  129. queryString = location.href.substring(location.href.indexOf("?")+1);
  130.  
  131. // split parameters into pairs, assuming pairs are separated by ampersands
  132. pairs = queryString.split("&");
  133.  
  134. // for each pair, we get the name and the value
  135. for (i = 0; i < pairs.length; i++) {
  136. pos = pairs[i].indexOf('=');
  137. if (pos == -1) {
  138. continue;
  139. }
  140. argname = pairs[i].substring(0,pos);
  141. argvalue = pairs[i].substring(pos+1);
  142.  
  143. // Replaces "Google-style" + signs with the spaces
  144. // they represent
  145. if (argname == getName) {
  146. return unescape(argvalue.replace(/\+/g, " "));
  147. }
  148. }
  149. return false;
  150. }
  151.  
  152. // Function to execute when the focus is passed to the search filed.
  153. // It just selects everything in the field, which will mean that if
  154. // the user clicks the search field then types, the placeholder text
  155. // will be removed automagically :)
  156. // call using onFocus="java script:searchFocus(this);
  157. function searchFocus(searchField) {
  158. searchField.focus();
  159. searchField.select();
  160. }


oraz search.php:

  1. <html>
  2. <head>
  3. <title>Search Example</title>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <script language="JavaScript1.2" src="searchEngine.js"></script>
  6. <script language="JavaScript1.2" src="searchDB.js"></script>
  7. <style type="text/css">
  8. .result { margin: 10px 0; }
  9. .result-title { font-weight: bold; }
  10. .result-desc { font-size: 90%; }
  11. #conterbody {
  12. background: white;
  13. }
  14. #conter {
  15. background: #e4ecf4;
  16. color: black;
  17. border: 1px solid black;
  18. margin-left: 25%;
  19. width: 50%;
  20. margin-top: 100px;
  21. padding: 10px;
  22. }
  23. #conter a {
  24. color: blue;
  25. }
  26. </style>
  27. </head>
  28. <body bgcolor="#FFFFFF" text="#000000">
  29. <script type="text/javascript">
  30. <!--
  31.  
  32. function searchForm() {
  33. /////////////////////////////////////////////////////////////
  34. // This is the search form, plus anything else to display when
  35. // the user hasn't searched yet. *MUST* use "get" as the method on the form and "searchTxt" as the search field name
  36. document.writeln("<form name=\"search\" action=\"search.php\" method=\"get\">");
  37. document.writeln("     <input type=\"text\" name=\"searchTxt\"value=\"wpisz kraj lub stolicę\" onclick=\"if(this.value=='wpisz kraj lub stolicę') this.value=''\" onblur=\"if(this.value=='') this.value='wpisz kraj lub stolicę'\" />");
  38. document.writeln("<input type=\"submit\" value=\"szukaj\" name=\"search\" />");
  39.  
  40. document.writeln("Przy kożystaniu z IE nie naciskaj ENTER tylko klikaj SZUKAJ");
  41. document.writeln("</form>");
  42. /////////////////////////////////////////////////////////////
  43. }
  44. if (getURLvalue('search')) {
  45. document.writeln("<div id=\"conterbody\"><div id=\"conter\">");
  46. }
  47. if (getURLvalue('search')) {
  48. // Do the search
  49. results = performSearch(getURLvalue("searchTxt"), 3);
  50.  
  51. if (results) {
  52. // This means that there are search results to be displayed.
  53. // Loop through them and make it pretty! :)
  54. if (is_array(results)) {
  55. document.writeln("<i>Znaleziono</i> ["+ results.length +"] <i>podpowiedzi</i>:<br>\n");
  56. document.writeln("<ol>");
  57. for (r = 0; r < results.length; r++) {
  58. result = searchDB[results[r]];
  59.  
  60. /////////////////////////////////////////////////////////////
  61. // This is where you modify the formatting of the results
  62. document.writeln("<li class=\"result\"><div class=\"result-title\"><a href=\"" + result.url + "\">" + result.heading + "</a></div>");
  63. document.writeln("<div class=\"result-desc\">" + result.description + "</div></li>");
  64. /////////////////////////////////////////////////////////////
  65. }
  66. document.writeln("</ol>");
  67. }
  68. // If it's not an array, then we got an error message, so display that
  69. // rather than results
  70. else {
  71.  
  72. document.writeln("<i>" + results + "</i>");
  73. document.writeln("<br />");
  74. }
  75.  
  76. document.writeln(" <form name=\"search\" action=\"search.php\" method=\"get\">  <input type=\"text\" name=\"searchTxt\" /><input type=\"submit\" value=\"szukaj\" name=\"search\" /></form><br><br><a href=\"index.php\">wróc</a></div></div>\n");
  77. } else {
  78. searchForm();
  79. }
  80. } else {
  81. searchForm();
  82. }
  83.  
  84. // -->
  85. </script>
  86. </body>
  87. </html>
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-2025 Invision Power Services, Inc.