簡體   English   中英

$ _POST返回“未定義索引”

[英]$_POST Returning “Undefined index”

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

請先閱讀下面的修改!

我對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 = "----@gmail.com";
    $subject = "New Contact Message - ----";
    $messageMail = "From: " . $name . "(" . $email . ")" . " | Message: " . $message;
    $header = "From: ----@gmail.com";

    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,該JavaScript僅返回true或false。 如果您出於某種原因認為這可能不起作用,請詢問。

另外,我嘗試了與上面顯示的相同的PHP代碼,並將其與原始HTML放在相同的文件中(並且還用isset if語句將其包圍了……沒有運氣。

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

來自:()| 信息:

編輯:這就是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"; , 有效! 但是,我確實將它們放在其中是有原因的。 還有其他選擇嗎? 我想這就是我現在要的...

您必須檢查所有變量名稱,電子郵件,消息是否都具有值,例如:

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>

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

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM