2013-08-24 44 views
0

我知道這個問題已經被問了很多,但沒有一個解決方案適用於我!

請先閱讀下面的編輯!

我是比較新的PHP和我創建一個簡單的聯繫形式:

HTML

<form action="contactUsSuccess.php" id="contactForm" method="post" name="contactForm" onsubmit="return validateContactUs();"> 
    <table> 
     <tr> 
      <td> 
       <input id="name" maxlength="70" name="name" placeholder="Name" type="text"> 
      </td> 
      <td> 
       <input id="email" maxlength="255" name="email" placeholder="Email" type="text"> 
      </td> 
     </tr> 
     <tr> 
      <td colspan="2"> 
       <textarea id="message" maxlength="5000" name="message" placeholder="Message"></textarea> 
      </td> 
     </tr> 
     <tr> 
      <td colspan="2" id="submitArea"> 
       <input id="submitButton" type="submit" value="Send"> 
      </td> 
     </tr> 
    </table> 
</form> 

PHP,這是另一頁

<?php 
    $success = false; 

    $name = $_POST["name"]; 
    $email = $_POST["email"]; 
    $message = $_POST["message"]; 

    $to = "[email protected]"; 
    $subject = "New Contact Message - ----"; 
    $messageMail = "From: " . $name . "(" . $email . ")" . " | Message: " . $message; 
    $header = "From: [email protected]"; 

    if(mail($to, $subject, $messageMail, $header)) { 
     $success = true; 
    } 
?> 

我的錯誤:

Notice: Undefined index: name in Z:\Documents\Workspace\----\help\contactUsSuccess.php on line 6 

Notice: Undefined index: email in Z:\Documents\Workspace\----\help\contactUsSuccess.php on line 7 

Notice: Undefined index: message in Z:\Documents\Workspace\----\help\contactUsSuccess.php on line 8 

正如你或許可以看到,我的窗體的onsubmit執行JavaScript的,剛剛返回true或false。如果你出於任何原因認爲這可能是不工作的原因,請問。

而且,我已經試過了上面顯示的相同的PHP代碼,並把它放在同一個文件中的原始HTML(並且還與isset周圍的if語句...沒有運氣。

哦,和電子郵件仍然發送...它只是發送:

From:() | Message:

編輯:所以這是JavaScript的下面是它的主要部分:

if (!(nameLen > 0 && nameLen <= 70 && emailLen > 0 && emailLen <= 255 && messageLen > 0 && messageLen <= 5000)) { 
     cont = false; 
    } else { 
     name.disabled = "true"; 
     email.disabled = "true"; 
     message.disabled = "true"; 
     document.getElementById("submitButton").disabled = "true"; 
    } 

    if (cont) { 
     return true; 
    } else { 
     return false; 
    } 
} 

當我刪除了3 --.disabled = "true";,它工作!不過,我確實有他們的原因。有其他選擇嗎?我想這是我要問現在...

+1

請問當你把它的工作提交事件中的驗證功能? (即,JavaScript) –

+0

您需要首先檢查是否設置了post變量的天氣 –

+0

您確定在所有$ _POST變量中使用了isset方法嗎? –

回答

4

您必須檢查所有的變量名,電子郵件,留言有值,例如:

if (isset($_POST['name']) && isset($_POST['email']) && isset($_POST['message'])) 

,如果是,則發送電子郵件。你的錯誤是說這些變量沒有價值。問題可能在JavaScript驗證中甚至存在,所以嘗試將其放回去,然後檢查。

編輯時,問題編輯:

試試這個:

<script> 
function validate(form) { 
fail = validateName(form.name.value) 
fail += validateEmail(form.email.value) 
fail += validateMessage(form.message.value) 
if (fail == "") return true 
    else { alert(fail); return false } 
} 

function validateName(field) { 
if (field == "") return "No Username was entered.\n" 
else if (field.length < 5) 
    return "Usernames must be at least 5 characters.\n" 
else if (field.length > 15) 
    return "Usernames must be shorter than 15 characters.\n" 
return "" 
</script> 
+1

或者您可能嘗試將所有變量賦值爲空字符串,例如'$ name = $ email = $ message =「」'但如果您發送沒有值的表單,它將發送空電子郵件 –

+0

正在處理表單數據的頁面只有在100%的時間提交表格時纔會訪問*。此外,我已經嘗試了'isset'的東西...沒有運氣。請閱讀我最新的編輯,謝謝! –

+1

我更新了我的答案,有一種方法如何驗證它...這個函數很簡單,只需在表單標籤中添加'onSubmit =「return validate(this)」',當你按下提交時它將運行函數validate(form )...然後現在檢查名稱的長度,但是你可以用同樣的方法做電子郵件和消息......如果驗證沒有問題,它會返回true並且可以驗證,如果沒有,它會提醒錯誤,希望它能幫助你 –

0

檢查您的按鈕,以確保它具有name屬性:

<input id="submitButton" type="submit" name="yourname" value="Send"> 

<?php 
if (isset($_POST['yourname'])) { 
    /*do some codes here*/ 
} else { 
    /**/ 
} 
?>