我有AJAX,PHP,jquery和mySQL在这个非常简单的网站,我试图使。这里只有一个文本区,它将数据发送到数据库,并使用ajax\jquery将数据显示到索引页面上。但是,由于某些原因,我按下submit,数据进入数据库,但我必须自己刷新页面才能看到页面上的数据。我假设这个问题与我的AJAX JQuery有关,甚至与索引中的某些错误有关。此外,当我在文本区域中键入文本并按submit时,文本将一直保留在文本区域中,直到我刷新页面为止。哈哈,如果这是个新手问题,很抱歉..我在努力学习。非常感谢
下面是AJAX:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
$(".submit").click(function()
{
var comment = $("#comment").val();
var post_id = $("#post").val();
var dataString = '&comment=' + comment
if(comment=='')
{
alert('Fill something in please!');
}
else
{
$("#flash").show();
$("#flash").fadeIn(400).html('<img src="noworries.jpg" /> ');
$.ajax({
type: "POST",
url: "commentajax.php",
data: dataString,
cache: false,
success: function(html){
$("ol#update").append(html);
$("ol#update li:last").fadeIn("slow");
$("#flash").hide();
}
});
}return false;
}); });
</script>
以下是索引\表单区域:
<body>
<div id="container"><img src="banner.jpg" width="890" height="150" alt="title" /></div>
<id="update" class="timeline">
<div id="flash"></div>
<div id="container">
<form action="#" method="post">
<textarea name="comment" id="comment" cols="35" rows="4"></textarea><br />
<input name="submit" type="submit" class="submit" id="submit" value=" Submit Comment " /><br />
</form>
</div>
<id="update" class="timeline">
<?php
include('config.php');
//$post_id value comes from the POSTS table
$prefix="I'm happy when";
$sql=mysql_query("select * from comments order by com_id desc");
while($row=mysql_fetch_array($sql))
{
$comment=$row['com_dis'];
?>
<!--Displaying comments-->
<div id="container">
<class="box">
<?php echo "$prefix $comment"; ?>
</div>
<?php
}
?>
这是我的commentajax.php
<?php
include('config.php');
if($_POST)
{
$comment=$_POST['comment'];
$comment=mysql_real_escape_string($comment);
mysql_query("INSERT INTO comments(com_id,com_dis) VALUES ('NULL', '$comment')");
}
?>
<li class="box"><br />
<?php echo $comment; ?>
</li>
我很抱歉有这么多代码,但我四天前才开始学习,这可能是网站正常运行前的最后一个bug。
发布于 2011-01-04 01:48:25
这是什么?(从您的代码中复制)
<id="update" class="timeline">
和
<class="box">
这里有html中不存在的标签,我相信这就是问题的根源。
当你说
$("ol#update")
在您的JQuery中,您正在尝试查找一个id为update的有序列表标记,如下所示…
<ol id="update"></ol>
但是在你的页面中没有这样的标签。也许您需要用<ol id="update" class="timeline"></ol>
替换<id="update" class="timeline">
。如果这就是您要查找的内容,请选中此答案旁边的复选标记。谢谢!
https://stackoverflow.com/questions/4576112
复制相似问题