0

这是我的第一页。我的问题是,当我选择一个选项时,页面保持空白。我知道问题出在我的 javascript 中,但不确定我哪里出错了?在此先感谢!(此页面上的数据库调用填充下拉列表):

<?php 

if (isset($_GET['t'])) {
 $t=$_GET ['t'];


mysql_select_db($database_db_tutorials2, $db_tutorials2);
$query_Recordset1 = "SELECT * FROM UC GROUP BY $t";
$Recordset1 = mysql_query($query_Recordset1, $db_tutorials2) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
 <head>
 <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" ></script>
<script type="text/javascript">

function showUser() {
  var Tsearch = document.getElementById("Tsearch"),
  Stype = document.getElementById("Stype");
  var str = "";
if (Tsearch.value != "") {
str += "Tsearch=" + encodeURIComponent(Tsearch.value);
}
if (Stype.value != "") {
str += (str.length == 0? "" : "&") + "Stype=" + encodeURIComponent(Stype.value);
}

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","process_as.php?" + string, true);
xmlhttp.send();
alert(xmlhttp.responseText);
} 
</script>
</head>
<body>


<form>
<select name="Tsearch" onchange="showUser(this.value,'Tsearch');">
<option selected="selected" value="">Choose</option>
<?php
          do {  
             ?>
<option value="<?php echo $row_Recordset1 [$t] ?>"> <?php echo $row_Recordset1 [$t];?></option>
<?php
    } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1));
      $rows = mysql_num_rows($Recordset1);
      if($rows > 0) {
          mysql_data_seek($Recordset1, 0);
          $row_Recordset1 = mysql_fetch_assoc($Recordset1);
      }
    }
 ?>
</select>
<input name="Stype" type="hidden" value="<?php echo $t ?> "/>
</form>
<br />
<div id="txtHint"><b>Info will be listed here:</b></div>
<hr />
</body>
</html>

这是 process_as.php 页面:

<?php $path = $_SERVER['DOCUMENT_ROOT'];
 include($path . '/connections/db_tutorials2.php'); 

 $con = mysql_connect($hostname_db_tutorials2, $username_db_tutorials2,         $password_db_tutorials2);
 if (!$con)
 {
 die('Could not connect: ' . mysql_error());
 }

mysql_select_db($database_db_tutorials2, $con);
$Tsearch=$_GET ["Tsearch"];
$stype=$_GET ["stype"];


echo "this is search:".$Tsearch.'</br>';
echo "this is stype:".$stype.'</br>';

$sql= sprintf("SELECT * FROM UC WHERE UC.%s = '%s'", mysql_real_escape_string($Tsearch),mysql_real_escape_string($stype)); 

$result = mysql_query($sql) or die($sql."<br/>oopsa<br/>".mysql_error());
echo "<table border='1'>
<tr>
<th>Category</th>
<th>Tutorial name</th>
<th>Fmt</th>
<th>Website</th>
</tr>";

while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['Category'] . "</td>";
echo "<td>" . $row['Tutorial_Name'] . "</td>";
echo "<td>" . $row['Fmat'] . "</td>";
echo "<td>" . $row['Website'] . "</td>";
echo "</tr>";
}
echo "</table>";
 ?>
4

1 回答 1

0

我可以看到您已链接到 JQuery 框架。看看jquery如何处理 ajax

尝试这个

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js" ></script>
<script type="text/javascript">
    $(document).ready(function () {  
      $(document).on('change', '#tsearch', function() {
              $.get("process_as.php", { 'Tsearch':$('#tsearch').val(),'Stype':$('#Stype').val()},
          function(data){
             $('#txtHint').html(data);
          }).error(function() { alert('error'); });;
      });
    });
    </script>
于 2012-10-12T19:00:09.960 回答