weixin_33712881 2017-06-25 14:35 采纳率: 0%
浏览 31


OK so I have been for 2 days trying to do this but for some reason, doesn't matter in what way I do it, it doesn't work (I tried different way with json_encode and then taking care of creating the elements with javascript, I've tried to echo the in the php itself, I've tried to return it, nothing really works)

What I'm trying to do is use AJAX to ask the server for a specific script (right now test.php) and then return the <option>'s so I can populate a <select>, so I'm trying to populate a comboBox, what seems to be happening is that the AJAX response is coming out empty, I tried to output the result and it was outputting an empty value. Can anyone please help me, I'm really desperate at this point, it should have been easy, I've spent hours on stack overflow trying to find the problem, I've debugged the code to the best of my ability, I've read everything relevant in the jquery documentation. well anyways my setting my suffering aside, here's the code:

comboStaging.php :

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<form id="testForm">
    <select name="states" id="states">

        $odb = new PDO('mysql:host=localhost;dbname=database', 'root', '');
        $query = "SELECT id, name FROM `database`.pt_state";
        $data = $odb->prepare($query);

        while ($row = $data->fetch(PDO::FETCH_ASSOC)) {
            echo '<option value="' . $row['id'] . '">' . $row['name'] . '</option>';
    <select name="cities" id="cities">


<script type="text/javascript">
    $(document).ready(function () {

        function loadFirst() {
                type: "GET",
                url: "test.php",
                cache: false,
                data: "id=1",
                dataType: "html",
                success: function (data) {



    $("#states").change(function () {
        var ID = document.getElementById("states").valueOf().value;

            type: "GET",
            url: "test.php",
            cache: false,
            data: "id=" + ID,
            success: function (r) {
                document.getElementById("cities").innerHTML = r;


header('Content-Type: text/html');
require_once $_SERVER['DOCUMENT_ROOT'] . "/resources/scripts/php/conn/dal.php";

$stmt = $DB_con->prepare('SELECT id, name FROM `database`.pt_city WHERE state_id=:state');
$stmt->bindParam(':state', $_GET['ID']);

if($stmt->rowCount() > 0 ) {
    while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
        $test = $test . '<option value="' . $row['id'] . '"id="optionTest">' . $row['name'] . '</option>';
return $test;

Dev console: dev console

  • 写回答

0条回答 默认 最新



      • ¥15 如何通过求后验分布求得样本中属于两种物种其中一种的概率?
      • ¥15 q从常量变成sin函数,怎么改写python代码?
      • ¥15 图论编程问题,有可以指导的吗
      • ¥15 DEA的CCR模型画图
      • ¥15 请假各位一个关于安卓车机的问题
      • ¥15 光谱仪怎么看这样的透射谱
      • ¥15 pyqt5 如何实现输入框输入关键词,下拉框显示模糊查询返回的结果?
      • ¥20 fluent模拟,可以燃烧和相变同时模拟吗?
      • ¥50 海康摄像头,C#如何识别车牌号码和抓取JPG
      • ¥15 yolov5 pt转engine的问题