簡體   English   中英

使用第二選擇標簽的值填充第三選擇標簽,第二選擇標簽的值使用第一選擇標簽從數據庫中獲取

[英]Populate the third select tag using the value of second select tag which has its values from the database using the first select tag

我使用了Ajax和abc.php(下面的代碼)來填充第二個select標簽的值,但是我無法填充應該在選擇第二個select標簽之后出現的第三個select標簽。 任何建議將不勝感激

<?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循環會給我很多選擇標簽。

它應該是

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>";
}

你能顯示你的HTML嗎?

嘗試在第二個選擇標記中添加onchange="someFunction()" ,其中someFunction()是使用ajax填充第三個選擇的javascript函數。

您的問題沒有任何意義,但是我可以建議您在使用之前檢查變量是否已設置並清理有example.com?q=' or '1=1的sql-injection。 當前,您的代碼將為返回的每個結果創建一個新的選擇框,您需要在循環外初始化選擇,然后在循環內插入選項:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM