【问题标题】:Sending radio button value using AJAX to get PHP file to retrieve data from MySQL使用 AJAX 发送单选按钮值以获取 PHP 文件以从 MySQL 检索数据
【发布时间】:2014-11-03 11:46:37
【问题描述】:

我希望能够使用单选按钮的返回值编写一些 mysql 查询。这是我的代码

<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

  <?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);
  ?>

但是,当我运行代码时,我不断收到此错误: 注意:未定义索引:q 这意味着 q 没有被发送到 php 文件,有什么想法可以解决这个问题吗? 我真的很感激我能得到的所有帮助。对不起,如果这是一个愚蠢的问题,但我真的很陌生,所以我非常感谢您的帮助。谢谢。

【问题讨论】:

    标签: javascript php mysql ajax radio-button


    【解决方案1】:

    改变

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

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

    q= 是错误的 ;)

    【讨论】:

    • 您好,感谢您的回复,我已经按照您的建议进行了更改,但仍然出现错误 ** 注意:未定义索引:q ** 单独运行 php 文件时出现相同的错误也是。
    • 您是否在查询字符串中包含q 的值?例如。在您的浏览器中访问getuser.php?q=123
    • 另外,在您的示例代码中,您在mysqli_close 之后有一个结束 php 标记,但后面还有一些 PHP。这也会导致意外行为...如果结束标记后的附加 PHP 应该在那里,您应该删除 mysqli_close 之后的结束 PHP 标记。
    • @joel harkes 我尝试了以下示例,您能帮我指出哪里出错了吗?
    • 嗨@Annie - 我认为你发布了错误的答案!您的意思是在this one 上发帖吗? :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-26
    • 1970-01-01
    • 2016-02-08
    • 2014-01-25
    • 2013-03-23
    • 1970-01-01
    相关资源
    最近更新 更多