dp152153 2015-05-13 02:59
浏览 68
已采纳

如何在选择第三个下拉列表时检索表

i am having 3 sets of drop down list. i am trying to display the mysql table when i selected the last dropdown list. all example are shows for only one dropdown only. Help are really appreciated!! Thanks in advance. :)

This is my ajax code:

<script type="text/javascript">
function ajaxFunction()
{

var httpxml;
try
  {
  // Firefox, Opera 8.0+, Safari
  httpxml=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    httpxml=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    try
      {
      httpxml=new ActiveXObject("Microsoft.XMLHTTP");
      }
    catch (e)
      {
      alert("Your browser does not support AJAX!");
      return false;
      }
    }
  }
function stateChanged() 
    {
    if(httpxml.readyState==4)
      {
//alert(httpxml.responseText);
var myObject = JSON.parse(httpxml.responseText);

for(j=document.myForm.PTT.options.length-1;j>=0;j--)
{
    document.myForm.PTT.remove(j);
}

var PTT1=myObject.value.PTT1; // ptt1 remove change to PTT only.

//var optn = document.createElement("OPTION");
//optn.text = 'Select PTT';
//optn.value = '';
document.myForm.PTT.options.add(optn);
for (i=0;i<myObject.PTT.length;i++)
    {
    var optn = document.createElement("OPTION");
    optn.text = myObject.PTT[i];
    optn.value = myObject.PTT[i];
    document.myForm.PTT.options.add(optn);


    if(optn.value==PTT1){
        var k= i+1;
        document.myForm.PTT.options[k].selected=true;
        }
    } 

//////////////////////////
for(j=document.myForm.EXT.options.length-1;j>=0;j--)
    {
        document.myForm.EXT.remove(j);
    }
    var EXT1=myObject.value.EXT1;
//alert(city1);
    for (i=0;i<myObject.EXT.length;i++)
    {
        var optn = document.createElement("OPTION");
        optn.text = myObject.EXT[i];
        optn.value = myObject.EXT[i];
        document.myForm.EXT.options.add(optn);

        if(optn.value==EXT1){
        document.myForm.EXT.options[i].selected=true;
        }

    } 

///////////////////////////
    document.getElementById("txtHint").style.background='#00f040';
    document.getElementById("txtHint").innerHTML='done';
//setTimeout("document.getElementById('txtHint').style.display='none'",3000)
    }
    }

    var url="checking2.php";
    var REGION=myForm.REGION.value;

    if(choice != 's1'){
        var PTT=myForm.PTT.value;
        var EXT=myForm.EXT.value;
            }else{
                var PTT='';
                var EXT='';
                }

    url=url+"?REGION="+REGION;
    url=url+"&PTT="+PTT;
    url=url+"&EXT="+EXT;
//url=url+"&id="+Math.random();
    myForm.st.value=PTT;
//alert(url);
    document.getElementById("txtHint2").innerHTML=url;
    httpxml.onreadystatechange=stateChanged;
    httpxml.open("GET",url,true);
    httpxml.send(null);
    document.getElementById("txtHint").innerHTML="Please Wait....";
    document.getElementById("txtHint").style.background='#f1f1f1';
}
</script>

//this is the html code

<div id="txtHint" style="width : 100px;background-color: #cccc33;">Message area</div>
    <br><br>
    <form name="myForm" action='details.php' method='post'">
    <input type=hidden name=st value=0>
    <table width=500>
    //use this function to collect the value of region from table.
    <tr><td >
    Select Region:<br><select name=REGION id='s1' onchange=ajaxFunction('s1');>
    <option value=''>Select One</option>

    <?Php

        require "config2.php";// connection to database 
        $sql="select distinct REGION from all_exchangelist ";
        foreach ($dbo->query($sql) as $row) {
        echo "<option value=$row[REGION]>$row[REGION]</option>";
            }
    ?>
</select>

</td><td ><select name=PTT  onchange=ajaxFunction('s2')>
<option value=''>Select One</option></select></td>
<td ><select name=EXT  onchange=ajaxFunction('s3')>
<option value=''>Select One</option></select></td>
</tr></tr>
<tr><td colspan=3><input type=submit value='Submit'></td></tr>
</form>
</table>
<br><br>
<div id="txtHint2"></div>

This is for validating the html and ajax code:

<?Php
include "config2.php"; // connection details

error_reporting(0);// With this no error reporting will be there
//////////
/////////////////////////////////////////////////////////////////////////////
$REGION=$_GET['REGION'];// 
$PTT1=$_GET['PTT'];
$EXT1=$_GET['EXT'];

///////////// Validate the inputs ////////////
if((strlen($REGION)) > 0 and (!ctype_alpha($REGION))){ 
echo "Data Error";
exit;
}
// Checking ptt variable (with space ) ///

if ((strlen($PTT1)) > 0 and ctype_alpha(str_replace(' ', '', $PTT1)) === false) {
echo "Data Error";
exit;
}

/////////// end of input validation //////

if(strlen($REGION) > 0){
$q_REGION="select distinct(PTT) from all_exchangelist where REGION = '$REGION'";
}else{
$q_REGION="select distinct(PTT) from all_exchangelist";
}
//echo $q_region;
$sth = $dbo->prepare($q_REGION);
$sth->execute();
$PTT = $sth->fetchAll(PDO::FETCH_COLUMN);

$q_PTT="select distinct(EXT) from all_exchangelist where ";
if(strlen($REGION) > 0){
$q_PTT= $q_PTT . " REGION = '$REGION' ";
}
if(strlen($PTT1) > 0){$q_PTT= $q_PTT . " and  PTT='$PTT1'";}
$sth = $dbo->prepare($q_PTT);
$sth->execute();
$EXT = $sth->fetchAll(PDO::FETCH_COLUMN);

$main = array('PTT'=>$PTT,'EXT'=>$EXT,'value'=>array("PTT1"=>"$PTT1","EXT1"=>"$EXT1"));
echo json_encode($main); 
?> 
  • 写回答

1条回答 默认 最新

  • dongxiong1941 2015-05-14 04:30
    关注

    @php_purest if i change the coding become like this? how to retrieve the table? btw, my connection is slow, so i cannot watch the youtube.

    <?php
    
    require "config2.php"; // Your Database details 
    ?>
    
    <!doctype html public "-//w3c//dtd html 3.2//en">
    
    <html>
    
    <head>
    <title></title>
    <meta name="GENERATOR" content="Arachnophilia 4.0">
    <meta name="FORMATTER" content="Arachnophilia 4.0">
    <SCRIPT language=JavaScript>
    function reload(form)
    {
    var value=form.REGION.options[form.REGION.options.selectedIndex].value; 
    self.location='threelist.php?REGION=' + value ;
    }
    function reload3(form)
    {
    var value=form.REGION.options[form.REGION.options.selectedIndex].value; 
    var value2=form.PTT.options[form.PTT.options.selectedIndex].value; 
    
    self.location='threelist.php?REGION=' + value + '&PTT=' + value2 ;
    }
    
    </script>
    </head>
    
    <body>
    <?Php
    
    
    ///////// Getting the data from Mysql table for first list box//////////
    $query2="SELECT DISTINCT REGION FROM all_exchangelist"; 
    ///////////// End of query for first list box////////////
    
    /////// for second drop down list we will check if category is selected else we will display all the subcategory///// 
    $REGION=$_GET['REGION']; // This line is added to take care if your global variable is off
    if(isset($PTT) and strlen($PTT) > 0)
    {
        $query="SELECT DISTINCT PTT FROM all_exchangelist where PTT=$REGION"; 
    }else{$query="SELECT DISTINCT PTT FROM all_exchangelist"; } 
    ////////// end of query for second subcategory drop down list box ///////////////////////////
    
    
    /////// for Third drop down list we will check if sub category is selected else we will display all the subcategory3///// 
    $PTT=$_GET['PTT']; // This line is added to take care if your global variable is off
    if(isset($EXT) and strlen($EXT) > 0){
    $query3="SELECT DISTINCT EXT FROM all_exchangelist where EXT=$PTT"; 
    }else{$query3="SELECT DISTINCT EXT FROM all_exchangelist"; } 
    ////////// end of query for third subcategory drop down list box ///////////////////////////
    
    
    echo "<form method=post name=f1 action='threelistcheck.php'>";
    //////////        Starting of first drop downlist /////////
    echo "<select name='REGION' onchange=\"reload(this.form)\">
          <option value=''>Select one</option>";
    
            foreach ($dbo->query($query2) as $noticia2) 
            {
            if($noticia2['REGION']==@$REGION)
            {   
                echo "<option selected value='$noticia2[REGION]'>$noticia2[REGION]</option>"."<BR>";
            }
            else
            {   
                echo  "<option value='$noticia2[REGION]'>$noticia2[REGION]</option>";
            }
            }
            echo "</select>";
    //////////////////  This will end the first drop down list ///////////
    
    //////////        Starting of second drop downlist /////////
        echo "<select name='PTT' onchange=\"reload3(this.form)\">
              <option value=''>Select one</option>";
    
                foreach ($dbo->query($query) as $noticia) 
                {
                    if($noticia['PTT']==@$PTT)
                    {
                    echo "<option selected value='$noticia[PTT]'>$noticia[PTT]</option>"."<BR>";
                    }
                    else
                        {
                            echo  "<option value='$noticia[PTT]'>$noticia[PTT]</option>";}
                        }
                    echo "</select>";
    //////////////////  This will end the second drop down list ///////////
    
    
    //////////        Starting of third drop downlist /////////
        echo "<select name='EXT' ><option value=''>Select one</option>";
            foreach ($dbo->query($query3) as $noticia) 
            {
                echo  "<option value='$noticia[EXT]'>$noticia[EXT]</option>";
            }
            echo "</select>";
    //////////////////  This will end the third drop down list ///////////
    
    
        echo "<input type=submit value='Submit the form data'></form>";
        ?>
    <br><br>
    <a href=threelist.php>Reset and Try again</a>
    
    </body>
    </html>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?