【问题标题】:Cannot get my javascript/php onchange function to work无法让我的 javascript/php onchange 函数工作
【发布时间】:2013-02-15 23:00:32
【问题描述】:

我已经在 2 小时前就这个问题发表了一篇文章,但我真的需要尽快完成。

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 中的每篇文章。

目前我没有每个位置的 URL。

当我选择一个新选项时出现以下消息:“加载结果:成功 ||| 200 OK”

谁能帮我解决这个令人沮丧的难题?

这是我正在使用的文件:

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, function (response, status, xhr) { alert("Load result: " + status + " ||| " + xhr.status + " " + xhr.statusText); 
    });
  });
});    
</script>

</head>
<body>
    <div id="header">
        <div class="headerLeftContent">
            <select id="location">
                    <option value="Glasgow">Glasgow</option>
                    <option value="x">x</option>
                    <option value="test">test</option>
                    <option value="Edinburgh">Edinburgh</option>
            </select>
            <div id='txtHint'></div>
        </div>          
    </div>

</body>
</html>

location.php

<?php
$connect = mysql_connect('xxx', 'xxx', 'xxx');
$select_db = mysql_select_db('xxx');

$location = $_REQUEST['location'];

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

$result = mysql_query( $query, $connect );

while($row = mysql_fetch_array($result))
{
echo $row['text'];
}

?>

谢谢。

【问题讨论】:

标签: php javascript onchange


【解决方案1】:

纠正这个

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

【讨论】:

    【解决方案2】:

    两件事:

    1. $query = "SELECT * FROM podContent WHERE location = '.$location.'"; 有两个额外的点。应该是$query = "SELECT * FROM podContent WHERE location = '$location'";
    2. 在使用来自用户的值$location 之前,您应该使用mysql_real_escape_string() 对其进行转义。请注意,此函数已弃用,并将在 PHP 的未来版本中删除。您应该查看PDO 和准备好的语句。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-10
      • 1970-01-01
      相关资源
      最近更新 更多