AJAX / PHP:从mysql数据库传递/检索两个变量

时间:2012-10-12 18:45:54

标签: php mysql ajax

这是我的第一页。我的问题是,当我选择一个选择时,页面仍为空白。我知道问题出在我的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>";
 ?>

1 个答案:

答案 0 :(得分: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>