【发布时间】: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,因为我确实理解这一点,但我目前只是在测试,并将对其进行修改。
谢谢。
【问题讨论】:
-
为什么你的
<option>元素有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