我在jquery脚本中使用ajax从db获取值时遇到了一些问题。
的index.php:
<?php include 'db/db_connection.php';?>
<?php include 'db/db_getvalues.php';?>
<div class="arrow">
<img src="img/arrow_left.png" />
</div>
<div class="text-box"></div>
的script.js
$(document).ready(function () {
$(".arrow").click(function () {
$.ajax({
type: "GET",
url: "../db/db_getvalues.php", // This is the correct path, but do jquery recognize "../db/" etc?
dataType: "text",
success: function (response) { // Here is where i'm lost, what do i need to write to update my innerHTML with the returned value from the db_getvalues.php-file?
$(".text-box").html(response);
}
});
});
});
db_getvalues.php //这个文件有效,我直接从html文件中选择了数据
<?php
function getdbvalues() {
$query = 'SELECT * FROM mydb WHERE Id = 1';
$fetch = mysql_query($query) or die ('Could not find tablerow');
$row = mysql_fetch_assoc($fetch);
$textString = $row['Text'];
return $textString;
}
?>
答案 0 :(得分:1)
是的,AJAX识别此类网址。您的ajax
似乎没问题,但有一点建议可以在db_getvalues.php
进行更改。你应该回应价值而不是回报。我建议你拨打getdbvalues()
功能&amp;如下所示回应它。
在db_getvalues.php中
function getdbvalues() {
$query = 'SELECT * FROM mydb WHERE Id = 1';
$fetch = mysql_query($query) or die ('Could not find tablerow');
$row = mysql_fetch_assoc($fetch);
$textString = $row['Text'];
return $textString;
}
echo getdbvalues(); //Added
答案 1 :(得分:0)
你必须确定 1.你的点击功能正常工作 你的ajax请求是成功的 3. php正在生成一些由ajax成功函数接收的html 4.你的PHP代码给你(回声)所需的html
检查我的代码中添加了注释(1,2,3,4)的行
$(document).ready(function () {
$(".arrow").click(function () {
alert("I am called"); //1
$.ajax({
type: "GET",
url: "../db/db_getvalues.php",
dataType: "text",
success: function (response) {
alert(response+" __ url is fine");//2
$(".text-box").html(response);
}
});
});
});
<?php
echo "php is called"; //3
function getdbvalues() {
$query = 'SELECT * FROM mydb WHERE Id = 1';
$fetch = mysql_query($query) or die ('Could not find tablerow');
$row = mysql_fetch_assoc($fetch);
$textString = $row['Text'];
return $textString;
}
echo getdbvalues(); //4
?>