2011-11-30 72 views
1

我剛剛幫助了一些功能和回調,以獲得表單一旦提交關於該動畫:不同的功能形式成功和形式驗證消息

  $("#message").show().delay(5000).fadeOut('fast', function(){ 
      $("#slide_panel").slideToggle("slow"); 
}); 

雖然,這個問題我現在已經是,如果有人必須提交表單而不輸入正確的詳細信息,則還會彈出錯誤消息(與感謝消息彈出相同的div「消息」),延遲5秒鐘,然後關閉表單。

當然,我不希望它關閉窗體,而是顯示錯誤消息5秒鐘,然後淡出錯誤消息。

什麼我需要在這裏添加:

function(data){ 
      document.getElementById('message').innerHTML = data; 
      $('#message').slideDown('slow'); 
      $('#contactform img.loader').fadeOut('fast',function() 
{$(this).remove()}); 
      $('#submit').removeAttr('disabled'); 
      if(data.match('success') != null); 
      $('#name').val(""); 
      $('#email').val(""); 
      $('#phone').val(""); 
      $('#dayin').val(""); 
      $('#dayout').val(""); 
      $('#comments').val(""); 
      $('#verify').val(""); 
      $("#message").show().delay(5000).fadeOut('fast', 
function(){ 
      $("#slide_panel").slideToggle("slow"); 
}); 

     } 
    ); 

    }); 

    return false; 

}); 

}); 

我假設我需要做類似這樣的東西代碼:

if(data.match('success') != null); 

在我contact.php形式....我有這個:

if (isset($_POST['verify'])) : 
    $posted_verify = $_POST['verify']; 
    $posted_verify = md5($posted_verify); 
else : 
    $posted_verify = ''; 
endif; 

// Important Variables 
$session_verify = $_SESSION['verify']; 

if (empty($session_verify)) $session_verify = $_COOKIE['verify']; 

$error = ''; 

    if(trim($name) == '') { 
     $error .= '<li>Your name is required.</li>'; 
    } 

    if(trim($email) == '') { 
     $error .= '<li>Your e-mail address is required.</li>'; 
    } elseif(!isEmail($email)) { 
     $error .= '<li>You have entered an invalid e-mail address.</li>'; 
    } 

    if(trim($phone) == '') { 
     $error .= '<li>Your phone number is required.</li>'; 
    } elseif(!is_numeric($phone)) { 
     $error .= '<li>Your phone number can only contain digits.</li>'; 
    } 

    if(trim($comments) == '') { 
     $error .= '<li>You must enter a message to send.</li>'; 
    } 

    if($session_verify != $posted_verify) { 
     $error .= '<li>The verification code you entered is incorrect. 
</li>'; 
    } 

    if($error != '') { 
     echo '<div class="error_message">Attention! Please correct the 
errors below and try again.'; 
     echo '<ul class="error_messages">' . $error . '</ul>'; 
     echo '</div>'; 

    } else { 

    if(get_magic_quotes_gpc()) { $comments = stripslashes($comments); } 

我需要在這裏做什麼?或者我只需要編輯JavaScript文件?

回答

1

如果您使用JSON在代碼後面的某處調用函數,您將能夠返回狀態屬性。

我用它在我的當前項目藏漢,這裏是我如何使用它的一個例子:

var link = '/brainbattleJSON/MarkAssignmentAsInProgress'; 
       $(this).hide(); 
       $.getJSON(link, { questionId: qId }, function (json) { 
        if(json.status == "ok"){ 
        //do this 
        }else{ 
        //do this 
        } 

       }); 

後面的代碼:

// your function with validation 
// if the form is valid make status "ok" otherwise put anything else 
Return Json(New With {.status = "ok"}); 

希望能夠對你有點幫助:)

編輯:

您需要VAR鏈接的值更改爲福的路徑你在哪裏檢查表格。 然後你現在說你的錯誤!=''你會發回json。在此 你會說:

return json(new with{.status = 'error', .errors = 'yourErrors'}) 

所以錯誤可能是有用的,如果你在表單上超過1個錯誤發送陣列,以防萬一。 所有的消息將不再顯示在PHP與回聲,但你將不得不把錯誤與JavaScript。

+0

嗨,檢查我的編輯以上我的問題...我已經添加了一些PHP代碼,可能需要編輯? – Reflex84

+0

編輯我的答案以更好地代碼;) –

1

我有一個上傳的寄存器,並在下面的鏈接登錄頁面(zip文件): http://www.4shared.com/folder/uanQHCAg/_online.html

它使用相同的div來顯示成功和錯誤消息。你可以嘗試並實施你正在尋找的東西,並添加淡化效果。

+0

謝謝!我已經下載了你的zip文件,看着你的javascript文件,但你的代碼看起來不一樣......老實說,不知道該怎麼做(我只是開始學習javascript) – Reflex84