0

I'm having some difficulty getting my contact form working on my website. Whenever I try submit, it opens index.php, a blank page, rather than actually executing the script and firing off an email.

Here's the form.

    <form method="post" action="index.php">
        <div class="field">
            <label for="name">Name</label>
            <input type="text" name="name" id="name" />
        </div>
        <div class="field">
            <label for="email">Email</label>
            <input type="email" name="email" id="email" />
        </div>
        <div class="field">
            <label for="message">Message</label>
            <textarea name="message" id="message" rows="4"></textarea>
        </div>
        <ul class="actions">
            <li><input type="submit" type="submit" value="Send Message" /></li>
        </ul>
    </form>

Here's the index.php file I'm using.

<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: Test'; 
$to = 'brett@edge.yt'; 
$subject = 'Hello';

$body = "From: $name\n E-Mail: $email\n Message:\n $message";

if ($_POST['submit']) {
  if (mail ($to, $subject, $body, $from)) { 
    echo '<p>Your message has been sent!</p>';
  } else { 
    echo '<p>Something went wrong, go back and try again!</p>'; 
  }
}
?>

PHP is seemingly properly installed on my server, I can use a PHPInfo() file no problem.

Don't Panic
  • 37,589
  • 9
  • 55
  • 71
Edge
  • 113
  • 4
  • in HTML in input, you have type="submit" twice. Second one should be name="submit". This way you do not have $_POST['submit'] set, and never getting in the condition. – Dexa Aug 30 '16 at 15:10
  • Thank you, fixed that and now I'm actually getting somewhere. Unfortunately I'm receiving `Something went wrong, go back and try again` every time, so I must have made an error somewhere else too. – Edge Aug 30 '16 at 15:18

1 Answers1

0

The email will never try to send because $_POST['submit'] is not set. You don't have a control on your form named submit. If you intended that to be the submit button, you need to give it a name attribute.

<input type="submit" name="submit" value="Send Message" />

Then, in your PHP script, check that submit isset rather than just evaluating it as a boolean.

if (isset($_POST['submit'])) { ...

That way, if the form hasn't been submitted you won't get an undefined index notice.

Don't Panic
  • 37,589
  • 9
  • 55
  • 71
  • Oh well spotted. I changed `type="submit"` to `name="submit"` and the form now runs, but it fails each time. Think there's something else wrong too. – Edge Aug 30 '16 at 15:14
  • You mean, you get the 'Something went wrong' message? – Don't Panic Aug 30 '16 at 15:20
  • Yes, I'm trying to test out the form now. It does run thanks to the previous fix, but now I'm receiving the 'Something went wrong' message. – Edge Aug 30 '16 at 15:21
  • Do you have [error reporting enabled,](http://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display) and are you seeing any errors? – Don't Panic Aug 30 '16 at 15:33
  • In the error log, I've found a huge number of the same error: `[Tue Aug 30 18:10:28.433915 2016] [:error] .../public/xmlrpc.php' not found or unable to stat` There is also an occasional `sh: /usr/sbin/sendmail: No such file or directory` – Edge Aug 30 '16 at 16:01
  • Follow up: Managed to fix the issue. Uninstall and reinstall of sendmail did the trick. Thanks for your help. – Edge Aug 30 '16 at 16:27