PHP下拉,每个都是可靠的 [英] PHP drop down which each are dependable
问题描述
我有这个问题。使用HTML和PHP。
我可以知道如何做到这一点。我有2个下拉列表,例如A和B.下拉B取决于下拉列表A.例子A具有这些选项,这些选项将从dbase中调用(没有这个问题,tq)(Jack,Carol)和B如果选择杰克(T1,T2,T3),如果选择卡罗尔(T1,T2,T3,T4,T5),则选择取决于A:如果选择杰克(T1,T2,T3)示例界面。
alt text http://i41.tinypic .com / 2vk1fgk.png
有人可以帮我解决这个问题吗?
谢谢。
在这种情况下,您需要使用Ajax。不刷新页面选择任何A列将给出相应的B列值。例如
< form method =postname =form1>
< table border =0cellpadding =0cellspacing =0width =60%>< tbody>
< tr>
< td width =150>国家< / td>
< td width =150>< select style =background-color:#ffffa0name =countryonchange =getState(this.value)>< option> Select Country< ; / option>< option value =1> USA< / option>< option value =2> Canada< / option> < /选择>< / TD>
< / tr>
< tr>
< td>州< / td>
< td>
< p id =statementsiv>
< select style =background-color:#ffffa0name =state>< option> Select Country First< / option> < /选择>< / TD>
< / tr>
< tr>
< td>城市< / td>
< td>
< p id =citydiv>
< select style =background-color:#ffffa0name =city>< option>选择状态优先< / option> < /选择>< / TD>
< / tr>
< / tbody>< / table>
< / form>
正如您在上面看到的,在国家的onChage事件下拉列表中的getState()函数javascript调用了更改state下拉菜单的选项值,让我们看看getState()函数的代码。
function getState (countryId)
{
var strURL =findState.php?country =+ countryId;
var req = getXMLHTTP();
if(req)
{
req.onreadystatechange = function()
{
if(req.readyState == 4)
{
//只有当OK时
if(req.status == 200)
{
document.getElementById('statementsiv')。innerHTML = req.responseText;
} else {
alert(使用XMLHTTP时出现问题:\ n+ req.statusText);
req.open(GET,strURL,true);
req.send(null);
}
}
PHP文件findState.php的代码,
< ;?在下拉菜单中填充从Ajax获取的状态的选项。 $国家= INTVAL($ _ GET [国家]);
$ link = mysql_connect('localhost','root','');如果(!$ link){
die('Could not connect:'。mysql_error()); //改变所需的
中的配置。
}
mysql_select_db('db_ajax');
$ query =SELECT id,statename FROM state WHERE countryid ='$ country';
$ result = mysql_query($ query);
?>
< option>选择状态< / option>
<? while($ row = mysql_fetch_array($ result)){?>
<? }?>
< / select>
在上面的状态下拉列表中,getCity()函数在带有countryId和stateId参数的onChage事件中调用,现在让我们来看看getCity()函数的代码。
$ b $ pre $ 函数getCity(countryId,stateId)
{
var strURL =findCity.php?country =+ countryId +& state =+ stateId;
var req = getXMLHTTP();
if(req)
{
req.onreadystatechange = function()
{
if(req.readyState == 4)//仅当OK
{
if(req.status == 200)
{
document.getElementById('citydiv')。innerHTML = req.responseText;
} else {
alert(使用XMLHTTP时出现问题:\ n+ req.statusText);
req.open(GET,strURL,true);
req.send(null);
$ b $ p
$ b在上面的ajax函数中,findcity.php被调用并且此PHP文件根据get方法提供的参数country和state填充城市下拉列表。现在让我们来看一下findcity.php的代码,
<?php $ countryId = intval($ _ GET ['country' ]);
$ stateId = intval($ _ GET ['state']);
$ link = mysql_connect('localhost','root','');如果(!$ link){
die('Could not connect:'。mysql_error()); //改变所需的
中的配置。
}
mysql_select_db('db_ajax');
$ query =SELECT id,city FROM city WHERE countryid ='$ countryId'AND stateid ='$ stateId';
$ result = mysql_query($ query);
?>
< select name =city>
< option>选择城市< / option>
<?php while($ row = mysql_fetch_array($ result)){?>
< option value><?= $ row ['city']?>< / option>
<?php}?>
< / select>
这就是所有使用Ajax和PHP的城市,国家和州的三重下拉列表人口稠密。
I have this problems. using html and php.
May I know how to do this. I have 2 drop down, eg A and B. Drop down B is depend to the drop down A. Example, A have these options which will be called from dbase(no prob with this, tq) (Jack, Carol), and B wil have options depend on A: if select Jack(T1, T2, T3), if select carol(T1,T2,T3,T4,T5).
Here are the sample interface.
alt text http://i41.tinypic.com/2vk1fgk.png
Can someone help me with this?
thank you.
U need to Work with Ajax In this Case. Without Refreshing the Page Selecting any of the A column will give u corresponding B column Value. For Example
<form method="post" name="form1">
<table border="0" cellpadding="0" cellspacing="0" width="60%"><tbody>
<tr>
<td width="150">Country</td>
<td width="150"><select style="background-color: #ffffa0" name="country" onchange="getState(this.value)"><option>Select Country</option><option value="1">USA</option><option value="2">Canada</option> </select></td>
</tr>
<tr>
<td>State</td>
<td>
<p id="statediv">
<select style="background-color: #ffffa0" name="state"><option>Select Country First</option> </select></td>
</tr>
<tr>
<td>City</td>
<td>
<p id="citydiv">
<select style="background-color: #ffffa0" name="city"><option>Select State First</option> </select></td>
</tr>
</tbody></table>
</form>
As you can see above, in the onChage event of the country drop down getState() function of the javascript is called which change the options values the State drop down, let’s look at the code the getState() function.
function getState(countryId)
{
var strURL="findState.php?country="+countryId;
var req = getXMLHTTP();
if (req)
{
req.onreadystatechange = function()
{
if (req.readyState == 4)
{
// only if "OK"
if (req.status == 200)
{
document.getElementById('statediv').innerHTML=req.responseText;
} else {
alert("There was a problem while using XMLHTTP:\n" + req.statusText);
}
}
}
req.open("GET", strURL, true);
req.send(null);
}
}
The code of the PHP file findState.php, which populate the options in the drop down of the state which is fetched from Ajax , is given below
<? $country=intval($_GET['country']);
$link = mysql_connect('localhost', 'root', ''); //changet the configuration in required
if (!$link) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db('db_ajax');
$query="SELECT id,statename FROM state WHERE countryid='$country'";
$result=mysql_query($query);
?>
<select name="state" onchange="getCity(<?=$country?>,this.value)">
<option>Select State</option>
<? while($row=mysql_fetch_array($result)) { ?>
<option value=<?=$row['id']?>><?=$row['statename']?></option>
<? } ?>
</select>
In the above state dropdown, getCity() function is called in onChage event with countryId and stateId parameter, now let’s look at the code of the getCity() function
function getCity(countryId,stateId)
{
var strURL="findCity.php?country="+countryId+"&state="+stateId;
var req = getXMLHTTP();
if (req)
{
req.onreadystatechange = function()
{
if (req.readyState == 4) // only if "OK"
{
if (req.status == 200)
{
document.getElementById('citydiv').innerHTML=req.responseText;
} else {
alert("There was a problem while using XMLHTTP:\n" + req.statusText);
}
}
}
req.open("GET", strURL, true);
req.send(null);
}
}
In the above ajax function, findcity.php is called and this PHP file populate the city dropdown according to the supplied parameters country and state from get method. Now let’s look at the code of findcity.php,
<?php $countryId=intval($_GET['country']);
$stateId=intval($_GET['state']);
$link = mysql_connect('localhost', 'root', ''); //changet the configuration in required
if (!$link) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db('db_ajax');
$query="SELECT id,city FROM city WHERE countryid='$countryId' AND stateid='$stateId'";
$result=mysql_query($query);
?>
<select name="city">
<option>Select City</option>
<?php while($row=mysql_fetch_array($result)) { ?>
<option value><?=$row['city']?></option>
<?php } ?>
</select>
And thats all, the triple drop down list of city, country and state using Ajax and PHP will be populated.
这篇关于PHP下拉,每个都是可靠的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!