duanchuiwen6694 2014-03-26 02:24
浏览 40

PHP AJAX MySQL查询问题

I am trying to query my MySQL DB with AJAX. The html table gets built but the MySQL data is not displaying. I am not sure what I am missing. I have 2 files. So far this is what I have:

ajax1.html

<html>
<head>
<script>
function showUser(str)
{
if (str=="")
{
document.getElementById("txtHint").innerHTML="";
return;
}

xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","getuser.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>

<form>
<select name="users" onchange="showUser(this.value)">
<option value="">Select a person:</option>
<option value="1">612</option>
<option value="2">614</option>
<option value="3">640</option>
<option value="4">641</option>
</select>
</form>
<br>
<div id="txtHint"><b>Results</b></div>

</body>
</html>

and getuser.php

<?php
$q = intval($_GET['q']);

$con = mysqli_connect('localhost','root','password','data');
if (!$con)
{
   die('Could not connect: ' . mysqli_error($con));
}

mysqli_select_db($con,"data");
$sql="SELECT * FROM tech_info WHERE Tech_Num = '".$q."'";

$result = mysqli_query($con,$sql);

echo "<table border='1'>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Tech Number</th>
<th>Mobile Number</th>
</tr>";

while($row = mysqli_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['Tech_F_Name'] . "</td>";
  echo "<td>" . $row['Tech_L_Name'] . "</td>";
  echo "<td>" . $row['Tech_Num'] . "</td>";
  echo "<td>" . $row['Mobile_Num'] . "</td>";
  echo "</tr>";
  }
echo "</table>";

mysqli_close($con);
?> 
  • 写回答

1条回答 默认 最新

  • duanraotun1674 2014-03-26 05:02
    关注

    It's better to use jquery instead of javascript for ajax.Here is code of jquery for AJAX. Don't forget to add js in your scirpt.

    <script> 
    function showUser(str) {
    var datastring ='?q='+str; 
    $.ajax({ 
              type:'POST',
              url : 'getuser.php'
              data:datastring,
              success:function(msg)
              {
                  alert(msg);
              }  
    });
    }
    </script> 
    
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值