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

AJAX和PDO问题

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 :

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

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

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

    </select>


</form>
<script type="text/javascript">
    $(document).ready(function () {
        console.warn("test");

        function loadFirst() {
            $.ajax({
                type: "GET",
                url: "test.php",
                cache: false,
                data: "id=1",
                dataType: "html",
                success: function (data) {
                    console.warn("test2")
                    console.warn(data);
                }
            });

        }

        console.warn("test3");
        loadFirst();
    });

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

        $.ajax({
            type: "GET",
            url: "test.php",
            cache: false,
            data: "id=" + ID,
            success: function (r) {
                document.getElementById("cities").innerHTML = r;
            }
        });
    });
</script>
</body>
</html>

Test.php:

<?php
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']);
$stmt->execute();

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 链接问题 C++LNK2001 无法解析的外部符号
    • ¥50 安装pyaudiokits失败
    • ¥15 计组这些题应该咋做呀
    • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
    • ¥15 让node服务器有自动加载文件的功能
    • ¥15 jmeter脚本回放有的是对的有的是错的
    • ¥15 r语言蛋白组学相关问题
    • ¥15 Python时间序列如何拟合疏系数模型
    • ¥15 求学软件的前人们指明方向🥺
    • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接