0

so this is a Contact Form php script with basic validations. This script was running absolutely fine without any issues or errors. Until recently I transferred the file to another web hosting.

The previous web host had PHP version 5.4.35 While the new web host has PHP version 5.4.45

I don't know much about PHP so I don't know what's going on. Here's what the error_log had logged everytime someone submitted the contact form.

[17-Jun-2016 17:05:20 Etc/GMT] PHP Notice: Undefined index: name in /home/domain/public_html/contact.php on line 70

[17-Jun-2016 17:05:20 Etc/GMT] PHP Notice: Undefined index: email in /home/domain/public_html/contact.php on line 76

[17-Jun-2016 17:05:20 Etc/GMT] PHP Notice: Undefined index: message in /home/domain/public_html/contact.php on line 82

In order to solve this I initialized the error variables ($name,$email,$message) as null in that case there was no more errors but the contact form failed to work.

Please help me! I don't know why this problem is occuring.

<?php

if (isset($_POST["submit"])) {
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $human = intval($_POST['human']);
    $to = 'example@domain.com';
    $subject = 'Contact Form';

    $header = "From:contact@domain.com \r\n";
    $header = "Cc:contact2@domain.com \r\n";
    $header .= "MIME-Version: 1.0\r\n";
    $header .= "Content-type: text/html\r\n";

    $body = "From: $name\n E-Mail: $email\n Message:\n $message";
    if (!$_POST['name']) {
        $errName = 'Please enter your name';
    }

    if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
        $errEmail = 'Please enter a valid email address';
    }

    if (!$_POST['message']) {
        $errMessage = 'Please enter your message';
    }

    if ($human !== 2) {
        $errHuman = 'Your anti-spam is incorrect';
    }

    if (!$errName && !$errEmail && !$errMessage && !$errHuman) {
        if (mail($to, $subject, $body, $header)) {
            $result='Thank You! Your message will be replied soon!';
        } else {
            $result='Sorry there was an error sending your message.';
        }
    }
}
?>
        <form class="col l12" method="post" action="contact.php">
                    <input id="name" name="name" type="text" class="validate" value="<?php echo htmlspecialchars($_POST['name']); ?>">
                    <?php echo "<p class='red-text'>$errName</p>";?>
                    <label for="name">Name</label>

                    <input id="email" name="email" type="email" class="validate" value="<?php echo htmlspecialchars($_POST['email']); ?>">
                    <?php echo "<p class='red-text'>$errEmail</p>";?>
                    <label for="email">Email</label>

                    <textarea name="message" class="materialize-textarea"><?php echo htmlspecialchars($_POST['message']);?></textarea>
                    <?php echo "<p class='red-text'>$errMessage</p>";?>
                    <label for="message">Message</label>

                    <label for="human"><strong>AntiSPAM Check:</strong> 5 - 3 = ?</label>
                    <input id="human" name="human" type="text" class="validate">
                    <?php echo "<p class='red-text'>$errHuman</p>";?>

                        <p class="left-align"><button class="blue darken-1 btn-large waves-effect waves-light" id="submit" type="submit" style="font-weight:500;" name="submit">Send</button>
                        <?php echo $result; ?>
        </form>
Community
  • 1
  • 1
Swayam
  • 25
  • 7
  • @Rizier123 Although this might seem duplicate because of the same error tags I had implemented the changes mentioned in the other thread but that didn't solve the problem. This problem is unique although marked under the same "php notice" tag. – Swayam Jun 17 '16 at 17:53

1 Answers1

0

Those errors are telling you that your $_POST array doesn't have name or email or message. Someone submitted an empty form.

Now, you check for missing values later on in your script, but not until after you've already tried to access these missing array values.

Best thing would be to move your validation code...

if (!$_POST['name']) {
    ...

...to the top so it runs first. Then, only do $name = $_POST['name']; once you're sure that it exists.

Here2Help
  • 484
  • 2
  • 6
  • I did the changes but it seems the errors appear when the page is refreshed or loaded. However after implementing the changes you mentioned the errors that were generated after submitting the form isn't there but the form still fails to work :( – Swayam Jun 17 '16 at 17:54
  • @Swayam Well your question was about the errors, and I answered that question. It makes perfect sense that the errors would appear when the page is refreshed, there is no form data submitted at that point! – Here2Help Jun 17 '16 at 18:03
  • @Swayam Not sure what you mean by "still fails to work." If you are now getting different errors, or perhaps just not getting an email, then that's an entirely new issue. Do some troubleshooting, adding logging, and come back and post a new question when you have specifics about your issue and can show what you've tried. – Here2Help Jun 17 '16 at 18:04
  • Hmm.. but then why is the above script creating problems in this webhost? Is this happening because of the change in php versions? – Swayam Jun 17 '16 at 18:15
  • @Swayam eh? your original issue was a _notice_ which isn't quite an error. those are generated based on your php settings, where you control which error levels are reported/logged. I don't think the minor version change had anything to do with it. – Here2Help Jun 17 '16 at 18:23