2017-04-04 8 views
0

JavaScriptから編集データ用のフォームを作成したいです。リンク編集をクリックすると、編集データの編集フォームが表示されます。phpおよびjavascriptフォーム編集データを作成できますか?

これは、出品者の出品者です。

<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. ユーザーがコメントする権利がある場合は、彼がログに記録されていることを意味します。したがって、基本的にセッションのCookieからIDを取得できます。ここでユーザーIDを抽出することはできません。また、ユーザーがコメントする権利があるかどうかを確認することもできます(user_idはセッション1と同じです)。
  2. はちょうど間と、次のようにスイッチを作る:

    $('.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(); 
    
        }) 
    
    を提出する:

<

<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がビューを切り替えるには

関連する問題