AJAx 脚本要么未连接到 testData.php,要么无法从 testData.php 中提取数据。无法隔离问题,任何帮助将不胜感激。
我有 2 个文件
index.php - AJAX 脚本所在的位置以及我希望在 getdiv 标记中显示数据的位置。
textData.php - 使用 mysql db 提取数据。这个脚本有效,我在底部复制/粘贴了结果。
我相信问题出在 AJAX 脚本中。如果不能在 div 标签中显示信息,它就不会提取信息。
testData.php - 数据库脚本和结果:
<?php
require_once('dbconv2.php');
$dbcon1 = Connection::getConnection();
try { $stmt = $dbcon1 ->query("SELECT
click_count.page_ur,
click_count.page_count
FROM click_count
");
$testArray = $stmt->fetchAll(PDO::FETCH_ASSOC);
echo $row = $stmt->rowCount();
}
catch(PDOException $e)
{ echo $e->getMessage(); }
var_dump($testArray);
echo json_encode($testArray);
?>
array (size=7)
0 =>
array (size=2)
'page_ur' => string 'test record page1' (length=17)
'page_count' => string '1' (length=1)
1 =>
array (size=2)
'page_ur' => string 'test record page2' (length=17)
'page_count' => string '4' (length=1)
2 =>
array (size=2)
'page_ur' => string 'test record page3' (length=17)
'page_count' => string '3' (length=1)
[{"page_ur":"test record page1","page_count":"1"},{"page_ur":"test record page2","page_count":"4"},{"page_ur":"test record page3","page_count":"3"},{"page_ur":"test record page5","page_count":"7"},{"page_ur":"test record page4","page_count":"9"},{"page_ur":"test record page6","page_count":"6"},{"page_ur":"test record page7","page_count":"2"}]
index.php - AJAX 脚本
<body>
<div id="getdiv"> </div>
<div id="questionButton">
<form method="POST" name="form_questions" action="">
<BUTTON class="btn_get" id ="btn_get" NAME="btn_get">getInfo</BUTTON>
</form>
</div>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(document).ready(function() {
$('#btn_get').on('click', function(e) {
$.ajax({
type: 'GET',
cache: false,
url: 'testData.php',
dataType: 'json',
success:function(info){
$("#getdiv").html(info[0].page_ur);
}
});
e.preventDefault();
});
});
</script>
</body>