2016-09-19 80 views
1

我試圖根據用戶點擊哪行來顯示模式,數據屬性字段是根據數據庫中現有的條目設置的。然後我需要將該行的數據顯示在模態本身中。通過對數據庫使用data-attr訪問特定行

這裏是我的表,你點擊之前..

<div class="table-responsive"> 
       <table class="table table-bordered"> 

       <?php 
       $list = mysqli_query($con,"SELECT * FROM notes WHERE memberid = '$membersid'"); 

       $rows = mysqli_num_rows($list); 

       if($rows == 0) 
       { 
        ?><th id="no-notes">No Existing Notes</th><?php 
       } 
       else 
       {?> 
        <tr> 
        <th id="note-h-date">Date</th> 
        <th id="note-h-excerpt">Excerpt</th> 
        <th id="note-h-addedby">Added By</th> 
        </tr> 
        <?php 
        if($list) 
        { 
         while($listnotes = mysqli_fetch_array($list)) 
         { 
          $dateadded = new DateTime($listnotes['dateadded']); 

          ?><tr class="clickable table-sel" data-toggle="modal" data-id="<?php echo $listnotes['id'];?>" data-target="#noteModal"><?php 
          echo "<td class='note-dateadded'>".$dateadded->format('d-m-Y')."</td>"; 
          echo "<td class='note-excerpt'>".substr($listnotes['note'],0,95)."...</td>"; 
          echo "<td class='note-addedby'>".$listnotes['addedby']."</td>"; 
          echo "</tr>"; 
         } 
        } 
       } 


       ?> 
       </table> 
      </div> 

這裏是模態代碼。

<!-- Modal --> 
      <div class="modal fade" id="noteModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"> 
       <div class="modal-dialog" role="document"> 
       <div class="modal-content"> 
        <div class="modal-header"> 
        <?php 
        $noteid = $_SESSION['noteid']; 

        $check = mysqli_query($con,"SELECT * FROM notes WHERE id = '$noteid'"); 
        if($check){ 

         $chkres = mysqli_fetch_array($check); 
         $dateadded = new DateTime($chkres['dateadded']); 
        } 
        else 
        { 
         echo "Failed to load note"; 
        } 
        ?> 
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button> 
        <h4 class="modal-title" id="myModalLabel">Note dated: <?php echo $dateadded->format('d-m-Y');?></h4> 
        </div> 
        <div class="modal-body"> 
        <textarea class="form-control" rows="5" id="fullnote"><?php echo $chkres['note'];?></textarea> 
        </div> 
        <div class="modal-footer"> 
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> 
        <button type="button" class="btn btn-primary">Save changes</button> 
        </div> 
       </div> 
       </div> 
      </div> 

JQuery as orginally試圖將其設置爲會話變量,然後使用它從數據庫訪問正確的數據。

jQuery(document).ready(function($) { 
    $(".clickable").click(function() { 
     var value = $(this).attr('data-id'); 

     $.post("modal-post.php",{"nid": value}); 
     alert(value); 
    }); 
}); 

任何幫助將是偉大的。解釋可能不是很好,我很抱歉。

+2

你現在面臨什麼問題?這是警報(價值)'不出現? – vijayP

+0

對不起,我使用警報來確認當用戶點擊特定的表格行時,設置了正確的data-attr值。然後我需要在模態窗口中使用該值並訪問正確的音符內容 –

+0

這個'noteModal'是作爲'modal-post.php'的響應而回來的嗎?或者它已經存在於你的頁面中? – vijayP

回答

0

好了,所以我設法解決我的問題與你們的反饋和鑽研得更深一些。

所以這裏是排序的代碼。

<!-- Modal --> 
<div class="modal fade" id="noteModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"> 
    <div class="modal-dialog modal-lg" role="document"> 
     <div class="modal-content"> 
      <div class="modal-header"> 
      <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button> 
      <h4 class="modal-title" id="myModalLabel"></h4> 
      </div> 
      <div class="modal-body"> 
       <textarea class="form-control" rows="15" id="fullnote"></textarea> 
      </div> 
      <div class="modal-footer"> 
       <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> 
       <button type="button" class="btn btn-primary">Save changes</button> 
      </div> 
     </div> 
    </div> 
</div> 

的JQuery - > Ajax代碼

jQuery(document).ready(function($) { 
$(".clickable").click(function() { 
    var value = $(this).attr('data-id'); 
    $.ajax({ 
       type: "POST", 
       url: "modal-post.php", //URL here 
       data: { param: value }, 
       dataType: 'json', 
       }).done(function(data) { 
        $('#myModalLabel').html("Date Added: " + data.notedate); 
        $('#fullnote').html(data.note); 
      }); 
    }); 
}); 

於是最後的PHP腳本。

$nid = $_POST['param']; 

include("connection.php"); 
$check = mysqli_query($con,"SELECT * FROM notes WHERE id = '$nid'"); 
if($check){ 
    $chkres = mysqli_fetch_array($check); 
    $dateadded = new DateTime($chkres['dateadded']); 
    echo json_encode(
     array(
      "notedate"=>$dateadded->format('d-m-y'), 
      "note"=>$chkres['note'] 
     ) 
    ); 
} 
else 
{ 
    echo "Failed to load note"; 
} 

謝謝。

0

您有錯誤。在模式頁代替,:

$noteid = $_SESSION['noteid']; 

你應該有

$noteid = $_POST['nid'];