2016-12-23 55 views
0

我有一個main.php頁面,有一些複選框和一個文本框,我試圖實現的是選擇「Select All或任何應該傳遞值的複選框」。複選框更改函數應該觸發併發布數據到ajaxData.php。 Ajaxdata.php應將相應的團隊成員名稱返回到main.php頁面,並在文本框中顯示fname值。複選框Array,jquery,PHP

上點擊右鍵,現在是什麼的問題發生在任何複選框

main.php頁低於

Select All <input type="checkbox" name="select_all" id="select_all" value="1"> 

Pythons<input type="checkbox" name="select[]" id="team" value="Pythons"> 
Wipers <input type="checkbox" name="select[]" id="team" value="Wipers"> 
<input type="textbox" name="first_name" value=""> 
<script> 

$("#select_all").change(function() { 
    var select_all = $(this).val(); 
alert('1'); 
    if(select_all){ 
     $.ajax({ 
      type:'POST', 
      url:'ajaxData.php', 
      data:'select_all='+select_all, 
      success:function(html){ 
       $('#first_name').html(html); 

      } 
     }); 
    }else{ 
     //$('#carrier').html('<option value="">Select State First</option>'); 

    } 

</script> 

ajaxData.php代碼如下所示

if(isset($_POST["select_all"]) && !empty($_POST["select_all"])){ 

$select_sql="SELECT * FROM member WHERE team_id = ".$_POST['select_all']." ORDER BY name ASC"; 
$result=mysqli_query($con,$select_sql); 
$cnt=mysqli_num_rows($result); 
if($cnt > 0) 
{ 
     while($results=mysqli_fetch_assoc($result)) 
      { 

      if($results['id'] !=0) 
       { 
       echo $results['fname']        
      } 
} 
+0

檢查合格/返回值開發者控制檯 –

+0

@u_mulder未捕獲的SyntaxError:意外的輸入結束 – user580950

+0

您的代碼格式非常糟糕..奇怪的括號..缺少大括號..這正是您如何擁有它?修復它,所以我們可以繼續 – CodeGodie

回答

1

你缺少});最後,這就是爲什麼你會得到一個錯誤,如Uncaught SyntaxError: Unexpected end of input

$("#select_all").change(function() { 
    var select_all = $(this).val(); 
alert('1'); 
    if(select_all){ 
     $.ajax({ 
      type:'POST', 
      url:'ajaxData.php', 
      data:'select_all='+select_all, 
      success:function(html){ 
       $('#first_name').html(html); 

      } 
     }); 
    }else{ 
     //$('#carrier').html('<option value="">Select State First</option>'); 

    } 
}); //this is missing in your code 

而且在你的PHP腳本你(正好兩個以上)丟失了一些},並且echo $results['fname']後也失蹤;,所以更改echo $results['fname']echo $results['fname'];試試這個:

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

$select_sql="SELECT * FROM member WHERE team_id = ".$_POST['select_all']." ORDER BY name ASC"; 
$result=mysqli_query($con,$select_sql); 
$cnt=mysqli_num_rows($result); 
if($cnt > 0){ 
     while($results=mysqli_fetch_assoc($result)){ 

      if($results['id'] !=0){ 
       echo $results['fname'];        
      } 
     } 
} 

} 
+1

@lonut非常感謝 – user580950

+0

@ user580950,不客氣。樂於幫助。 – Ionut