我使用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 & 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>
blat blah
是的,我知道先生,while循环会给我很多选择标签。
答案 0 :(得分: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>";
}
答案 1 :(得分:0)
你能展示你的HTML吗?
尝试在第二个选择标记中添加onchange="someFunction()"
,其中someFunction()
是一个javascript函数,它使用ajax填充第三个选择。
答案 2 :(得分:0)
您的问题毫无意义,但我可以建议您在使用之前检查您的变量是否已设置并清理您拥有的example.com?q=' or '1=1
sql注入。目前您的代码将为返回的每个结果创建一个新的选择框,您需要在循环外初始化选择,然后在循环中插入选项:
<?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;
}
?>