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
E-Mail: $email
Message:
$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.
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.