链式多选

时间:2012-07-29 19:21:38

标签: php jquery mysql ajax

  

可能重复:
  PHP populate the data from database when District is selected

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

当我选择区时,我得到了车站 但是我希望当我选择类别时,我应该自动填充区和站。

我发布了整个代码。不建议我任何教程或链接或任何其他代码。如果任何人可以帮助我使用我的代码,那么我将非常感谢:)

的index.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>"; 
}
?>

func.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文件......我需要解决方案尽快......

0 个答案:

没有答案