[英]AJAX fade on submit
我對此淡出div有一個簡單的問題。 我使用facebox,並且在提交之后,它顯示了一個錯誤,它將顯示以下內容:
但是,我希望它們在收到該消息后消失。 在提交表單的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.