2012-02-25 107 views
0

我有两个下拉列表,我想第二个下拉得到填充从第一下拉填充用ajax

这里选择的数据的基础上,两个下拉列表是我的main.php:

<?php 
include('connection.php'); 
$query_religion="SELECT DISTINCT religion FROM religion_caste_table"; 
$result_religion = mysql_query($query_religion, $con); 
?> 

<html> 
<body> 
    <select name="religion" id="religion" style="width: 100px" onChange="showcaste(this.value)"> 
           <?php 
           while($q_rel_data = mysql_fetch_array($result_religion)) 
           {?> 
           <option value="<?php echo $q_rel_data[0]; ?>"> 
           <?php echo $q_rel_data[0]; ?> 
           </option> 
           <?php }?> 
          </select> 
    <div id="view"></div> 
    <select name="caste" id="caste" style="width: 100px"></select> 
</body> 
</html> 

这是getcaste.php:

<?php 
include('connection.php'); 
$string=$_GET["religion"]; 

$sql="SELECT caste FROM religion_caste_table WHERE religion = '".$string."'"; 
$result = mysql_query($sql,$con); 
$myarray=array(); 
$str=""; 
while($result_caste=mysql_fetch_array($result)) 
{ 
$str=$str . "\"$result_caste[caste]\"".","; 
} 
$str=substr($str,0,(strLen($str)-1)); // Removing the last char , from the string 
echo $str;/*Stop hiding from IE Mac */ 

//echo $string; 
mysql_close($con); 
?> 
<html> 
</html> 
现在

的JavaScript的AJAX文件,(religion_caste.js)

function showcaste(string) 
{ 
if (string=="") 
    { 
    document.getElementById("caste").innerHTML=""; 
    return; 
    } 

alert(string); 
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("view").innerHTML=xmlhttp.responseText; 
     //now how to catch the string from (getcaste.php), split it and save it into array 
      // and to populate the second dropdwn by the array; 

    } 
    } 
xmlhttp.open("GET","getcaste.php?religion="+string,true); 
xmlhttp.send(); 
} 

为了获得更好的帮助,数据库表包含两个字段,宗教和种姓,其中第一个属性是宗教,第二个是种姓。

如果以某种方式我可以捕捉到字符串str,那么它可以解决此事。

document.getElementById("view").InnerHTML=xmlhttp.responseText提供 “STR1”, “STR2”,......在<div id="view">

浏览的字符串,但如果我写了下面的代码:

var string=(document.getElementById("view").InnerHTML); 
alert(string); 

然后一个MsgBox这样的弹出查看:

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> 
<title>Untitled Document</title> 




"Catholic","Protestant" 

如果我能得到字符串,我会很高兴。

+0

好友,我正在编辑我的问题,你现在可以看到它....代码已经提供 – Saswat 2012-02-25 05:37:41

+0

为什么不使用jQuery? – 2012-02-25 05:58:57

回答

1

因此,在您的位置,首先会在getcaste.php中重做一次 - 将查询中的所有结果放入数组中,然后使用implode使用分隔符获取字符串 - ;。 (Optionaly你可以使用json并在js中解码它)。在religion_caste.js中使用split来获取数组(;作为分隔符); 现在通过所有的值使用循环周期,每个周期加一个选择到选择

例如:

var select = document.getElementById("caste"); 
for (var key in values) 
{ 
    var OptNew = document.createElement('option'); 
    OptNew.text = values[key]; 
    OptNew.value = key; 
    try 
    { 
     select.add(OptNew, null); // doesn't work in IE 
    } 
    catch(ex) 
    { 
     select.add(OptNew); // IE only 
    } 
} 

编辑:

所以JSON在PHP中很容易 - json_encode JSON在JS有点困难。新的浏览器应该支持JSON.parse,对于旧的浏览器,你必须使用这个code(因为我总是使用jQuery,所以不能100%确定)。或者你可以使用jQuery和jQuery.parseJSON

+0

谢谢你的朋友,但我试图用json来做到这一点,但是让它成为可能......请你用getcaste.php中的json代码更详细地告诉我一些问题并解码它.....我会非常感谢......我已经在getcaste中获得$ str,如下面的格式...“shia”,“suni”,“wahabbi”....如果我可以使用var myarray = new Array(xmlhttp。 responseText)或类似的东西,那么它可以转换为数组 – Saswat 2012-02-25 06:09:37

+0

Thanx朋友,我的工作,并得到它完全解决.....虽然我没有使用JSON的概念,而是我用split()函数与一点技巧,它很好地工作....很多谢谢你的帮助...现在得到了下拉填充也...... – Saswat 2012-02-26 03:39:53