0

我使用 Ajax 和 abc.php(代码如下)来填充第二个选择标签的值,但我无法填充第三个选择标签,它应该出现在选择第二个选择标签之后。任何建议将不胜感激

<?php
$q=$_GET["q"];
include "localhost.php";
$sql="SELECT * FROM books WHERE class = '".$q."'";
$result = mysql_query($sql);
if(mysql_num_rows($result) > 0)
{
  while($row = mysql_fetch_array($result))
  {
     echo "<select name=\"name\">
           <option>Select subject
           </option>";
    echo "<option>" . $row['name'] ."</option>";
    echo "</select>";
  }          
}
else
{
   echo "error";
}

mysql_close($con);
?>

我的html代码是

<?php 
    include "menu.php";
    include "localhost.php";

?>
<!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>
<!--Code for selecting class-->
function showUser(str)
{
if (str=="Select class:")
  {
  document.getElementById("txtHint").innerHTML="Select any class";
  return;
  }
  if (str=="Select cla:")
  {
  document.getElementById("txtHint1").innerHTML="Select any cla";
  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","select11.php?q="+str,true);
xmlhttp.send();
}
<!--End of Code for selecting class-->








</script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>


        <link type="text/css" href="style.css" rel="stylesheet" />
</head>

<body>

        <div id="sn">

            <ul class="crumbs">
                <li class="first"><a href="iindex.php" style="z-index:9;"><span></span>Home</a></li>
                <li><a href="#" style="z-index:8;">Books</a></li>
                <li><a href="#" style="z-index:7;">Sale Books</a></li>
            </ul>

    </div>
    <div id="newad">
<fieldset>
    <legend><strong>Sale Books &amp; Stationary</strong></legend>
    <form >
      <table width="499" >
        <tr>
          <td width="96">Select Class:</td>
          <td width="139"><select required="required" x-moz-errormessage="Select the Class" name="class" id="select" onchange="showUser(this.value)">
            <option  selected="selected">Select class:</option> 

胡说八道

是的,我知道先生,while 循环会给我无数的选择标签。

4

3 回答 3

1

它应该是

if(mysql_num_rows($result) > 0)
{
    echo "<select name=\"name\"><option>Select subject</option>";
    while($row = mysql_fetch_array($result))
    {         
         echo "<option>" . $row['name'] ."</option>";

    }
  echo "</select>";
}
于 2013-01-12T14:08:50.493 回答
0

你能显示你的html吗?

尝试添加onchange="someFunction()"第二个选择标记,其中someFunction()是一个使用 ajax 填充第三个选择的 javascript 函数。

于 2013-01-12T14:08:06.243 回答
0

你的问题没有意义,但我可以建议你在使用之前检查你的变量是否设置并清理你拥有的 sql-injection example.com?q=' or '1=1。目前,您的代码将做的是为每个返回的结果创建一个新的选择框,您需要在循环外初始化选择,然后在循环内插入选项:

<?php
$q = !empty($_GET["q"])?$_GET["q"]:null;

include "localhost.php";

if($q != null){
    $sql="SELECT * FROM books WHERE class = '".mysql_real_escape_string($q)."'";

    $result = mysql_query($sql);

    $sel = '<select name="name"><option>Select subject</option>';
    if(mysql_num_rows($result) > 0){
        while($row = mysql_fetch_array($result)){
            $sel .= "<option>" . $row['name'] ."</option>";
        }
    }else{
        $sel .= '<option>No Books</option>';
    }
    $sel .= "</select>";


    echo $sel;
}
?>
于 2013-01-12T14:08:57.457 回答