我想用javascript创建从MySQL编辑数据的表单。当我点击链接编辑时,它将显示编辑数据的编辑表单。
这是来自显示数据的php代码。
ColorSpacePoint
我像这样编写代码java脚本。
<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>
PHP可以列出所有评论并正确显示comment_id,但是当我点击“编辑”时,它会显示comment_id的最大数量,当我关闭表单时再次单击“编辑”。 comment_id将减少comment_id数字,直到没有comment_id。 我可以使用php和javascript创建表单编辑数据,还是我必须将数据发送到新页面?
答案 0 :(得分:0)
&LT;
<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();
})