【问题标题】:Send value taken from AJAX to another PHP file via AJAX通过 AJAX 将从 AJAX 获取的值发送到另一个 PHP 文件
【发布时间】:2016-08-22 17:30:22
【问题描述】:

我有一个 MySQL 表,其中包含 country codestatepostcodecity 等字段。我想创建一个表单,当用户输入国家代码AU 时,状态字段应填写与国家代码对应的状态。 And when a state is selected, it should only show postcodes which are under the selected state.选择邮政编码后,该值对应的城市应显示在城市字段中。

数据库中的一些行是:

| country | state | postcode | suburb    |
+---------+-------+----------+-----------+
| AU      | VIC   | 3000     | Melbourne |
| AU      | NSW   | 3006     | Sydney    |

所以现在我有了这个将数据发送到另一个 PHP 文件的 AJAX 代码:

function searchq5(){
    var searchTxt = $("input[name='ccd']").val();
    $.post("search-region.php", {searchVal: searchTxt}, function(state){
        $("#state").html(state);
        searchq6();
    });
}

function searchq6(){
    var searchsub = $("input[name='region']").val();
    $.post("search-suburb.php", {searchVal: searchsub}, function(sbb){
        $("#sbb").html(sbb);
        searchq7();
    });
}

search-region.php:

if (isset($_POST['searchVal'])){
    $searchq = $_POST['searchVal'];
    $query = mysqli_query($link, "SELECT DISTINCT state FROM `wp_locations` WHERE `country` LIKE '%".$searchq."%' ")or die("Could not search!");

    $count = mysqli_num_rows($query);
    if($count == 0){
        $output = '<option>No results!</option>';
    }else{
        while($row = mysqli_fetch_array($query)){
            $state = $row['state'];
            ?>
            <option value="<?php echo $state; ?>"><?php echo $state; ?></option>
            <?php
        } // while
    } // else
}

search-suburb.php:

if (isset($_POST['searchsub'])){
    $searchq = $_POST['searchsub'];
    $query = mysqli_query($link, "SELECT DISTINCT title FROM `wp_locations` WHERE `state`='$searchq' ")or die("Could not search!");

    $count = mysqli_num_rows($query);
    if($count == 0){
        $output = '<option>No results!</option>';
    }else{
        while($row = mysqli_fetch_array($query)){
            $suburb = $row['title'];
            ?>
            <option value="<?php echo $suburb; ?>"><?php echo $suburb; ?></option>
            <?php
        } // while
    } // else
} // main if

My thinking here is, when a state is chosen, the value of that input box is sent to select-suburb.php as the search value.没有任何东西可以作为郊区的选择。我没有从其他 PHP 文件中得到结果。有人可以告诉我解决方案吗?

【问题讨论】:

  • “不起作用”不是很有帮助。请告诉我们是否显示 PHP 错误或控制台错误
  • @mplungjan,编辑了问题。它只是搜索所有郊区,而不是所选州的郊区
  • 这看起来很奇怪:WHERE `state`='$searchq' - 我希望WHERE state="'.$searchq.'"
  • 改成你说的,还是没有@mplungjan
  • 实际上的问题是没有任何选项显示。我没有从其他 php 得到结果

标签: javascript php html mysql ajax


【解决方案1】:

您是否尝试过使用“async:false”选项?由于 ajax 请求是异步的,第二个方法将在请求完成之前被调用。

$.ajaxSetup({async: false});

做你的要求,然后放:

$.ajaxSetup({ async: true });

【讨论】:

  • 函数在回调中被调用
猜你喜欢
  • 1970-01-01
  • 2019-07-23
  • 1970-01-01
  • 1970-01-01
  • 2013-03-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-24
相关资源
最近更新 更多