2017-06-17 50 views
1

我仍然是新的網絡編程。當我選擇單選按鈕然後提交該值時,我使用ajax和jQuery獲取mysql數據時遇到問題。對於我正在使用PDO的查詢。這裏是我的代碼: 1.選擇單選按鈕值阿賈克斯不顯示數據庫表時,單選按鈕選擇

<form method="get" name="urut"> 
 
       <input type="radio" name="urut" value="ASC" id="urut"/>ASC 
 
       <input type="radio" name="urut" value="DESC" id="urut"/>DESC 
 
       <input type="submit" value="Filter" id="masukan"/> 
 
</form>

  • jQuery代碼來從單選按鈕ASC或DESC值,當點擊輸入提交
  • <script type="text/javascript"> 
     
        $(document).ready(function(){ 
     
         $('#masukan').click(function(){ 
     
          $('#table_div').text(""); 
     
          $('#info').text(""); 
     
           $.ajax({ 
     
            url : "php/by_name.php", 
     
            type : "get", 
     
            data : {"urut": $("#urut").val()}, 
     
            success : function(data){ 
     
             $('#info').html('<b>Daftar Siswa Berdasarkan Nama</b>'); 
     
             $('#table_div').html(data); 
     
            }, 
     
            error : function(xhr, teksStatus, kesalahan){ 
     
             $('#info').html('<b>Terjadi Kesalahan</b>'); 
     
            } 
     
           }); 
     
         }); 
     
        }); 
     
        </script>

  • 該頁面給由用戶選擇數據時提交的值是DESC或HTML表ASC
  • <!DOCTYPE html> 
     
    <html> 
     
    <head> 
     
    </head> 
     
    <body> 
     
        <form method="get"> 
     
        <table> 
     
        <?php 
     
    
     
    
     
        # code... 
     
        if(isset($_GET['urut'])){ 
     
         $urut = trim($_GET['urut']); 
     
         ?> 
     
          <thead> 
     
            <th>N I S</th> 
     
            <th>N I S N</th> 
     
            <th>NAMA SISWA</th> 
     
            <th>TEMPAT LAHIR</th> 
     
            <th>TANGGAL LAHIR</th> 
     
            <th>AKSI</th> 
     
          </thead> 
     
          <tbody> 
     
          <?php 
     
         //  echo $urut; 
     
           include "db_connect.php"; 
     
           if($urut == "ASC"){ 
     
            try{ 
     
             $kueri = $dt_bas->prepare("SELECT nis, nisn, nm_dpn, tmp_lhr, dob_siswa FROM siswa ORDER BY nm_dpn ASC"); 
     
          //    $kueri->bindParam(":pilih", $urut); 
     
             $kueri->execute(); 
     
            }catch(PDOException $e){ 
     
    
     
            } 
     
            while ($row = $kueri->fetch(PDO::FETCH_ASSOC)) { 
     
    
     
    
     
    
     
    
     
    
     
    
     
           ?> 
     
        <!---      Tabel Row Start ASC------------------> 
     
           <tr> 
     
            <td><?php echo $row["nis"];?></td> 
     
            <td><?php echo $row["nisn"];?></td> 
     
            <td><?php echo $row["nm_dpn"];?></td> 
     
            <td><?php echo $row["tmp_lhr"];?></td> 
     
            <td><?php echo $row["dob_siswa"];?></td> 
     
    
     
            <td> 
     
            <a href="#">Ubah<a/> 
     
            <a href="#">Detail</a> 
     
            </td> 
     
           </tr> 
     
    
     
           <?php 
     
          } // end of while where $urut == ASC 
     
           }elseif($urut == "DESC"){ 
     
    
     
           try{ 
     
             $kueri = $dt_bas->prepare("SELECT nis, nisn, nm_dpn, tmp_lhr, dob_siswa FROM siswa ORDER BY nm_dpn DESC"); 
     
         //    $kueri->bindParam(":pilih", $urut); 
     
             $kueri->execute(); 
     
           }catch(PDOException $e){ 
     
    
     
           } 
     
            while ($row = $kueri->fetch(PDO::FETCH_ASSOC)) { 
     
    
     
                ?> 
     
             <!---      Tabel Row Start ASC------------------> 
     
                <tr> 
     
                 <td><?php echo $row["nis"];?></td> 
     
                 <td><?php echo $row["nisn"];?></td> 
     
                 <td><?php echo $row["nm_dpn"];?></td> 
     
                 <td><?php echo $row["tmp_lhr"];?></td> 
     
                 <td><?php echo $row["dob_siswa"];?></td> 
     
    
     
                 <td> 
     
                 <a href="#">Ubah<a/> 
     
                 <a href="#">Detail</a> 
     
                 </td> 
     
                </tr> 
     
    
     
    
     
          <?php 
     
          } // end of while where $urut == DESC 
     
         } // end of child if 
     
         } // end of parent if 
     
           ?> 
     
          </tbody> 
     
        </table> 
     
    </form> 
     
    </body> 
     
    </html>

    當,我刷新我的網絡瀏覽器上的頁面,我收到錯誤消息,但它只爲一個值,我輸入一次,它只是暫時的,但當我嘗試了2次或更多次,我甚至沒有得到任何錯誤消息。

    這裏我的控制檯錯誤 enter image description here

    我得到一些奇怪也在這個錯誤,我在我的網頁瀏覽器的網址提交DESC我可以看到DESC,但在我的控制檯錯誤拿給我ASC即使我嘗試多次提交DESC。這裏的圖片 enter image description here

    感謝您的時間,每個人。

    +0

    感謝的現在的工作 –

    回答

    0

    您對無線電輸入使用相同的ID,這就是爲什麼你沒有得到正確的價值。更改一個單選按鈕ID並使用jQuery獲取選定的輸入按鈕值,然後使用。

    +0

    謝謝,我已經改變了它和它的工作原理(Y) –

    0

    使用單獨的id爲單選按鈕。那會做你的需要..