【发布时间】:2014-07-17 05:14:29
【问题描述】:
我想做的是在我的索引页面中显示我的 count.php 的结果。我的计划是每次访问或查看页面时自动计算 user_code 的行数并在索引页面中显示结果。
我的问题是 ajax 脚本没有收到 count.php 的结果来显示在 index 页面的 count 输入框中。
索引页面:
<input type="text" value="1" name="countValue" id="countValue" style="width: 12px;" /><br />
Count: <input type="text" name="count" id="count" readonly="readonly" /><br /><br /><br />
<script>
$(document).ready(function(){
var countTimer = setInterval(
function ()
{
codeValue();
}, 500);
var $count = $('#count');
function codeValue(){
$.ajax({
url:"count.php",
type:"GET",
data: { term : $('#countValue').val() },
dataType:"JSON",
success: function(result) {
$("#count").val(result.user_code);
}
});
};
});
</script>
count.php
<?php
error_reporting(-1);
ini_set('display_errors', 'On');
$host = "localhost";
$user = "root";
$pass = "";
$db = "test";
$dbc = new PDO("mysql:host=" . $host . ";dbname=" . $db, $user, $pass);
$dbc->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo $_GET['term'];
if (isset($_GET['term'])) {
$q = $_GET['term'];
$sql = "SELECT user_code FROM students";
$query = $dbc->prepare($sql);
$query->execute();
$num_rows = $query->rowCount();
echo json_encode(array('user_code'=>$num_rows));
}
?>
【问题讨论】:
-
你得到什么错误信息?
-
请在控制台检查错误
-
您的 count.php 页面的响应是什么??
-
也去掉这一行var $count = $('#count');
-
@sharif 除了未被使用外,不会导致 OP 的问题