2013-08-27 38 views
-1

我正在嘗試爲我的網站創建註釋部分,並且需要使其在提交註釋(並且包含錯​​誤)後纔會顯示錯誤(一個新的HTML元素)而不刷新頁面。我對AJAX/JQuery幾乎一無所知,所以我需要一些幫助。在沒有頁面刷新的情況下在新的HTML元素中顯示錯誤

這是我到目前爲止有:

<?php 
    if(isset($_POST['reply_submit'])) { 
    $reply = $_POST['new_reply']; 
    $reply_message = ""; 

    if(empty($reply)) { 
     $reply_message = "Your comment is too short."; 
    } 
    } 
?> 

<html lang="en"> 
    <body> 
    <form class="no-margin" method="POST" action=""> 
     <textarea name="new_reply" placeholder="Write a reply..."></textarea> 
     <button name="reply_submit" class="btn post-button" type="submit">Post' . (isset($reply_message) ? '<div class="comment-warning">' . $reply_message . '</div>' : '') . '</button> 
    </form> 
    </body> 
</html> 

所以我需要做的是,如果人的評論框不符合標準(在這種情況下,空場),我需要它顯示該錯誤線而無需刷新頁面:

<button name="reply_submit" class="btn post-button" type="submit">Post' . (isset($reply_message) ? '<div class="comment-warning">' . $reply_message . '</div>' : '') . '</button> 

請幫助。

+2

你應該使用JS這個,然後做一個服務器端檢查也是一樣的通道eck,讓js檢查,ajax發佈,php檢查,js錯誤返回。 – cmorrissey

+0

請不要多次發表相同的問題。 – Paul

+0

繼續這樣做會導致你被阻止提問。 –

回答

0

HTML大致相同,將onsubmit行動

<form class="no-margin" method="POST" action="" onsubmit=verify();> 
     <textarea name="new_reply" id="new_reply_textarea" placeholder="Write a reply..."></textarea> 
... 
</form> 
<div class=comment-warning></div> 

JS

function verify() 
{ 
if ($('#new_reply_textarea').is(':empty')) 
{ 
$('.comment-warning').html("Your comment is too short."); 
return false; 
} 
else 
{ 
return true; 
} 
} 
相關問題