需要帮助 - 用DB中的数据替换DIV内容而不刷新(使用 - jquery,hmlhttp,CI) [英] Need help - Replacing DIV content with data from DB without refresh (using - jquery, hmlhttp, CI)
问题描述
这里我再去。我试图改变一个部门的内容与从我的数据库检索的数据。这必须做,而不重新加载页面,因此我使用xmlhttp和javascript。该网站正在运行CodeIgniter。下面是它的简短摘要的代码。我一直关注 this 教程,但由于某些原因,当我点击按钮,没有发生任何事情。
Here I go again. I am trying to change content of a division with data retrieved from my database. This has to be done without reloading the page, therefore I am using xmlhttp and javascript. The website is running on CodeIgniter. Below is the code with a short summary of it. I have been following this tutorial, but for some reason, when I click the button, nothing is happening.
strong>查看 * xampInstallFolder / htdocs / application / obs / views / test_v.php *
View *xampInstallFolder/htdocs/application/obs/views/test_v.php*
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$( "#next_btn" ).click(function() {
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("listA").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","getnext5.php?q="true);
xmlhttp.send();
})
</script>
</head>
<body>
<div id="listA">
</div>
<div id="next_btn">
</div>
</body>
</html>
现在当我的按钮被点击时触发该函数。该函数在视图的头部。我不知道应该存储与查询文件( getnext5.php
)。现在它在 views
文件夹中。文件的链接是这个 xmlhttp.open(GET,getnext5.php?q =true);
应该以某种方式更改这个?
Right now I trigger the function when my button gets clicked. The function is in the head of the view,. I am not sure where should the file with the query (getnext5.php
) be stored. Right now it is inside the views
folder. The link to the file is this xmlhttp.open("GET","getnext5.php?q="true);
should I somehow change this?
功能 xampInstallFolder / htdocs / application / obs / views / getnext5.php
Functions xampInstallFolder/htdocs/application/obs/views/getnext5.php
<?php
$con = mysqli_connect('localhost','root','root','obs2');
if (!$con)
{
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"obs2");
$sql="SELECT * FROM user WHERE id > 5";
$result = mysqli_query($con,$sql);
echo "<table>";
while($row = mysqli_fetch_array($result))
{
echo "<a style="display:block" href="base_url('core/detail/'.$row[id])">";
echo "<div id="suggested" onmouseover="" style="cursor: pointer;">";
echo "<div id="info">";
echo "<div id="info_center">";
echo "<p>" . $row['name'] . "</p>";
echo "</div>";
echo "</div>";
echo "<div class="mosaic-block fade">";
echo "<a href="base_url('core/detail/'.$row[id])" class="mosaic-overlay">";
echo '<div class="details">';
echo "<p>" . $row['summary'] . "</p>";
echo "</div>";
echo "</a>";
echo "<div class="mosaic-backdrop"><img src="www.puu.sh/5DSWj.jpg">";
echo "</div>";
echo "</div>";
echo "<div id="info">";
echo "<div id="info_center">";
echo "<p>" . "Rating: 7.7" . "</p>";
echo "</div>";
echo "</div>";
echo "</div>";
echo "</a>";
}
echo "</table>";
?>
在此文件中,我建立与我的数据库的连接,运行查询,调用 $ pages
,然后使用while循环为结果设置样式。该样式被回显,因为它然后由xmlhttp请求返回,并显示在 listA
部分中,替换原始内容。我不确定回音是否写入正确。
In this file I establish the connection to my DB, run the query, store the rows in an array called $pages
and then style the results using a while loop. The style is echoed, because it is then returned by the xmlhttp request and displayed in the listA
division, replacing the original content. I am not sure if the echoes are written right or not.
我对任何建议持开放态度。一直试图解决这一整天没有任何成功。
I am open to any suggestions. Have been trying to resolve this the whole day without any success. Thank you all for reading and most importantly for your replies.
推荐答案
您没有正确地连接get参数。
You're not concatenating the get parameter properly.
xmlhttp.open("GET","getnext5.php?q="true);
q =true
true是一个布尔值!
q="true
<-- true is a boolean!
尝试这样打开:
xmlhttp.open("GET","getnext5.php?q=true");
想要连接正确,请执行q =+ variable
If you do want to concatenate proper, do "q="+variable
这篇关于需要帮助 - 用DB中的数据替换DIV内容而不刷新(使用 - jquery,hmlhttp,CI)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!