本文介绍了发送使用AJAX来让PHP文件单选按钮值从MySQL检索数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能写我的单选按钮(S)的返回值(S)几MySQL的查询。这里是code我有

I want to be able to write a few mysql queries with the returned value(s) of my radio button(s). Here is the code I have

<html>
<head>
<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) {
  document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
 }
}
xmlhttp.open("GET","getuser.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>

                <?php include 'db_connector.php';

                $result = mysqli_query($con,"SELECT * FROM os_scope");

                while($row = mysqli_fetch_array($result)) {

                $row['name'];

                echo "<li><div class='toggle-btn-grp cssonly'>

                        <div><form><input type='radio' name='os' value=".$row['name']." id='myRadio' onchange='showUser(this.value)'>

                        <label class='toggle-btn'><div class='title'>".$row['name']."</form></div></label></div></div></li>";

            }

                ?>
<br>
<div id="txtHint"><b>Person info will be listed here.</b></div>

</body>
</html>

下面是我的PHP文件getuser.php

Here is my php file getuser.php

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

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

  mysqli_select_db($con,"ajax_demo");
  $sql="SELECT * FROM os ";
  $result = mysqli_query($con,$sql);

  echo "<table border='1'>
  <tr>
  <th>ID</th>
  <th>Name</th>
  <th>Scope</th>
  <th>Client</th>
  <th>Supplier</th>
  </tr>";

  while($row = mysqli_fetch_array($result)) {
  echo "<tr>";
  echo "<td>" . $row['id'] . "</td>";
  echo "<td>" . $row['name'] . "</td>";
  echo "<td>" . $row['scope'] . "</td>";
  echo "<td>" . $row['client'] . "</td>";
  echo "<td>" . $row['supplier'] . "</td>";
  echo "</tr>";
  }
  echo "</table>";

  mysqli_close($con);
  ?>

然而,当我运行code我不断收到此错误:注意:未定义指数:Q 这意味着Q不是被发送到PHP文件,任何想法如何,我可以解决这一问题?我真的AP preciate所有帮助我能。很抱歉,如果这是一个愚蠢的问题,但我真的在这个新的,所以我真的AP preciate的帮助。谢谢你。

However, when I run the code I keep getting this error:Notice: Undefined index: qWhich means q is not been sent to the php file, any ideas how I can fix this?I would really appreciate all the help I can get. Sorry if this is a dumb question but am really new at this, so I would really appreciate the help. Thanks.

推荐答案

修改

xmlhttp.open("GET","getuser.php?=q"+str,true);

xmlhttp.open("GET","getuser.php?q="+str,true);

= 是走错了路周围;)

The q and = are the wrong way around ;)

这篇关于发送使用AJAX来让PHP文件单选按钮值从MySQL检索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 01:36