[英]Populate the third select tag using the value of second select tag which has its values from the database using the first select tag
I used Ajax and abc.php(code for it is below) to populate the values of second select tag but i am not able to populate the third select tag which should appear after the selection of the second select tag. 我使用了Ajax和abc.php(下面的代码)来填充第二个select标签的值,但是我无法填充应该在选择第二个select标签之后出现的第三个select标签。 Any suggestion would be highly appreciated 任何建议将不胜感激
<?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);
?>
my html code is 我的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>
blah blah 等等等等
And yes i know sir that the while loop will give me ennumerous select tags. 是的,我知道,先生,while循环会给我很多选择标签。
it should be 它应该是
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>";
}
Can you show your html? 你能显示你的HTML吗?
Try to add onchange="someFunction()"
in the second select tag, where someFunction()
is a javascript function that uses ajax to populate the third select. 尝试在第二个选择标记中添加onchange="someFunction()"
,其中someFunction()
是使用ajax填充第三个选择的javascript函数。
Your question makes no sense, but can I suggest you check your variables are set before using and clean up the sql-injection you have example.com?q=' or '1=1
. 您的问题没有任何意义,但是我可以建议您在使用之前检查变量是否已设置并清理有example.com?q=' or '1=1
的sql-injection。 Currently what your code will do is create a new select box for every result returned, you need to initialize the select outside the loop then insert the option within the loop: 当前,您的代码将为返回的每个结果创建一个新的选择框,您需要在循环外初始化选择,然后在循环内插入选项:
<?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.