2017-06-26 160 views
0

我有一個jquery模式按鈕,我想從我的數據庫顯示信息。Ajax調用返回相同的結果

我有我想要的一切被它識別被稱爲「數字」 的模式按鈕顯示在其已經被從一個SQL查詢中顯示的所有條目

while ($row = getarray($res)){ 

    <tbody> 
    <tr> 
    <td> <? $row["state"] ?> </td> 
    <td><? $row["number"] ?></td> 
    <td> 
    <button type="button" class="btn btn-primary" onclick="detailsmodal(<? 
    $row["number"])">Description</button> 
    </td> 
    </tr> 


<!-- Modal Button --> 
<button type ="button" class="btn btn-primary" onclick="detailsmodal(<?= $row['number']; ?>)>Modal Button</button> 
<!-- Code for modal --> 
<?php 
$number =$_POST["number"]; 
$number =(int)$number; 
$modalsql = myquery($dbvariable,"SELECT * FROM myTable WHERE Number = '$number' "); 
$result2 = myfunction_fetch_array($modalsql); 
ob_start(); 

?> 

<div class="modal fade" id="myModal" data-keyboard="false" data-backdrop="static" role="dialog"> 
<div class="modal-dialog"> 
<!-- Modal content--> 
<div class="modal-content"> 
    <div class="modal-header"> 
    <button type="button" class="close" onclick="closeModal()" aria-label="Close"><span aria-hidden="true">&times;</span></button> 
    <h4 class="modal-title"> <?= $result2['number']?></h4> 
    </div> 
    <div class="modal-body"> 
    <p><strong>Field 1:</strong><?= $result2['number']?> </p> 
    <p><strong>Field 2:</strong><?= $result2['field_2']?> </p> 
    <p><strong>Field 3</strong><?= $result2['field_3']?> </p> 
    <p><strong>Field 4:</strong><?= $result2['field_4']?> </p> 
    <p><strong>Field 5:</strong><?= $result2['field_5']?></p> 
    <p><strong>Field 6:</strong><?= $result2['field_6']?> </p> 
    </div> 
    <div class="modal-footer"> 
    <button type="button" class="btn btn-default" onclick="closeModal()">Close</button> 
    </div> 
    </div> 
    </div> 
    </div> 

<?php 
ob_get_clean(); 
?> 

<!-- Script for the modal--> 
<script> 
    function detailsmodal(number){ 

     var data = {"number" : number}; 
     jQuery.ajax({ 
     url : "/page1/page2", 
     method : "post", 
     data : data, 
     success: function(){ 
      jQuery("body").append(data); 
      jQuery("#myModal").modal("toggle"); 
     }, 
     error: function(){ 
      alert("Something went wrong!"); 
     } 
     }); 
    } 
</script> 
循環表數據庫中的字段

理想情況下,信息應根據被點擊的不同項目進行更改。但它保持不變。

+0

ajax是我提交的代碼的最後一部分。無論我點擊哪個按鈕,我都會從db中獲取第一個條目 – jumpman8947

+0

當您進行調試時,特別是在何處發生問題?這裏生成的客戶端代碼是什麼?在檢查網絡請求時,在瀏覽器的調試工具中,AJAX請求是否包含您期望的數據?當在這些相同的工具中調試JavaScript時,'number'的值是多少?它是你期望的值嗎?什麼是由AJAX請求調用的PHP代碼?什麼是回報作爲迴應?你可以在這裏做很多調試。 – David

+1

問題出在您發佈的代碼的第一行。你正在傳遞一個以'$ row ['number'];'爲函數'detailsmodal''計算的「數字」。你很高興地傳遞相同的數字,這樣你總能得到相同的結果。 – gforce301

回答

0

請嘗試使用以下代碼,您的代碼中存在如此多的拼寫錯誤。

<?php while ($row = getarray($res)){ ?> 

     <tbody> 
      <tr> 
      <td> <? $row["state"] ?> </td> 
      <td><? $row["number"] ?></td> 
      <td> 
       <button type="button" class="btn btn-primary" onclick="detailsmodal(<?= 
       $row["number"]) ?>">Description</button> 
      </td> 
      </tr> 


      <!-- Modal Button --> 
      <button type ="button" class="btn btn-primary" onclick="detailsmodal(<?= $row['number']; ?>)">Modal Button</button> 
      <!-- Code for modal --> 
      <?php 
      $number =$_POST["number"]; 
      $number =(int)$number; 
      $modalsql = myquery($dbvariable,"SELECT * FROM myTable WHERE Number = '".$number."' "); 
      $result2 = myfunction_fetch_array($modalsql); 
      ob_start(); 

      ?> 

      <div class="modal fade" id="myModal" data-keyboard="false" data-backdrop="static" role="dialog"> 
      <div class="modal-dialog"> 
       <!-- Modal content--> 
       <div class="modal-content"> 
       <div class="modal-header"> 
        <button type="button" class="close" onclick="closeModal()" aria-label="Close"><span aria-hidden="true">&times;</span></button> 
        <h4 class="modal-title"> <?= $result2['number']?></h4> 
       </div> 
       <div class="modal-body"> 
        <p><strong>Field 1:</strong><?= $result2['number']?> </p> 
        <p><strong>Field 2:</strong><?= $result2['field_2']?> </p> 
        <p><strong>Field 3</strong><?= $result2['field_3']?> </p> 
        <p><strong>Field 4:</strong><?= $result2['field_4']?> </p> 
        <p><strong>Field 5:</strong><?= $result2['field_5']?></p> 
        <p><strong>Field 6:</strong><?= $result2['field_6']?> </p> 
       </div> 
       <div class="modal-footer"> 
       <button type="button" class="btn btn-default" onclick="closeModal()">Close</button> 
       </div> 
      </div> 
      </div> 
     </div> 

     <?php 
     ob_get_clean(); 
     ?> 

     <!-- Script for the modal--> 
     <script> 
      function detailsmodal(number){ 

      var data = {"number" : number}; 
      jQuery.ajax({ 
       url : "/page1/page2", 
       method : "post", 
       data : data, 
       success: function(){ 
       jQuery("body").append(data); 
       jQuery("#myModal").modal("toggle"); 
       }, 
       error: function(){ 
       alert("Something went wrong!"); 
       } 
      }); 
      } 
     </script>