我的AJAX下拉列表并没有提取数据库选项

时间:2014-07-31 17:54:42

标签: php jquery ajax forms

构建此表单以便稍后放入更大的表单。这个想法是为了填充第一个下拉列表,然后根据第一个下拉列表填充第二个下拉列表。这是form.php和func.php。这是我第一次进入AJAX。我知道我错过了一些东西,但可能是"代码失明"。

表格

    <?php 
  include('db.php');
  include('func.php');
  ?>
  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"                                 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Chained Select Boxes using PHP, MySQL and jQuery</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/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;
    });
    $('#wait_2').hide();
    $('#drop_2').change(function(){
      $('#wait_2').show();
      $('#result_2').hide();
      $.get("func.php", {
        func: "drop_2",
        drop_var: $('#drop_2').val()
      }, function(response){
        $('#result_2').fadeOut();
        setTimeout("finishAjax2('result_2', '"+escape(response)+"')", 400);
      });
        return false;
    });
});

function finishAjax(id, response) {
  $('#wait_1').hide();
  $('#'+id).html(unescape(response));
  $('#'+id).fadeIn();
}
function finishAjax2(id, response) {
  $('#wait_2').hide();
  $('#'+id).html(unescape(response));
  $('#'+id).fadeIn();
}

function finishAjax_tier_three(id, response) {
  $('#wait_2').hide();
  $('#'+id).html(unescape(response));
  $('#'+id).fadeIn();
}
function finishAjax_tier_four(id, response) {
  $('#wait_3').hide();
  $('#'+id).html(unescape(response));
  $('#'+id).fadeIn();
}
</script>
</head>

<body>
<p>
<form action="" method="post">

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

      <option value="" selected="selected" disabled="disabled">Select Manufacturer</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>
    <span id="wait_2" style="display: none;">
    <img alt="Please Wait" src="ajax-loader.gif"/>
    </span>
    <span id="result_2" style="display: none;"></span> 
      <span id="wait_3" style="display: none;">
    <img alt="Please Wait" src="ajax-loader.gif"/>
    </span>
    <span id="result_3" style="display: none;"></span> 

</form>
</p>
<p>
<?php if(isset($_POST['submit'])){
    $drop = $_POST['drop_1'];
    $drop_2 = $_POST['drop_2'];
    $drop_3 = $_POST['drop_3'];
    $drop_4 = $_POST['drop_4'];
    echo "You have a ";
    echo $drop." ".$drop_2." is selected from ".$drop_3."  ".$drop_4;
?>
<table border="1" bordercolor="#B5B5B5" style="background-color:#FFFFFF" width="250" cellpadding="3" cellspacing="3">
    <tr>
        <td>Manufacturer</td>
        <td><?php echo $drop;?></td>
    </tr>
    <tr>
        <td>Model</td>
        <td><?php echo $drop_2;?></td>
    </tr>
    <tr>
        <td>Size</td>
        <td><?php echo $drop_3;?></td>
    </tr>
    <tr>
        <td>Pressure</td>
        <td><?php echo $drop_4;?></td>
    </tr>
</table>
<?php
}
?>
</body>

功能

    <?php

//     Page load dropdown results     //

function getTierOne()
{
    $result = mysql_query("SELECT FROM stmTrps ASC") 
    or die(mysql_error());

      while($tier = mysql_fetch_array( $result )) 

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

}


//     First selection results     //

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

function drop_1($drop_var)
{  
    include_once('db.php');
    $result = mysql_query("SELECT DISTINCT Trap_Manufacturer FROM stmTrps WHERE Trap_Manufacturer='$drop_var' ORDER BY Trap_Manufacturer") 
    or die(mysql_error());

    echo '<select name="drop_2" id="drop_2">
          <option value=" " disabled="disabled" selected="selected">Select Model</option>';

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

    echo '</select>';
    echo "<script type=\"text/javascript\">
$('#wait_2').hide();
    $('#drop_2').change(function(){
      $('#wait_2').show();
      $('#result_2').hide();
      $.get(\"func.php\", {
        func: \"drop_2\",
        drop_var: $('#drop_1').val(),
        drop_var2: $('#drop_2').val()
      }, function(response){
        $('#result_2').fadeOut();
        setTimeout(\"finishAjax_tier_three('result_2', '\"+escape(response)+\"')\", 400);
      });
        return false;
    });
</script>";
}


//     Second selection results     //

if (isset($_GET['func'])&& $_GET['func'] == "drop_2" ) {
drop_2($_GET['drop_var'], $_GET['drop_var2']);
}

function drop_2($drop_var, $drop_var2)
{  
    include_once('db.php');
    $result = mysql_query("SELECT DISTINCT size FROM stmTrps WHERE Trap_Manufacturer='$drop_var' AND Trap_Model='$drop_var2'") 
    or die(mysql_error());

    echo '<select name="drop_3" id="drop_3">
          <option value=" " disabled="disabled" selected="selected">Select Size</option>';

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

    echo '</select>';
    echo "<script type=\"text/javascript\">
$('#wait_3').hide();
    $('#drop_3').change(function(){
      $('#wait_3').show();
      $('#result_3').hide();
      $.get(\"func.php\", {
        func: \"drop_3\",
        drop_var: $('#drop_1').val(),
        drop_var2: $('#drop_2').val(),
        drop_var3: $('#drop_3').val()
      }, function(response){
        $('#result_3').fadeOut();
        setTimeout(\"finishAjax_tier_four('result_3', '\"+escape(response)+\"')\", 400);
      });
        return false;
    });
</script>";
}


//     Second selection results     //

if(isset($_GET['func'])&& $_GET['func'] == "drop_3" ) {
drop_3($_GET['drop_var'], $_GET['drop_var2'], $_GET['drop_var3']);
}
function drop_3($drop_var, $drop_var2, $drop_var3)
{  
    include_once('db.php');
        $result = mysql_query("SELECT * FROM stmTrps WHERE Trap_Manufacturer='$drop_var' AND Trap_Model='$drop_var2' AND size='$drop_var3'") 
    or die(mysql_error());  

    echo '<select name="drop_4" id="drop_4">
          <option value="" disabled="disabled" selected="selected">Select Pressure</option>';


           while($drop_4 = mysql_fetch_array( $result )) 
                {
                if ($drop_4['accu'] != "") {
              echo '<option value="'.$drop_4['accu'].'">'.$drop_4['accu'].'</option>';
                }
  }
    echo '</select> ';
    echo '<input type="submit" name="submit" value="Submit" />';
}

?>

0 个答案:

没有答案