I am not good at forms, and I am having some trouble. The problems are:
In the HTML section there is an "action" item that I do not know what to fill with. Right now it is #, but that is just a placeholder.
I don't get how to call up the php script that makes the thing work.
In the php section there is this: $from = 'example@example.com'; What should actually go in the email address area?
I've read all kinds of tutorials, and I am getting more confused with each, and none seem to directly address my troubles. Can someone help me to sort this out? I greatly appreciate it. If you see any other obvious errors other than what I stated above, please feel free to share that information as well. Thank you in advance for your help.
The HTML:
<form method="post" action="#">
<div class="row uniform">
<div class="6u 12u$(xsmall)">
<label for="name">Full Name</label>
<input name="name" type="text" class="required" id="name" value="" />
</div>
<div class="6u$ 12u$(xsmall)">
<label for="email">Email Address</label>
<input name="email" type="email" class="required" id="email" value="" />
</div>
<div class="6u 12u$(xsmall)">
<label for="date">Date of Birth</label>
<input name="date" type="text" class="required" id="date" value="" />
</div>
<div class="6u$ 12u$(xsmall)">
<label for="age">Age as of July 1, 2016</label>
<input name="age" type="text" class="required" id="age" value="" />
</div>
<div class="6u 12u$(xsmall)">
<label for="phone">Phone Number</label>
<input name="phone" type="tel" class="required" id="phone" value="" />
</div>
<div class="12u$">
<label for="club">Current Club Membership</label>
<div class="select-wrapper">
<select name="club" class="required" id="club">
<option value="">-</option>
<option value="Colonial Road Runners">Colonial Road Runners</option>
<option value="Peninsula Track Club">Peninsula Track Club</option>
<option value="Tidewater Striders">Tidewater Striders</option>
</select>
</div>
</div>
<div class="12u$">
<label for="message">Message</label>
<textarea name="message" id="message" rows="6"></textarea>
</div>
<div class="12u$">
<ul class="actions">
<li><input type="submit" value="Send Message" class="special" /></li>
<li><input type="reset" value="Reset" /></li>
</ul>
</div>
</div>
</form>
The php:
<?php
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$email = $_POST['email'];
$date = $_POST['date'];
$age = $_POST['age'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$from = 'example@example.com';
$to = 'hrsupergp@gmail.com';
$subject = 'New Registration';
$body = "From: $name
E-Mail: $email
Message:
$message";
// Check if name has been entered
if (!$_POST['name']) {
$errName = 'Please enter your name';
}
// Check if email has been entered and is valid
if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$errEmail = 'Please enter a valid email address';
}
if (mail ($to, $subject, $body, $from)) {
$result='<div class="alert alert-success">Thank You! I will be in touch</div>';
} else {
$result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later</div>';
}
}
?>
Your questions 1 & 2 just have to do with your action, which is the page to load when submitting the form. This can be the current file (in PHP you can do forexample <?php echo __FILE__; ?>
or <?php echo $_SERVER['PHP_SELF'] ?>
) or just directly the script index.php
where you gonna check the data. So basically point it to the file where your PHP code is in.
3 is just mail header information, the from email address can be something like "no-reply@yourdomain.com"
or "info@yourdomain.com"
or your own email address so then when people click "reply" in their email client it sends it to that filled in email address. As per comment below, this is overruled by the additional header of "Reply-To".
To add to your email message body the form selected fields:
$message = "This is a string before the contents of $POST['message']" . $message;
$message .= "This is a string after the contents of $POST['message']"
$body = "From: $name
E-Mail: $email
Message:
$message";
However emails are quite tedious to master, incorrect header information may lead to the email being marked as spam. As for example a new line in an email header should be done with " "
instead of just " "
.
My recommendation for sending emails is using a library called phpmailer. Might be a slight pain to set up, but now you don't need to deal with header information.