Mysql
 sql >> डेटाबेस >  >> RDS >> Mysql

पहले चयन पर दूसरे चयन का मान बदलें

जैसा कि आपने कहा था कि आपके पास jQuery या अजाक्स के साथ अनुभव नहीं है, मैं अपने उत्तर को यथासंभव अधिक टिप्पणियों के साथ पोस्ट करूंगा। मैं मान लूंगा कि आपके पृष्ठ में दो चुनिंदा ड्रॉपडाउन हैं।

पहले वाले में बिल्डर्स होते हैं, इसलिए इसमें id="builders" . होगा ।

दूसरे में क्षेत्र शामिल हैं, इसलिए इसमें id="regions" . होंगे ।

जो मैं समझता हूं, पहला चयन ठीक वही होगा जिसे आपने अपने प्रश्न में पोस्ट किया था, जेनरेट किया गया सर्वर-साइड (PHP द्वारा)। मैं केवल यह पूछता हूं कि आप कृपया इसमें थोड़ा सा बदलाव करें, जिससे प्रत्येक विकल्प मान बिल्डर के डेटाबेस आईडी के बराबर हो, न कि उसका नाम (जब तक कि बिल्डर की प्राथमिक कुंजी उनका नाम न हो, न कि आईडी)। इससे अंतिम उपयोगकर्ता के लिए कोई फर्क नहीं पड़ेगा लेकिन यह हमारे jQuery समाधान के लिए महत्वपूर्ण होगा। दूसरा खाली होगा, क्योंकि पहले ड्रॉपडाउन में चयनित बिल्डर से संबंधित क्षेत्रों के साथ इसे गतिशील रूप से भरने का विचार है।

अब चलिए jQuery कोड पर आते हैं:

//Everything that goes below this first line will be ready as soon as the page is fully loaded
$(document).ready(function() {
  //The following code defines an event. More precisely, we are defining that the code inside it will run every time our select with id "builders" has its value changed
  $('#builders').change(function() {
    //$(this) is our builders select. $(this).val() contains the selected value, which is the ID of our selected builder
    var currentValue = $(this).val();
    //Now, this is our Ajax command that will invoke a script called get_regions.php, which will receive the builder's ID in $_GET['builder_id'] and you can use to query your database looking for all regions related to this builder. Make sure to create an array with the returned regions. Your get_regions.php's last line should be echo json_encode($regions); 
    $.get("get_regions.php", {'builder_id': currentValue}, function(data) {
      //Inside this function is the code that will run when we receive the response from our PHP script. It contains a JSON encoded list of regions, so first of all we need to parse this JSON
      var regions = $.parseJSON(data);
      //Before filling our second select dropdown with the regions, let's remove all options it already contains, if any
      $('#regions').empty();
      //Now, all there is left is to loop over the regions, adding each as an option of the regions dropdown. I'll do it the universal way
      for (var i = 0; i < regions.length; i++) {
        var regionOption = '<option value="'+regions[i]['region_name']+'">';
        regionOption += regions[i]['region_name'];
        regionOption += '</option>';
        $('#regions').append(regionOption);
      }
    });
  });
});

किसी भी वाक्यविन्यास त्रुटियों के बावजूद (यहां से कोड का परीक्षण नहीं कर सकता) यह चाल चलनी चाहिए। आशा है कि टिप्पणियाँ इतनी स्पष्ट थीं कि आप यह समझ सकें कि jQuery में चीज़ें कैसे काम करती हैं।




  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. php . में समान आईडी वाली पंक्तियों की गणना करें

  2. एक जेपीए/हाइबरनेट एनोटेशन का उपयोग कर एक इंस्टेंस वैरिएबल में एक MySQL char (n) कॉलम को कैसे मैप करें?

  3. ड्रॉप डेटाबेस रिटर्न डेटाबेस को छोड़ने में त्रुटि त्रुटि:66 MySQL में

  4. PHP के साथ MySQL से नेस्टेड मेनू कैसे बनाएं?

  5. अपने मूडल MySQL डेटाबेस का बैकअप कैसे लें