2

Possible Duplicate:
PHP populate the data from database when District is selected

好的,我在多个链式选择中苦苦挣扎,我不知道什么是 Ajax 或 JQuery。我不介意在 PHP 中使用任何语言。我有一个适用于两次选择但不适用于第三次选择的代码。我有分类 >> 区 >> 车站

当我选择 District 时,我得到了 Station 但是我希望当我选择 Category 时,我应该让 District 和 Station 自动填充。

我正在发布整个代码。不要向我建议任何教程或链接或任何其他代码。如果有人可以帮助我编写代码,那么我将非常感激:)

索引.php

<?php 
error_reporting(E_ALL ^ (E_NOTICE | E_WARNING));
session_start();
include_once('db.php'); 
include_once('func.php');
?>
....

<script type="text/javascript" src="jquery.min.js"></script>

<script type="text/javascript">
$(document).ready(function() {
$('#wait_1').hide();
$('#drop_1').change(function()
    {
  $('#wait_1').show();
  $('#result_1').hide();
  $.get("func.php", 
      {
    func: "drop_1",
    drop_var: $('#drop_1').val()
  }, function(response)
        {
    $('#result_1').fadeOut();
    setTimeout("finishAjax('result_1', '"+escape(response)+"')", 400);
  }
  );
    return false;
});
});

function finishAjax(id, response) {
 $('#wait_1').hide();
 $('#'+id).html(unescape(response));
 $('#'+id).fadeIn();
}
</script>
</head>

<body>
<p>
<form action="" method="post">
<select name="Category" id="Category">

  <option value="" selected="selected" disabled="disabled">Select a Category</option>

  <?php getTierCategory(); ?>

</select> 


<select name="drop_1" id="drop_1">

  <option value="" selected="selected" disabled="disabled">Select a District</option>

  <?php getTierOne(); ?>

</select> 

<span id="wait_1" style="display: none;">
<img alt="Please Wait" src="ajax-loader.gif"/>
</span>
<span id="result_1" style="display: none;"></span> 

</form>
</p>
<p>
<?php 
if(isset($_POST['submit'])){
$drop = $_POST['drop_1'];
$tier_two = $_POST['tier_two'];
echo "You selected ";
$Cat = $_POST['Category'];
echo $drop." & ".$tier_two." & ".$Cat;
}
$mySqlStm = "SELECT * FROM table_name WHERE Category ='$Cat' and Station ='$tier_two'";
$result2 = mysql_query($mySqlStm) or die("Error:mysql_error()"); 
$row=mysql_fetch_array($result2);
$_SESSION['Category1'] = $row['Category'];
$_SESSION['Station1']=$row['Station'];


if(mysql_num_rows($result2) == 0){ 
echo("<br/>no records found"); } 
ELSE { 
echo "<table id='gradient-style'>"; 
//ECHO THE RECORDS FETCHED

while($row1=mysql_fetch_array($result2)) {
echo "<tr>";  
echo "<td><a href='result.php'>" . $row1['Name'] . "</a></td>" ; 
echo "</tr>";
}
echo "</table>"; 
}
?>

函数.php

function  getTierCategory()
{
echo "</br>";
$result = mysql_query("SELECT DISTINCT Category FROM table_name order by Category asc") 
or die(mysql_error());

  while($tier = mysql_fetch_array( $result )) 

    {
       echo '<option value="'.$tier['Category'].'">'.$tier['Category'].'</option>';
    }

}

function getTierOne()
{
echo "<br/>";
$result = mysql_query("SELECT DISTINCT District FROM table_name order by District asc") 
or die(mysql_error());

  while($tier = mysql_fetch_array( $result )) 

    {
       echo '<option value="'.$tier['District'].'">'.$tier['District'].'</option>';
    }

}


if($_GET['func'] == "category" && isset($_GET['func'])) { 
category($_GET['drop_var']); 
}
if($_GET['func'] == "drop_1" && isset($_GET['func'])) { 
drop_1($_GET['drop_var']); 
}

function drop_1($drop_var)
{  
include_once('db.php');
$result = mysql_query("SELECT distinct Station FROM table_name WHERE District='$drop_var'") 
or die(mysql_error());

echo '<select name="tier_two" id="tier_two">
      <option value=" " disabled="disabled" selected="selected">Choose one</option>';

       while($drop_2 = mysql_fetch_array( $result )) 
        {
          echo '<option value="'.$drop_2['Station'].'">'.$drop_2['Station'].'</option>';
        }

echo '</select> ';
echo '<input type="submit" name="submit" value="Submit" />';
}
?>

我正在使用这两个 PHP 文件...我需要尽快解决...

4

0 回答 0