2016-03-02 64 views
0

的 「id」 屬性,但不是 「名」 我有4個文件,如下圖所示:爲什麼我只能拉從MySQL

PostVent.php:

<!DOCTYPE html> 
<html lang = "en"> 
<head> 
    <meta charset = "utf-8" /> 
    <link rel="stylesheet" type="text/css" href="PostLinkStyle.css" /> 
    <style type="text/css"> 
    legend.standout{font-weight: bold; font-size: 24pt;} 
    </style>  
    <title>Post Vent</title> 
</head> 
<body> 
<!-- start header div --> 
<div id="header"> 
    <h3>SomeVent</h3> 
</div> 
<div id="wrap"> 
    <form action="" method="post"> 
     <div class="row"> 
      <div class="large-8 small-centered columns"> 
       <fieldset> 
        <legend id="legend">Post Vent</legend> 
        <div class="row"> 
         <div class="small-12 columns"> 
          <label for="email">Email</label> 
          <input type="text" id="email" size="35"></input> 
         </div> 
        </div> 
        <div class="row"> 
         <div class="small-8 columns"> 
          <?php 
          //Include database configuration file 
          include('dbConfig.php'); 
          include('index.php'); 
          //Get all state data 
          $query = $db->query("SELECT **name** FROM states"); 
          //Count total number of rows 
          $rowCount = $query->num_rows; 
          ?> 
          <select name="state" id="state"> 
           <option value="">Select State</option> 
           <?php 
           if($rowCount > 0){ 
            while($row = $query->fetch_array()){ 
             echo '<option value="'.$row['name'].'">'.$row['id'].'</option>'; 
            } 
           }else{ 
            echo '<option value="">States not available</option>'; 
           } 
           ?> 
          </select> 
          <select name="county" id="county"> 
           <option value="">Select County</option> 
          </select> 
         </div> 
        </div> 

的index.php:

<script src="jquery.min.js"></script> 
<script type="text/javascript"> 
$(document).ready(function(){ 
    $('#state').on('change',function(){ 
     var stateID = $(this).val(); 
     if(stateID){ 
      $.ajax({ 
       type:'POST', 
       url:'ajaxData.php', 
       data:'id='+stateID, 
       success:function(html){ 
        $('#county').html(html); 
       } 
      }); 
     }else{ 
      $('#county').html('<option value="">Select state first</option>'); 
     } 
    }); 
    $('#county').on('change',function(){ 
     var countyID = $(this).val(); 
     if(countyID){ 
      $.ajax({ 
       type:'POST', 
       url:'ajaxData.php', 
       data:'id='+stateID, 
       success:function(html){ 
        $('#county').html(html); 
       } 
      }); 
     }else{ 
      $('#city').html('<option value="">Select state first</option>'); 
     } 
    }); 
}); 
</script> 

ajax.php:

<?php 
//Include database configuration file 
include('dbConfig.php'); 
if(isset($_POST["id"]) && !empty($_POST["id"])){ 
    //Get all county data 
    $query = $db->query("SELECT name FROM counties..."); 
    //Count total number of rows 
    $rowCount = $query->num_rows; 
    //Display county list 
    if($rowCount > 0){ 
     echo '<option value="">Select County</option>'; 
     while($row = $query->fetch_assoc()){ 
      echo '<option value="'.$row['id'].'">'.$row['name'].'</option>'; 
     } 
    }else{ 
     echo '<option value="">County not available</option>'; 
    } 
} 
?> 

再加上我DAT基礎配置文件(我不認爲它需要顯示)。我試圖根據選定的狀態動態填充縣下拉菜單。

我對此非常感興趣,並將我在網上提供的許多教程放在一起。我的問題是這個。我的縣菜單怎麼沒有填充?我試圖將選定的狀態ID屬性發布到ajax以獲得具有相同ID的縣。看不到有什麼問題。

+0

尋找'SELECT ID,名字從states' ??? – devpro

+1

您正在使用'SELECT name FROM states'名稱查詢並打印結果中的'echo'';'??? – devpro

回答

1

當然,你不會得到你想要的,因爲在你的PostVent.php你正在顯示的是id,隱藏值是name。它應該是:

<option>
echo '<option value="' . $row['id'] . '">' . $row['name'] . '</option>'; 

語法是<option value='[Specifies the value to be sent to a server]'>[Label or display value]</option>

而且您的查詢應該是:

SELECT id, name FROM states ORDER BY name 
+0

謝謝你的幫助,我沒有意識到我藏了一個。 – user3499087

+0

關於在哪裏查看我的縣列表無法加載的建議?我正試圖通過選定的國家ID拉起縣? – user3499087