覆盖(刷新)echo 语句,而不是将其与旧语句一起添加


Override (refresh) echo statement instead of adding it along with an old one

我创建了一个输入,允许用户更新有关他/她的信息。输入显示紧随其后的输入信息(我使用 AJAX)。我保证一切正常,问题是当用户决定更改个人信息,输入一些数据并点击"保存"时 - 它不会覆盖刷新之前显示的数据,而是将其放在其下方

我的观点是允许用户立即更改有关他/她的信息并立即显示它,而不是将新记录与旧记录一起显示。

这是我的索引.php

<head>   
<script type="text/javascript">
$(function() {
$(".submit_button").click(function() {
var textcontent = $("#content").val();
var dataString = 'content='+ textcontent;
if(textcontent=='')
{
alert("Enter some text..");
$("#content").focus();
}
else
{
$("#flash").show();
$("#flash").fadeIn(400).html('<span class="load">Loading..</span>');
$.ajax({
type: "POST",
url: "action.php",
data: dataString,
cache: true,
success: function(html){
$("#show").after(html);
document.getElementById('content').value='';
$("#flash").hide();
$("#content").focus();
}  
});
}
return false;
});
});
</script>
</head>
<body>

<div class="container">
<div class="main">
<form  method="post" name="form" action="">
<textarea style="width:500px; font-size:14px; height:60px; font-weight:bold;   resize:none;" name="content" id="content" ></textarea><br />
<input type="submit" id="submit"value="Post" name="submit" class="submit_button"/>
</form>
</div>
<div class="space"></div>
<div id="flash" align="left"  ></div>
<div id="show" align="left"></div>
</div>
</body>
</html>

行动.php

<?php
include('db.php');
$check = mysqli_query($conn,"SELECT * FROM user order by age desc");
if(isset($_POST['content']))
{
$content=$_POST['content'];
$ip=$_SERVER['REMOTE_ADDR'];

$query = "UPDATE user SET rain = '$content' WHERE name = 'gala'";
mysqli_query($conn,$query) or die("error querying record");
$fetch= mysqli_query($conn,"SELECT rain FROM user WHERE name = 'gala' LIMIT    1");
$result = mysqli_fetch_assoc($fetch);
}
?>

这就是我的输入所在 - 这就是问题所在。它添加了新记录,而我希望它得到刷新!

<div class="showbox"> <?php echo $result['rain'];  ?> </div>

提前感谢!

在 ajax 成功回调中,使用 jQuerys .after()函数。之后将给定的 html(您的div 类"showbox")附加到 id 为 "box" 的div 中。因此,每次点击保存时,另一个div.showbox都会添加到DOM中。

改用 jQuerys .html()替换已经存在的内容

取代

$("#show").after(html);

$("#show").html(html);