2017-04-04 49 views
0

我想用javascript爲MySQL創建編輯數據的表單。當我點擊鏈接編輯時,它會顯示編輯數據的編輯表單。我可以使用PHP和JavaScript創建表單編輯數據嗎?

這是從和顯示數據的PHP代碼。

<form id="form-edit" method="post"> 
<?php 
    $sql = "SELECT * FROM comment WHERE question_id = $question_id ORDER BY id DESC"; 
    $r1 = mysqli_query($link, $sql); 
    while($cm = mysqli_fetch_array($r1)) { 

     $comment_id = $cm['id']; 

     echo '<section class="section-comment">'; 
     echo '<span class="commentator">' .$cm['user_id'] . '</span>'; 
     echo $cm['detail']; 

     // This is link Edit 
     echo '<a href="#" class="edit-comment" edit-id="'.$comment_id.'">Edit</a>'; 

     echo $comment_id; //This can show correct comment_id 

     ?> 

     <div id="form-edit-dialog"> 


    <input type="text" name="user_id" value="<?php echo $user_id ?>" readonly > <br> 

    //I add this line for check comment_id but it show max comment_id to min when I open and close form 
    <input type="text" name="id" value="<?php echo $comment_id ?>" readonly > <br> 

    <textarea name="detail"></textarea><br> 

    <button type="submit" id="submit-edit">Submit</button> 

    <input type="hidden" name="comment_id" id="comment-id"> 

} 
</form> 
</div> 

我寫這樣的代碼java腳本。

$(function() { 
    $('a.edit-comment').click(function(event) { //Click link Edit 
     $('#form-edit')[0].reset(); 

     event.preventDefault(); 

     var t = "Edit Comment"; 

     $('#form-edit-dialog').dialog({ 
      width: '600px', 
      title: t, 
      modal: true, 
      position: { my: "center", at: "center", of: window} 
     }); 

     //set value for hidden 
     $('#comment-id').val($(this).attr('edit-id')); 

    }); 

    $('#submit-edit').click(function() { 

     $('form#form-edit').ajaxForm({ 
      url: 'save-edit.php', 
      type: 'post', 
      dataType: 'script', 
      beforeSend: function() { 
       $.blockUI({message:'<h3>Sending data...</h3>'}); 
      }, 
      complete: function() { 

       $.unblockUI(); 
      } 
     }); 
    }); 
}); 

PHP可以列出所有評論和正確顯示COMMENT_ID但是當我點擊編輯,顯示它COMMENT_ID的最大數目,當我關閉窗體,然後再次單擊編輯。 comment_id將減少comment_id數量直到沒有comment_id。 我可以使用PHP和JavaScript創建表單編輯數據,或者我必須將數據發送到新頁面嗎?

回答

0
  1. 如果用戶有權發表評論意味着他已被記錄,所以基本上你可以從session,cookie獲得id;在這裏不提取用戶標識,或者您可以檢查他是否有權評論(user_id與第一個會話相同)。
  2. 只之間做出像這樣的開關:

<

<div id="showtext<?php echo $comment_id ?>" > 
<span id="comment<?php echo $comment_id ?>"><?php echo $cm['detail'] ?></span> 
<a href="javascript:void(0)" class="edit-comment" id="<?php echo $comment_id ?>">Edit</a> 
<div> 

<!-- this is invisible at start --> 
<div id="showedit<?php echo $comment_id ?>" style="display:none"> 
<textarea id="edittext<?php echo $comment_id ?>"><?php echo $cm['detail'] ?></textarea> 
<a href="javascript:void(0)" class="submit-comment" id="<?php echo $comment_id ?>">Save comment</a> 
</div> 

和JavaScript來切換視圖:

$('.edit-comment').click(function() { 
     var id=$(this).attr("id"); 
$("#showedit"+id).show(); 
$("#showtext"+id).hide(); 
}); 

,並提交

$('.submit-comment').click(function() { 
    var id=$(this).attr("id"); 
    var comment=$("#edittext"+id).val(); 

    //send the data via ajax with parameters id and comment to alter the row, in php add the user id from session, cookie and on success: 

//replace the old text 
    $("#comment"+id).html(comment); 
//and make the switchback 
    $("#showedit"+id).hide(); 
    $("#showtext"+id).show(); 

    }) 
相關問題