doulang1945 2011-01-01 22:28
浏览 31
已采纳

注释显示在数据库中,但仅在刷新后显示在我的索引页面上

I have AJAX, PHP, jquery, and mySQL in this very simple website I'm trying to make. All there is is a text area that sends data to the database and uses ajax\jquery to display that data onto the index page. For some reason though, I press submit and the data goes to the database, but I have to refresh the page myself to see that data on the page. I'm assuming that the problem has to do with my AJAX JQuery or even some mistake in the index. Also, when I type the text into the text area and press submit, the text remains in the textarea until I refresh the page. Haha, sorry if this is such a noob question.. I'm trying to learn. Thanks so much

Here is the 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>

Here is the index\form area:

<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
}
?>

Here is my 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>

I'm sorry for so much code but I just started learning this four days ago and this is probably one of the last bugs until the website is functional.

  • 写回答

1条回答 默认 最新

  • douju9272 2011-01-04 01:48
    关注

    what is this? (copied from your code)

    <id="update" class="timeline">
    

    and

    <class="box">
    

    you have tags here that don't exist in html, and I believe it is the root of your problem.

    when you say

     $("ol#update")
    

    in your JQuery, you are trying to find an ordered list tag with an id of update, which would look like this...

    <ol id="update"></ol>
    

    but there is no tag that looks like this in your page. perhaps you need to replace <id="update" class="timeline"> with <ol id="update" class="timeline"></ol>. if this is what you are looking for, please select the check mark next to this answer. thanks!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测