2011-08-01 173 views
0

確定這將是一個小長篇大論,我不知道是否應該在JavaScript或PHP的,我有一個接觸的形式如下:的jQuery/PHP表單提交

<form action="contact.php" method="POST" id='contact_form'> 
      <p><label for="name">Name</label> 
      <input type='text' name='name' id='name' /></p> 
      <div class="clear"></div> 
      <p id='name_error' class='error'>Enter your name</p> 

      <p><label for="email">Email</label> 
      <input type='text' name='email' id='email' /></p> 
      <div class="clear"></div> 
      <p id='email_error' class='error'>Enter a valid email address</p> 

      <p><label for="telephone">Telephone</label> 
      <input type='text' name='telephone' id='telephone' /></p> 
      <div class="clear"></div> 
      <p id='subject_error' class='error'>Enter your telephone number</p> 

      <p><label for="message">Details</label> 
      <textarea name='message' id='message' rows="30" cols="30"></textarea></p> 
      <div class="clear"></div> 
      <p id='message_error' class='error'>Enter your message</p> 

      <p id='mail_success' class='success'>Thank you. We will be in contact soon.</p> 
      <p id='mail_fail' class='error'>Sorry, an error has occured. Please try again later.</p> 

      <div id="button"> 
       <input type='submit' id='send_message' class="button" value='Submit' /> 
      </div> 
     </form> 

的JavaScript :

$(document).ready(function(){ 
     $('#send_message').click(function(e){ 

      //stop the form from being submitted 
      e.preventDefault(); 

      /* declare the variables, var error is the variable that we use on the end 
      to determine if there was an error or not */ 
      var error = false; 
      var name = $('#name').val(); 
      var email = $('#email').val(); 
      var telephone = $('#telephone').val(); 
      var message = $('#message').val(); 


      if(name.length == 0){ 
       var error = true; 
       $('#name_error').fadeIn(500); 
      }else{ 
       $('#name_error').fadeOut(500); 
      } 
      if(email.length == 0 || email.indexOf('@') == '-1'){ 
       var error = true; 
       $('#email_error').fadeIn(500); 
      }else{ 
       $('#email_error').fadeOut(500); 
      } 
      if(telephone.length == 0){ 
       var error = true; 
       $('#subject_error').fadeIn(500); 
      }else{ 
       $('#subject_error').fadeOut(500); 
      } 
      if(message.length == 0){ 
       var error = true; 
       $('#message_error').fadeIn(500); 
      }else{ 
       $('#message_error').fadeOut(500); 
      } 

      //now when the validation is done we check if the error variable is false (no errors) 
      if(error == false){ 
       //disable the submit button to avoid spamming 
       //and change the button text to Sending... 
       $('#send_message').attr({'disabled' : 'true', 'value' : 'Sending...' }); 

       //we submit it to contact.asp 
       $.post("contact.php", $("#contact_form").serialize(),function(result){ 
        //and after the ajax request ends we check the text returned 
        if($.trim(result) == 'sent'){ 
         $('#button').remove(); 
         $('#mail_fail').fadeOut(500); 
         $('#mail_success').fadeIn(500); 
        }else{ 
         //show the mail failed div 
         $('#mail_fail').fadeIn(500); 
         //reenable the submit button by removing attribute disabled and change the text back to Send The Message 
         $('#send_message').removeAttr('disabled').attr('value', 'Submit'); 
        } 
       }); 
      } 
     });  
    }); 

和PHP:

<?php 

$EmailFrom = Trim(stripslashes($_POST['email'])); 
$EmailTo = "[email protected]"; 
$Subject = "Contact Form Message"; 
$name = Trim(stripslashes($_POST['name'])); 
$email = Trim(stripslashes($_POST['email'])); 
$telephone = Trim(stripslashes($_POST['telephone'])); 
$message = Trim(stripslashes($_POST['message'])); 


$validationOK=true; 
if (Trim($name)=="") $validationOK=false; 
if (Trim($email)=="") $validationOK=false; 
if (Trim($telephone)=="") $validationOK=false; 
if (Trim($message)=="") $validationOK=false; 
if (!$validationOK) { 
    print "failed"; 
    exit; 
} 


$Body = ""; 
$Body .= "name: "; 
$Body .= $name; 
$Body .= "\n"; 
$Body .= "email: "; 
$Body .= $email; 
$Body .= "\n"; 
$Body .= "telephone: "; 
$Body .= $telephone; 
$Body .= "\n"; 
$Body .= "message: "; 
$Body .= $message; 
$Body .= "\n"; 


$success = mail($EmailTo, $Subject, $Body, "From: <$email>"); 

if ($success){ 
    print "sent"; 
} 
else{ 
    print "failed"; 
} 
?> 

當填寫我收到「對不起,發生錯誤,請重試啦。之三「。消息,但沒有看到我出錯的地方?

在此先感謝您的幫助。

+0

什麼是JS的結果?嘗試提醒它。如果您寫入failed1/failed2而不是失敗/失敗,則可以指定它實際失敗的位置。 – Damien

+0

當我警告結果,它給了我整個頁面的HTML? – Bob

+0

註釋掉你的JavaScript並通過正常發送數據來調試'contact.php'腳本;如果除了預期的「發送」以外,還有其他問題,那麼問題出在PHP腳本上。 – stealthyninja

回答

-1

是否上傳到支持PHP的服務器,因爲如果沒有支持服務器,PHP將無法正常運行。

+0

是支持 – Bob

+0

請使用評論提出問題或進行澄清。 – RustyFluff

+0

我以前不能,因爲我沒有評論無處不在的功能。 –

0

如果結果包含您網頁的整個html代碼,這意味着您的腳本不會停在正確的位置,也可能是js發佈到錯誤的網址(是的,我看到您指定了contact.php,但如果有在htaccess中重寫規則是可能的)。

嘗試添加exit(0);在這個腳本的底部。在最後如果。看起來這個php包含在另一個php文件中,它在發送電子郵件後顯示網站的其他部分。

0

php代碼沒有返回任何東西給用戶。

  1. 是您的電子郵件模塊在PHP?
  2. 您應該回顯您的消息正文以進行調試。

我個人儘量避免使用打印,因爲它只是一個結構,有不同的行爲,當你「打印」多個項目背靠背,不要緊

0

你工作在本地主機或者是你的共享主機服務器上的代碼? (即GoDaddy或類似公司)

如果您位於本地主機上,則可能沒有配置正確的郵件服務器。如果您的代碼是託管的,您是否有與提供者一起發送電子郵件的電子郵件帳戶?您的帳戶包是否提供對郵件()功能的訪問?

如果您有權訪問PHP錯誤日誌或Apache日誌,您應該檢查這些日誌。您還應該設置一個phpinfo頁面,以便您可以看到您的環境是如何配置的。

在任一情況下,您都可以檢查瀏覽器中的POST請求,以查看服務器是否返回HTTP錯誤。啓用開發工具後,將Firefox與Firebug或Chrome一起使用。