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

时间:2015-05-13 02:59:57

标签: javascript php ajax

我有3套下拉列表。当我选择最后一个下拉列表时,我正在尝试显示mysql表。所有示例仅显示一个下拉列表。非常感谢帮助!! 提前致谢。 :)

这是我的ajax代码:

<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>

//这是html代码

<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>

这是用于验证html和ajax代码:

<?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 个答案:

答案 0 :(得分:0)

@php_purest如果我改变编码变成这样?如何检索表?顺便说一下,我的连接速度很慢,所以我无法观看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>