2017-09-04 104 views
2

我有兩個下拉列表。我正在使用Jquery加載第二個下拉菜單。沒有jqyery我的php代碼工作正常。但是當我使用jquery時,第二個下拉列表在選擇第一個下拉列表時變空。依賴下拉列表不會加載選擇第一個下拉列表

第一個下拉列表(教育)

$sqleducation = "select * from education order by education_id asc "; 
$reseducation = mysqli_query($conn,$sqleducation); 

<select name="education" id="education"> 
<option value="-1">Please Select</option> 
<?php while($roweducation=mysqli_fetch_array($reseducation)){ ?> 
<option value="<?php echo $roweducation['education_id']?>"> 
<?php echo $roweducation['education_name']?> 
</option> 
<?php }?> 
</select> 

第二個下拉(度)

<select name="degree" id="degree" > 
<option value="-1">Please Select</option> 

<?php if(isset($_POST["education_id"]) && !empty($_POST["education_id"])){ 

$sqldegree = "SELECT * FROM degree WHERE education_id = ".$_POST['education_id']." "; 
$resdegree = mysqli_query($conn,$sqldegree); 
while($rowdegree=mysqli_fetch_array($resdegree)) 
    { ?> 

       <option value="<?php echo $rowdegree['degree_id']?>"> 
       <?php echo $rowdegree['degree_name']?> 
       </option> 
      <?php } }?> 
    </select> 

第二個下拉使用juery對第一個下拉教育的選擇加載。

<script src="https://code.jquery.com/jquery-3.2.1.min.js" 
integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" 
    crossorigin="anonymous"></script> 

<script type="text/javascript"> 
    $(document).ready(function(){ 
    $('#education').on('change',function(){ 
    var educationID = $(this).val(); 
    if(educationID){ 
     $.ajax({ 
      type:'POST', 
      url:'education-career.php', 
      data:'education_id='+educationID, 
      success:function(html){ 
       $('#degree').html(html); 

      } 
     }); 
    }else{ 
     $('#degree').html('<option value="">Select Education first</option>'); 

    } 
});});</script> 

回答

0

在這裏,在阿賈克斯,您使用的是

data:'education_id='+educationID, 

這是用於發佈數據。變量名稱將在這裏education_id

而在你的第二頁你正在試圖獲得:

isset($_POST["education"]) 

這隻。因此,在第二頁上,您必須將education替換爲education_id。下面一行

+0

我試了一下,如果(isset($ _ POST [ 「education_id」])&&空($ _ POST [! 「education_id」]))但是沒有iffect –

+0

你也必須在這裏改變:'data:'education_id ='+ educationID,'使用這個:'data:{'education_id'; educationID},' –

1

嘗試改變

data:'education_id='+educationID, 

data:{education_id : educationID}, 
1

試試這個。(第二選擇標籤在第一頁的地方纔能使用$('#degree').html(...)

第一個下拉

$sqleducation = "select * from education order by education_id asc "; 
$reseducation = mysqli_query($conn,$sqleducation); 

<select name="education" id="education"> 
<option value="-1">Please Select</option> 
<?php while($roweducation=mysqli_fetch_array($reseducation)){ ?> 
<option value="<?php echo $roweducation['education_id']?>"> 
<?php echo $roweducation['education_name']?> 
</option> 
<?php }?> 
</select> 
<select name="degree" id="degree" > 
    <option value="-1">Please Select</option> 
</select> 

第二個下拉

<option value="-1">Please Select</option> 

<?php if(isset($_POST["education_id"]) && !empty($_POST["education_id"])){ 

$sqldegree = "SELECT * FROM degree WHERE education_id = ".$_POST['education_id']." "; 
$resdegree = mysqli_query($conn,$sqldegree); 
while($rowdegree=mysqli_fetch_array($resdegree)) 
    { ?> 
     <option value="<?php echo $rowdegree['degree_id']?>"> 
     <?php echo $rowdegree['degree_name']?> 
     </option> 
    <?php } }?> 
+0

你做了什麼改變? –

+0

@SaritaSharma,第二個選擇標籤必須放在第一頁才能使用$('#degree')。html(...)。 –

+0

@Star_Man老兄。在第二個下拉菜單中檢查你的位置。 –

0

變化:

$('#education').on('change',function(){ 

要:

$('select#education').change(function(){ 
相關問題