为AJAX PHP MySQL生成的表创建动态Div标记
上面是PHP,下面是HTML,我现在正在开发mysql的示例世界数据库为AJAX PHP MySQL生成的表创建动态Div标记,php,jquery,mysql,css,ajax,Php,Jquery,Mysql,Css,Ajax,上面是PHP,下面是HTML,我现在正在开发mysql的示例世界数据库 <?php $q=$_GET["q"]; $con = mysql_connect('localhost', 'root', ''); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("world", $con); $sql="SELECT * FROM co
<?php
$q=$_GET["q"];
$con = mysql_connect('localhost', 'root', '');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("world", $con);
$sql="SELECT * FROM country WHERE Code = '".$q."'";
$result = mysql_query($sql);
echo "<table border='1'>
<tr>
<th>Code</th>
<th>Name</th>
<th>Continent</th>
<th>GNP</th>
<th>GNPOld</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['Code'] . "</td>";
echo "<td>" . $row['Name'] . "</td>";
echo "<td>" . $row['Continent'] . "</td>";
echo "<td>" . $row['GNP'] . "</td>";
echo "<td>" . $row['GNPOld'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
它从表单和显示表中获取相同的名称。
我的另一套代码是:-
<html>
<head>
<script type="text/javascript">
function showUser(str)
{
if (str=="")
{
document.getElementById("txtHint").innerHTML="";
return;
}
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("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","moviedetail.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>
<table width="100%" border="0">
<tr>
<td>
<select name="Country" onChange="showUser(this.value)">
<option>Select Name</option>
<?php
mysql_connect('localhost','root','')
or die ("could not connect DB");
mysql_select_db('world')
or die ("could not connect database");
$query="select code, name from country order by name asc"
or die ("query failed");
$result=mysql_query($query);
while(list($code, $name)=mysql_fetch_row($result)) {
echo "<option value=\"".$code."\">".$name."</option>";
echo "<div id=\"".$code."\">".$name."</div>";
}
?>
</select>
</td>
<td>
<div id="txtHint"><b>Country info will be listed here.</b></div>
</td>
</tr>
</table>
</body>
</html>
html的相关部分是
<?php
for (;$i<$nrows;)
{
#add 1 so that numbers don't start with 0
echo"<tr>\n";
for ($j=0;$j<10&&$i<=$nrows;$j++)
{
$n = $i;
$i=$i + 1;
$k=$n%30;
$row = mysqli_fetch_assoc($result);
extract($row);
echo "<td>
<table>
<tr>
<td>$n</td>\n
</tr>\n
<tr>
<td>$Name</td>\n
</tr>\n
<tr>\n
<td>$Continent</td>\n
</tr>\n
<tr>\n
<td>$Region</td>\n
</tr>\n
<tr>\n
<td>$SurfaceArea</td>\n
</tr>\n
<tr>\n
<td>$IndepYear</td>\n
</tr>\n
<tr>\n
<td>$GNP</td>\n
</tr>\n
<tr>\n
<td>$k</td>\n
</tr>\n
</table>\n
</td>";
if ($k==0)break 2;
}
echo"</tr>\n";
}
?>
我在设计上没有什么困难。我希望前两组代码中有一个鼠标悬停效果,显示每个国家/地区的更多详细信息,最后两组代码在上面的代码中为空的列中生成。我希望它始终显示在相同的位置,尽管页面移动
我还有一个简单的问题。正如你们在上面看到的,我已经停止了查询30个结果。我想在底部添加“显示更多”按钮,以便在同一页面上显示更多结果
我是一个新手,所以如果你指出代码中的错误会非常有帮助。到目前为止,它在localhost上工作得很好。我建议您使用ez sql使查询数据库更容易: 还有jquery: 下面是一个教程,演示如何在jquery中执行ajax调用: 从您的代码中,我可以看到您正在尝试使用$\u GET变量查询数据库。我假设您的搜索字段的名称是'q'。并使用javascript动态显示结果 HTML:
您是否有可能将jQuery库集成到您的系统中?这将使我们更容易为您提供执行AJAX查询和修改DOM的示例代码。我对任何协议或语言都没有任何特别的亲和力,因为我对任何东西都是希腊语。我只关心最终结果。我等待你的建议和代码
<td><table border="1">
<tr>
<td>
<?php
include ("/connections/query.php");
$nrows = mysqli_num_rows($result);
/* Display results in a table */
echo "<table>\n
<tr>\n";
$i=1;
include ("/function/movietable.php");
echo "</tr>\n
</table>\n";
?>
</td>
<td> </td>
</tr>
</table></td>
<input type="text" id="q" name="q"/>
<div id="your_div"></div><!--this is where your html table will be loaded dynamically as you type a value on the textbox-->
<script src="jquery.js"></script>
<script>
$(function(){
$('#q').keyup(function(){
var query = $.trim($(this).val());
$('#your_div').load('phpfile.php', {'q' : query});
});
});
</script>
//database configuration here
$q = mysql_real_escape_string($_POST['q']);
//html table here