2

这就是我所拥有的。我正在尝试让 jquery 运行 mysql 查询。

这是我的PHP:

<select name="kingdom" id="kingdom" >
    <option value="standard">-- Kingdom --</option>
        <?php
            $the_d = $_POST['d'];
            $filter = mysql_query("SELECT DISTINCT sci_kingdom FROM tbl_lifedata WHERE sci_domain = '$the_d'");
                while($row = mysql_fetch_array($filter, MYSQL_NUM))
                    {
                        $row['name'];
                        //echo "<option value='$row[0]'>$row[0]</option>";
                    }
        ?>
</select>

还有我的 jQuery:

$('#domain').change(function () {

    var selectval = $('#domain').val();

    $.post("search.php", {
        d: selectval
    }, function (data) {
        $('.result').html(data);
    });
});

现在我只想让 jQuery 吐出 mysql 结果的值。当我有这个工作时,我可以让他们填充一个选择框。我现在得到的是 search.php 的 html,但与 mysql 查询无关。

4

1 回答 1

2

您实际上应该打印从数据库中检索的内容。

<select name="kingdom" id="kingdom" >
                <option value="standard">-- Kingdom --</option>
                <?php 
                $the_d = mysql_real_escape_string($_POST['d']);//thanks to @Mansfield
                $filter = mysql_query("SELECT DISTINCT sci_kingdom FROM tbl_lifedata WHERE sci_domain = '".$the_d."'");
                while($row = mysql_fetch_array($filter, MYSQL_NUM))
                {
                    echo "<option value='$row[0]'>$row[0]</option>";

                }

                ?>
            </select>


 //or you can also use mysql_fetch_array($filter, MYSQL_ASSOC) if you want to print $row['name']

更新的答案:

<script src="jquery.min.js"></script>
<input type='text' class="domain" id="domain"/>
<div class="result" id="result"></div>
<script>
$('#domain').change(function() {

            var selectval = $('#domain').val();

            $.post(
                "test.php", 
                { d : selectval },
                function(data) {
                    $('.result').html(data);//my bad should be displaying data retrenched
                }
                );
});
</script>

///test.php contents:

<?php
print $the_d = mysql_real_escape_string($_POST['d']);// see if the scripts are working fine until here, if so, then see below.
//test your mysql result by doing a print_r($row.)    
?>

发布 print_r($row) 的结果;

于 2012-06-21T01:36:36.700 回答