如何将选择值从Javascript传递给PHP以生成更改时的选择选项

时间:2017-02-18 12:57:13

标签: javascript php ajax wordpress

这是我生成位置的代码:

<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

这是生成区域的代码:

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

Districts

如您所见,要生成区域,需要$location->term_id

每当有人选择某个位置时,如何发送$location->term_id

1 个答案:

答案 0 :(得分:0)

在更改location选择时,将调用ajaxDistrictsRequest()函数,并将所选值作为参数传递。

<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>

以下代码使用jQuery向districts.php发送AJAX请求,其中提供的值为term_id

<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>

为了完整性,这里有一个示例districts.php文件,它为#district下拉列表生成选项:

<?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;
?>