使用javascript和php创建onChange选择下拉菜单,但它';It’不起作用


Creating an onChange select drop down with javascript and php but it's not working

我要做的是创建一个包含苏格兰32个不同位置的下拉容器,当选择其中一个位置时,例如格拉斯哥,它应该转到一个URL,该URL显示div WHERE location=Glasgow中每篇文章的标题、文本等内容。

我没有错误消息,也没有任何形式的识别我的代码已经工作了,因为当我在下拉菜单上选择四个代码中的一个时,它绝对不会起任何作用。

能来收拾一下并纠正我迄今为止所做的一切吗?我会非常棒的!

这是我正在使用的文件:

header.php

<html>
<head>
<script src="http://code.jquery.com/jquery.min.js"></script>
<script type="text/javascript">
    $(document).ready(function(){
        $('#location').change(function(){
            //Retrieve Content from the back-end PHP page, and pass the ID selected
            var url = 'location.php?location=' + $(this).val();
            $('#txtHint').load(url);
        });
    });
</script>
</head>
<body>
     <div id="header">
        <div class="headerLeftContent">
            <select id='location'>
                    <option href="Link to a dynamic page with all the content from glasgow" value="Glasgow">Glasgow</option>
                    <option href="Link to a dynamic page with all the content from x" value="x">x</option>
                    <option href="Link to a dynamic page with all the content from test" value="test">test</option>
                    <option href="Link to a dynamic page with all the content from edinburgh" value="Edinburgh">Edinburgh</option>
            </select>
            <div id='txtHint'></div>
        </div>          
    </div>
</body>
</html>

location.php

<?php
$connect = mysql_connect('xxxxxx', 'xxxxxx', 'xxxxxx');
$select_db = mysql_select_db('xxxxxx');
$location = $_REQUEST['location'];
$query = "SELECT * FROM podContent WHERE location = '.$location.'";
while($row = mysql_fetch_array($query))
{
echo $row['text'];
}
mysql_close($connect);
?>

请注意,任何关于"SQL注入"或"mysql"应该如何成为"PDO"的评论都是不需要的,因为我确实理解这一点,但我目前只是在测试,并将对此进行修改。

谢谢。

您在MySQL查询中连接位置名称时似乎有一个错误,而且它与任何内容都不匹配(因此没有返回echo)。更改此项:

$query = "SELECT * FROM podContent WHERE location = '.$location.'";

$query = "SELECT * FROM podContent WHERE location = '$location'";

(除非你的数据库中有.Glasgow.这样的东西…)

然后,您必须按照Alon的建议致电mysql_query($query)

您需要使用mysql_query并将资源传递给mysql_fetch_array函数:

$query = "SELECT * FROM podContent WHERE location = '.$location.'";
$result = mysql_query($query);
while($row = mysql_fetch_array($result))
 {
    echo $row['text'];
 }
相关文章: