【问题标题】:Creating an onChange select drop down with javascript and php but it's not working使用 javascript 和 php 创建一个 onChange 选择下拉菜单,但它不起作用
【发布时间】:2013-01-31 23:11:40
【问题描述】:

What I am trying to do is create a drop down container of 32 different locations in Scotland and when one of the selections is selected, for example, Glasgow it should go to a URL which displays content such as heading, text, for 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”的 cmets,因为我确实理解这一点,但我目前只是在测试,并将对其进行修改。

谢谢。

【问题讨论】:

  • 为什么你的&lt;option&gt; 元素有href 属性?
  • 这只是为了帮助人们了解我想要做什么,我希望它链接到内容具有位置 = 格拉斯哥或爱丁堡等的 URL。
  • 啊,好吧。查看此处的 Javascript:jsfiddle.net/r5SWJ 并尝试使用 load 的形式。注意它是如何使用load(回调)的第二个参数来显示AJAX的结果
  • @Ian 所以我把它粘贴到我的代码中,当我将此代码上传到我的服务器时,它在 jsfiddle 平台上显示成功,我收到一条警告说错误。
  • 这意味着你有一个通信/服务器问题。现在,试试这个以获取更多信息:$('#txtHint').load(url, function (response, status, xhr) { alert("Load result: " + status + " ||| " + xhr.status + " " + xhr.statusText); });

标签: php javascript sql onchange


【解决方案1】:

您在 MySQL 查询中连接您的位置名称似乎有一个错误,并且它不匹配任何内容(所以没有任何内容是 echoed 回复)。改变这个:

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

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

(除非您的数据库中有 .Glasgow. 之类的东西...)

那么您必须按照 Alon 的建议致电 mysql_query($query)

【讨论】:

  • 你好有同样的问题像@AyeTry 但它没有帮助我:(
【解决方案2】:

你需要使用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'];
 }

【讨论】:

  • 是的,没关系,我在发布此消息后立即意识到这一点。
猜你喜欢
  • 2016-08-30
  • 1970-01-01
  • 2012-08-18
  • 2015-02-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多