gpt4 book ai didi

javascript - 用户输入文本后,如何使用 AJAX 更新数据库?

转载 作者:行者123 更新时间:2023-12-01 04:33:51 25 4
gpt4 key购买 nike

我创建了一个表,其中有一列用于注释,注释来自 MySQL 表并显示。当用户单击注释时,它将转换为输入框并允许他们编辑文本。然后,我如何使用已输入的新数据更新我的表格,以便当用户单击刚刚输入文本的输入框时,它将使用正确的行 ID 更新注释列

这是我目前的代码...

Javascript

    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0-beta1/jquery.min.js"></script>
<script>
$( document ).ready(function() {
$(function(){
$("#loading").hide();
//acknowledgement message
var message_status = $("#status");
$("td[contenteditable=true]").blur(function(){
var field_id = $(this).attr("id") ;
var value = $(this).text() ;
$.post('scripts/update-jobdiary-notes.php' , field_id + "=" + value, function(data){ //change this to your liveupdatingpage.php
if(data != '')
{
message_status.show();
message_status.text(data);
//hide the message
setTimeout(function(){message_status.hide()},1000);
}
});
});
});
});
</script>

HTML

<td data-editable contenteditable="true" id="note_text_db_column:<?php $note['notes_id']; ?>"><?= $note['notes_text']; ?></td>

PHP

//DATABASE CONNECTION
if(!empty($_POST))
{
//database settings

foreach($_POST as $field_name => $val)
{
//clean post values
$field_id = strip_tags(trim($field_name));

//from the fieldname:user_id we need to get user_id
$split_data = explode(':', $field_id);
$data_id = $split_data[1];
$field_name = $split_data[0];
if(!empty($data_id) && !empty($field_name) && !empty($val))
{
$stmt = $pdo->prepare("UPDATE ser_diarynotes SET notes_text = ? WHERE notes_id = ?"); //here just change name of your table and name of your unique_column
$stmt->bind_param("si", $val, $data_id);
$stmt->execute();
$stmt->close();

echo "Updated";
} else {
echo "Invalid Requests";
}
}
}
?>

最佳答案

让我使用替代 js 和更新的 html 进一步修改您的代码以捕获:

  1. unique ID of your content. assuming each record has to have a unique id column for easy update
  2. the content being edited

这是一个 HTML 示例

<style>
#status { padding:10px; position:fixed; top:10px; border-radius:5px; z-index:10000000; background:#88C4FF; color:#000; font-weight:bold; font-size:12px; margin-bottom:10px; display:none; width:100%; }
#loading { padding:10px; position:absolute; top:10px; border-radius:5px; z-index:10000000; background:#F99; color:#000; font-weight:bold; font-size:12px; margin-bottom:10px; display:none; width:100%; }
</style>

<div id="status"> </div>
<div id="loading"></div>

<table id="tableID">
<tbody>
<tr>
<!--Edit below by specifying notes_text column and notes_unique_id-->
<td data-editable contenteditable="true" id="note_text_db_column:<?php echo $note['notes_unique_id']; ?>"><?php echo $note['notes_text']; ?></td>
</tr>
</tbody>
</table>

这是将两个变量传递到 PHP 页面以在数据库上更新的 JavaScript

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0-beta1/jquery.min.js"></script>
<script>
$( document ).ready(function() {
$(function(){
$("#loading").hide();
//acknowledgement message
var message_status = $("#status");
$("td[contenteditable=true]").blur(function(){
var field_id = $(this).attr("id") ;
var value = $(this).text() ;
$.post('liveupdatingpage.php' , field_id + "=" + value, function(data){ //change this to your liveupdatingpage.php
if(data != '')
{
message_status.show();
message_status.text(data);
//hide the message
setTimeout(function(){message_status.hide()},1000);
}
});
});
});
});
</script>

现在您的 PHP 示例页面将更新您的数据

<?php  
//Your connection
$conn = new mysqli( 'localhost', 'db_user', 'user_password', 'your_db');

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $con->connect_error);
}

if(!empty($_POST))
{
//database settings

foreach($_POST as $field_name => $val)
{
//clean post values
$field_id = strip_tags(trim($field_name));

//from the fieldname:user_id we need to get user_id
$split_data = explode(':', $field_id);
$data_id = $split_data[1];
$field_name = $split_data[0];
if(!empty($data_id) && !empty($field_name) && !empty($val))
{
$stmt = $conn->prepare("UPDATE your_table SET $field_name = ? WHERE column_id = ?"); //here just change name of your table and name of your unique_column
$stmt->bind_param("si", $val, $data_id);
$stmt->execute();
$stmt->close();

echo "Updated";
} else {
echo "Invalid Requests";
}
}
}

else {
echo "Invalid Requests";
}
?>

关于javascript - 用户输入文本后,如何使用 AJAX 更新数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60279471/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com