2017-02-18 5 views
1

Dies ist mein Code, die Standorte erzeugt:Wie Wählen Sie Wert von Javascript zu PHP weitergeben Änderung wählen Sie die Option zum Erzeugen

<select name="filter-location" id="location"> 
    <option value=""> 
     <?php if ($input_titles == 'placeholders') : ?> 
     <?php echo __('Location', 'realia'); ?> 
     <?php else: ?> 
     <?php echo __('All locations', 'realia'); ?> 
     <?php endif; ?> 
    </option> 
    <?php 
     $locations = get_terms('locations', array(
      'hide_empty' => false, 
      'parent'  => 0 
     )); 
    ?> 
    <?php if (is_array($locations)) : ?> 
    <?php foreach ($locations as $location) : ?> 
    <option value=" 
     <?php echo esc_attr($location->term_id); ?>" 
     <?php if (! empty($_GET['filter-location']) && $_GET['filter-location'] == $location->term_id) : ?>selected="selected" 
     <?php endif; ?>> 
     <?php echo esc_html($location->name); ?> 
    </option> 
    <?php endforeach; ?> 
    <?php endif; ?> 
</select> 

Locations

Und dies ist der Code, die Bezirke erzeugt:

<?php $sublocations = get_terms('locations', array(
           'hide_empty' => false, 
           'parent' => $location->term_id, 
           )); 
?> 

Districts

Wie Sie sehen können, zu Bezirke generieren, benötigt es $location->term_id.

Wie kann ich $location->term_id senden, wenn jemand einen Standort auswählt?

Antwort

0

Bei Änderung der location Auswahl wird die Funktion ajaxDistrictsRequest() aufgerufen, wobei der ausgewählte Wert als Parameter übergeben wird.

<select name="filter-location" id="location" onchange="ajaxDistrictsRequest(this.value)"> 
    <option value=""> 
<?php 
    if ($input_titles == 'placeholders') : 
    echo __('Location', 'realia'); 
    else: 
    echo __('All locations', 'realia'); 
    endif; 
?> 
    </option> 
<?php 
    $locations = get_terms('locations', array(
     'hide_empty' => false, 
     'parent'  => 0 
)); 
    if (is_array($locations)) : 
    foreach ($locations as $location) : 
     $selected = ((! empty($_GET['filter-location']) && $_GET['filter-location'] == $location->term_id) ? "selected" : ""; 
?> 
    <option value="<?php echo esc_attr($location->term_id); ?>" 
    <?php echo $selected ?>><?php echo esc_html($location->name); ?></option> 
<?php 
    endforeach; 
    endif; 
?> 
</select> 
<select name="filter-sub-location" id="district" disabled> 
    <option value=""> 
</select> 

Der folgende Code verwendet jQuery eine AJAX Anforderung an districts.php mit dem vorgesehenen Wert als term_id zu senden.

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"> 
<script type="text/javascript"> 
    function ajaxDistrictsRequest(value) { 
    if (value) { 
     var districtSelect = document.getElementById('district'); 
     // disable the #district list until it is populated by the response 
     districtSelect.disabled = true; 
     // make the ajax call to districts.php 
     $.ajax({ 
     url: 'districts.php', 
     type: 'POST', 
     data: { 
      term_id : value 
     }, 
     success: function (response) { 
      // populate the #districts option list with the result 
      districtSelect.innerHTML = response; 
      // enable the #district control 
      districtSelect.disabled = false; 
     } 
     }); 
    } 
    }); 
</script> 

Der Vollständigkeit halber hier ist ein Beispiel districts.php-Datei, die die Optionen für die #district Dropdown-Liste erzeugt:

<?php 
    if (isset($_POST['term_id'])) : 
    $term_id = $_POST['term_id']; 
    // do database query 
    $sql = 'SELECT id, name FROM some_table WHERE term_id = :value'; 
    $stmt = $dbh->prepare($sql); 
    $stmt->execute(array(':value' => $term_id)); 
    $results = $stmt->fetchAll(PDO::FETCH_ASSOC); 
    foreach ($results as $row) : 
     echo "<option value='" . $row['id'] . "'>" . $row['name'] . "</option>"; 
    endforeach; 
    exit; 
    endif; 
?> 
+0

Dank für die Antwort Kumpel, es funktioniert –

Verwandte Themen