$ _POST返回“未定义的索引”

时间:2013-08-24 06:18:09

标签: php javascript html forms post

我知道这个问题已被问到很多,但没有一个解决方案适合我!

请先阅读下面的编辑内容

我对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,它只返回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";时,它有效!但是,我确实有他们在那里有一个原因。还有其他选择吗?我想这就是我现在要求的......

2 个答案:

答案 0 :(得分: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 :(得分:0)

检查按钮以确保其具有名称属性:

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

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