[英]AJAX drop-down-select site displaying information outside of table
下拉选择将填充正确的数据,但输出始终显示在表外部。 我的代码有什么问题? 有什么想法/建议吗? 提前致谢,
select.php
<?php
$q = $_GET['q'];
$con = mysqli_connect('host','username','password','database');
if (!$con)
{
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"ajax_demo");
$sql="SELECT * FROM timeSheet, timeSheetUsers WHERE timeSheet.userName=timeSheetUsers.userName AND timeSheet.userName= '".$q."' ORDER BY startTime ASC";
$result = mysqli_query($con,$sql);
echo "<table border ='1'>
<tr>
<th bgcolor='#F0E68C'>username</th>
<th bgcolor='#E0FFFF'>date</th>
<th bgcolor='#F0E68C'>starTime</th>
<th bgcolor='#E0FFFF'>endTime</th>
<th bgcolor='#F0E68C'>total</th>
</tr>";
echo "<br />\n";
echo "<br />\n";
echo "<br />\n";
echo "<br />";
echo "<br />";
echo "<br />";
//
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<tdbgcolor='#F0E68C'>" . $row['userName'] . "</td>";
echo "<tdbgcolor='#F0E68C'>" . $row['date'] . "</td>";
echo "<tdbgcolor='#F0E68C'>" . $row['startTime'] . "</td>";
echo "<tdbgcolor='#F0E68C'>" . $row['endTime'] . "</td>";
echo "<tdbgcolor='#F0E68C'>" . $row['total'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
home.php
<html>
<head>
<script type="text/javascript"
src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script>
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)
{
var txtHint = document.getElementById("txtHint");
$("#txtHint").css("display", "none");
$("#txtHint").fadeIn('slow');
txtHint.innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","select.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>
<?php
require "credentials.php";
try {
$database = "database";
$conn = new PDO("mysql:host=$hostname;dbname=$database", $username, $password);
echo "<p>Database Connection OK.<p>";
}
catch (PDOException $e) {
$conn->setArribute(PDO::ATT_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Could not open database.";
}
echo "<form>";
echo "<select name='selStudentWorker' onChange='showUser(this.value)'>";
$sql = $conn->prepare("SELECT firstName, lastName, userName FROM timeSheetUsers ORDER BY firstName ASC");
if ($sql->execute(array(
$_SESSION['userName']
))) {
while ($row = $sql->fetch()) {
echo "<option value='".$row['userName']."' "."selected='selected'>" . $row['firstName'] . " " . $row['lastName'] . " (" . $row['userName'] . ")";
echo "</option>";
}
}
echo "</select>";
echo "</form>";
?>
</body>
</html>
参考:基于此的代码-> http://www.w3schools.com/ajax/ajax_database.asp
您的td标签中有一个错误:
<tdbgcolor='#F0E68C'>
应该:
<td bgcolor='#F0E68C'>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.