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創建表單編輯數據,或者我必須將數據發送到新頁面嗎?