ugrás a tartalomhoz

Szűkített JS keresés DB

makgab · 2012. Jún. 1. (P), 20.09
Üdv!

Korábban találtam egy JS-es kereső szűkítést. Az ország (Country) <select> kiválasztásakor az állam/megye (State) <select> elemeit szűkíti.
Miben különbözik az egész JS/html oldal, ha a select-ekben PHP adatbázisból generált elemek vannak és nem statikus HTML kód? A lenti példában a <select> elemeit a JS tartalmazza. Tehát miben más a JS, ha a <select> elemeit PHP adatbázisból tölti fel?

Itt a statikus HTML kód minta:
  1. <HTML>  
  2. <HEAD>  
  3. <TITLE>The JavaScript Source: Forms : Country State Drop Down</TITLE>  
  4. <META HTTP-EQUIV="The JavaScript Source" CONTENT = "no-cache">  
  5. <META NAME="description" CONTENT="A simple international country and state drop down list that automatically repopulates the state section based on the country selected without page refreshes.">  
  6. <META NAME="date" CONTENT="2005-12-09">  
  7. <META NAME="channel" CONTENT="Developer">  
  8. <META NAME="author" CONTENT="Down Home Consulting">  
  9. <META NAME="section" CONTENT="Forms">  
  10.   
  11. <script type="text/javascript">  
  12. <!--  
  13. // If you have PHP you can set the post values like this  
  14. //var postState = '<?= $_POST["state"] ?>';  
  15. //var postCountry = '<?= $_POST["country"] ?>';  
  16. var postState = '';  
  17. var postCountry = '';  
  18.   
  19. // State table  
  20. //  
  21. var state = '\  
  22. US:AK:Alaska|\  
  23. US:AL:Alabama|\  
  24. US:AR:Arkansas|\  
  25. US:AS:American Samoa|\  
  26. // ...stb.  
  27. ';  
  28.   
  29. // Country data table  
  30. //  
  31. var country = '\  
  32. AF:Afghanistan|\  
  33. AL:Albania|\  
  34. DZ:Algeria|\  
  35. AS:American Samoa|\  
  36. AD:Andorra|\  
  37. AO:Angola|\  
  38. // ...stb.  
  39. ';  
  40.   
  41. function TrimString(sInString) {  
  42.   if ( sInString ) {  
  43.     sInStringsInString = sInString.replace( /^\s+/g, "" );// strip leading  
  44.     return sInString.replace( /\s+$/g, "" );// strip trailing  
  45.   }  
  46. }  
  47.   
  48. // Populates the country selected with the counties from the country list  
  49. function populateCountry(defaultCountry) {  
  50.   if ( postCountry != '' ) {  
  51.     defaultCountry = postCountry;  
  52.   }  
  53.   var countrycountryLineArray = country.split('|');  // Split into lines  
  54.   var selObj = document.getElementById('countrySelect');  
  55.   selObj.options[0] = new Option('Select Country','');  
  56.   selObj.selectedIndex = 0;  
  57.   for (var loop = 0; loop < countryLineArray.length; loop++) {  
  58.     lineArray = countryLineArray[loop].split(':');  
  59.     countryCode  = TrimString(lineArray[0]);  
  60.     countryName  = TrimString(lineArray[1]);  
  61.     if ( countryCode != '' ) {  
  62.       selObj.options[loop + 1] = new Option(countryName, countryCode);  
  63.     }  
  64.     if ( defaultCountry == countryCode ) {  
  65.       selObj.selectedIndex = loop + 1;  
  66.     }  
  67.   }  
  68. }  
  69.   
  70. function populateState() {  
  71.   var selObj = document.getElementById('stateSelect');  
  72.   var foundState = false;  
  73.   // Empty options just in case new drop down is shorter  
  74.   if ( selObj.type == 'select-one' ) {  
  75.     for (var i = 0; i < selObj.options.length; i++) {  
  76.       selObj.options[i] = null;  
  77.     }  
  78.     selObj.options.length=null;  
  79.     selObj.options[0] = new Option('Select State','');  
  80.     selObj.selectedIndex = 0;  
  81.   }  
  82.   // Populate the drop down with states from the selected country  
  83.   var statestateLineArray = state.split("|");  // Split into lines  
  84.   var optionCntr = 1;  
  85.   for (var loop = 0; loop < stateLineArray.length; loop++) {  
  86.     lineArray = stateLineArray[loop].split(":");  
  87.     countryCode  = TrimString(lineArray[0]);  
  88.     stateCode    = TrimString(lineArray[1]);  
  89.     stateName    = TrimString(lineArray[2]);  
  90.   if (document.getElementById('countrySelect').value == countryCode && countryCode != '' ) {  
  91.     // If it's a input element, change it to a select  
  92.       if ( selObj.type == 'text' ) {  
  93.         parentObj = document.getElementById('stateSelect').parentNode;  
  94.         parentObj.removeChild(selObj);  
  95.         var inputSel = document.createElement("SELECT");  
  96.         inputSel.setAttribute("name","state");  
  97.         inputSel.setAttribute("id","stateSelect");  
  98.         parentObj.appendChild(inputSel) ;  
  99.         selObj = document.getElementById('stateSelect');  
  100.         selObj.options[0] = new Option('Select State','');  
  101.         selObj.selectedIndex = 0;  
  102.       }  
  103.       if ( stateCode != '' ) {  
  104.         selObj.options[optionCntr] = new Option(stateName, stateCode);  
  105.       }  
  106.       // See if it's selected from a previous post  
  107.       if ( stateCode == postState && countryCode == postCountry ) {  
  108.         selObj.selectedIndex = optionCntr;  
  109.       }  
  110.       foundState = true;  
  111.       optionCntr++  
  112.     }  
  113.   }  
  114.   // If the country has no states, change the select to a text box  
  115.   if ( ! foundState ) {  
  116.     parentObj = document.getElementById('stateSelect').parentNode;  
  117.     parentObj.removeChild(selObj);  
  118.   // Create the Input Field  
  119.     var inputEl = document.createElement("INPUT");  
  120.     inputEl.setAttribute("id", "stateSelect");  
  121.     inputEl.setAttribute("type", "text");  
  122.     inputEl.setAttribute("name", "state");  
  123.     inputEl.setAttribute("size", 20);  
  124.     inputEl.setAttribute("value", postState);  
  125.     parentObj.appendChild(inputEl) ;  
  126.   }  
  127. }  
  128.   
  129. function initCountry(country) {  
  130.   populateCountry(country);  
  131.   populateState();  
  132. }  
  133. //-->  
  134. </script>  
  135.   
  136.   
  137. </HEAD>  
  138.   
  139. <BODY BGCOLOR=#ffffff vlink=#0000ff>  
  140.   
  141. <BR>  
  142. <center>  
  143.   
  144. <BR>  
  145. <BR>  
  146. <!-- Demonstration -->  
  147. <form>  
  148.   <table border="0">  
  149.   
  150.     <tr>  
  151.       <td>  
  152.         <select id='countrySelect' name='country' onchange='populateState()'>  
  153.         </select>  
  154.         </td>  
  155.         <td>  
  156.         <select id='stateSelect' name='state'>  
  157.         </select>  
  158.         <script type="text/javascript">initCountry('US'); </script>  
  159.   
  160.       </td>  
  161.     </tr>  
  162.   </table>  
  163. </form>  
  164.   
  165.   
  166. </center>  
  167. </body>  
  168. </html>  
 
1

Miben különbözik az egész

Poetro · 2012. Jún. 1. (P), 22.25
Miben különbözik az egész JS/html oldal, ha a select-ekben PHP adatbázisból generált elemek vannak és nem statikus HTML kód.

Semmiben, mivel a PHP is HTML-t generál. Mondjuk jó lenne a kérdő mondatok végén kérdőjel, csak hogy egyértelmű legyen.
2

PHP -> JS

makgab · 2012. Jún. 2. (Szo), 09.56
Mondjuk jó lenne a kérdő mondatok végén kérdőjel, csak hogy egyértelmű legyen.

Ok, javítottam. :)

Tehát a szép, elegáns megoldást kerestem erre. De akkor erre az a szép megoldás, hogy PHP-ból generáljam a JS forrását a <select> elemekhez?
3

Ha több select-ed van és

therest · 2012. Jún. 5. (K), 12.01
Ha több select-ed van és select-ek függnek egymástól, pl Ország->Megye->Település akkor valamiféle ajaxos betöltés a legszebb talán. Így select-ek onchange eseményekor, nem kell az egész oldalt betölteni, és js-t sem kell php-ból html-be nyomni.
JQuery ajax, JSON formátumban való kéréssel, php oldalon meg a megfelelő DB select, és egy echo JSON encode. Az ajax kérés által visszaadott json objektumból aztán könnyedén felépíted a selectjeid javascriptben.

(Nem olvastam végig a teljes forrást, ha valamit félreértettem, akkor bocs.)