TJ Mazeika
TJ Mazeika

Reputation: 1042

$_POST Returning "Undefined index"

I know this question has been asked a lot, but none of the solutions work for me!

Please read the edit below first!

I'm relatively new to PHP, and I am creating a simple contact form:

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, which is on another page.

<?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;
    }
?>

My error:

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

As you may be able to see, my form's onsubmit executes JavaScript, which just returns true or false. If you think for any reason that may be the reason why this isn't working, just ask.

Also, I've tried the same PHP code that is shown above and put it in the same file as the original HTML (and also surrounding it with isset if statements... no luck.

Oh, and the email still sends... it just sends:

From: () | Message:

Edit: So it was the JavaScript! Here is the main part of it:

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;
    }
}

When I removed the 3 --.disabled = "true";, it worked! However, I did have them in there for a reason. Is there an alternative? I guess this is what I'm asking for now...

Upvotes: 0

Views: 2067

Answers (2)

jundell agbo
jundell agbo

Reputation: 289

Check your button to make sure it has the name attribute:

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

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

Upvotes: 0

M.Svrcek
M.Svrcek

Reputation: 5635

You must check if all the variables name, email, message have values, for example:

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

and if yes, then send the email. Your error is saying that there's no value in those variables. Problem may be even in yout javascript validation, so try to put it away and then check.

EDIT AFTER QUESTION EDIT:

Try this:

<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>

Upvotes: 4

Related Questions