繁体   English   中英

AJAX在提交时淡出

[英]AJAX fade on submit

我对此淡出div有一个简单的问题。 我使用facebox,并且在提交之后,它显示了一个错误,它将显示以下内容:

http://puu.sh/1uPnF

但是,我希望它们在收到该消息后消失。 在提交表单的AJAX处理程序上,这是PHP代码:

<?php
require_once('....');

$form_name = $_GET['form_name'];
$form_comment = $_GET['form_comment'];
$date = date('Y-n-j');
$ip = $_SERVER['REMOTE_ADDR'];


 if($form_name == '') {
    echo("<div class='alert alert-error-x'>Don't forget to enter a name, as we need to identify who's commenting on this article!</div>");

} else if($form_comment == '') {
    echo("<div class='alert alert-error-x'>Please do not leave the comment field blank, we want to know what you're saying!</div>");

} else {
mysql_query("INSERT INTO comment (id, articleid, name, comment, date, ip) VALUES (NULL,'{$_GET['id']}','{$form_name}','{$form_comment}','{$date}','{$ip}')");

    // output comment
    echo "<div class='alert alert-success-x'>Posted by <strong>$form_name</strong> on     <strong>{$date}</strong>$form_comment</div>";
}

?>

这是将在article.php上提交的内容的输出:

 <?php

$amount_get = mysql_query("SELECT * FROM comment WHERE articleid='" . mysql_real_escape_string($_GET['id']) . "'"); $comments = mysql_num_rows($amount_get);

$grab = mysql_query("SELECT * FROM comment WHERE articleid='" . mysql_real_escape_string($_GET['id']) . "'");

if (mysql_num_rows($grab)==0) {

    echo "<div class='alert alert-note-x'>Sorry, it looks like their are no comments to be displayed, check back later!</div>";
}

    while($row = mysql_fetch_array($grab)){

    ?>

 <div id="new_comment"></div>
 <div class="article-comment">
 Posted by <b><?php echo $row['name'] ?></b> on <b><?php echo $row['date'] ?></b>  
 <br />  
 <?php echo $row['comment'] ?>
 </div>
   <?php } ?>         
</div>
</body>
</html>

现在,我尝试将以下代码添加到core.js文件中,该文件位于我的网站krissales.com上

$(window).bind("load", function() { 
$('#new_comment').fadeOut(4000);
});

但是它没有用。 如果要测试该演示,请在以下位置进行查看: http : //www.krissales.com/#/media/30.This-is-a-new-article-for-Testing

请问我做错了什么事?

谢谢!

如果您使用的是AJAX,建议您使用jquery的ajax方法( http://api.jquery.com/jQuery.ajax/ )来调用PHP文件。

然后,使用该方法的success回调函数来显示正确的响应。 然后可以通过淡出div来隐藏响应(如果需要,可以延迟一下)。

if ($('#new_comment').length > 0)
{
    $('#new_comment').delay(4000).fadeOut();
}

现在,您正在尝试淡出窗口加载时的消息,但是由于您使用的是AJAX,因此没有页面重新加载发生。

希望这对您有所帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM