覆盖(刷新)echo语句,而不是将其与旧的

时间:2016-03-19 17:27:46

标签: php jquery ajax forms

我创建了一个输入,允许用户更新有关他/她的信息。输入显示超出它的输入信息(我使用AJAX)。我把所有工作都做了,问题是当用户决定更改个人信息时,输入一些数据并点击“保存”。 - 它不会覆盖刷新显示的先前数据,但将放在它下面

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

这是我的index.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>

action.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>

提前致谢!

1 个答案:

答案 0 :(得分:0)

在你的ajax成功回调中你使用jQuerys .after()函数。将给定的html(您的div类&#34; showbox&#34;)附加到ID为#&#34; box&#34;的div后。因此,每次点击保存时,另一个div.showbox将被添加到DOM。

使用jQuerys .html()代替已存在的内容

替换

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

$("#show").html(html);
相关问题