I cannot for the life of me get my form to run. I am a recent graduate from college in Web and any help would be greatly appreciated! The form has a name, email, phone, datePicker and message component.
I have been at this for hours now and can’t figure it out.
My HTML aspect of the form:
***Note: Adjusted < & > so you can see code below.
<form id="contact-form">
<div class="row">
<div class="col-md-6">
<input name="name" id="name" type="text" placeholder="Name">
</div>
<div class="col-md-6">
<input name="mail" id="mail" type="text" placeholder="Email">
</div>
</div>
<div class="row">
<div class="col-md-6">
<input name="Phone" id="phone" type="text" placeholder="Phone">
</div>
<div class="col-md-6">
<font color="#999999">Date of Preferred Appointment: <input name="Date of Appointment" id="date" type="date" placeholder="Date"></font>
</div><br>
</div>
<textarea name="comment" id="comment" placeholder="Message"></textarea>
<div class="submit-area">
<input type="submit" id="submit_contact" value="Send Message">
<div id="msg" class="message"></div>
</div>
</form>
My PHP file:
<?php
/* ========================== Define variables ========================== */
#Your e-mail address
define("__TO__", "johndoe@gmail.com");
#Message subject
define("__SUBJECT__", "Website.com = From:");
#Success message
define('__SUCCESS_MESSAGE__', "Your message has been sent. Thank you!");
#Error message
define('__ERROR_MESSAGE__', "Error, your message hasn't been sent.");
#Messege when one or more fields are empty
define('__MESSAGE_EMPTY_FILDS__', "Please fill out all fields.");
/* ======================== End Define variables ======================== */
//Send mail function
function send_mail($to,$subject,$message,$headers){
if(@mail($to,$subject,$message,$headers)){
echo json_encode(array('info' => 'success', 'msg' => __SUCCESS_MESSAGE__));
} else {
echo json_encode(array('info' => 'error', 'msg' => __ERROR_MESSAGE__));
}
}
//Check e-mail validation
function check_email($email){
if(!@eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)){
return false;
} else {
return true;
}
}
//Get post data
if(isset($_POST['name']) and isset($_POST['phone']) and isset($_POST['date']) and isset($_POST['mail']) and isset($_POST['comment'])){
$name = $_POST['name'];
$phone = $_POST['phone'];
$date = $_POST['date'];
$mail = $_POST['mail'];
$website = $_POST['website'];
$comment = $_POST['comment'];
if($name == '') {
echo json_encode(array('info' => 'error', 'msg' => "Please enter your name."));
exit();
} else if($phone == ''){
echo json_encode(array('info' => 'error', 'msg' => "Please enter valid phone number."));
exit();
} else if($date == ''){
echo json_encode(array('info' => 'error', 'msg' => "Please enter valid date for perferred appointment."));
exit();
} else if($mail == '' or check_email($mail) == false){
echo json_encode(array('info' => 'error', 'msg' => "Please enter valid e-mail."));
exit();
} else if($comment == ''){
echo json_encode(array('info' => 'error', 'msg' => "Please enter your message."));
exit();
} else {
//Send Mail
$to = __TO__;
$subject = __SUBJECT__ . ' ' . $name;
$message = '
<html>
<head>
<title>Mail from '. $name .'</title>
</head>
<body>
<table style="width: 500px; font-family: arial; font-size: 14px;" border="1">
<tr style="height: 32px;">
<th align="right" style="width:150px; padding-right:5px;">Name:</th>
<td align="left" style="padding-left:5px; line-height: 20px;">'. $name .'</td>
</tr>
<tr style="height: 32px;">
<th align="right" style="width:150px; padding-right:5px;">Phone Number:</th>
<td align="left" style="padding-left:5px; line-height: 20px;">'. $phone .'</td>
</tr>
<tr style="height: 32px;">
<th align="right" style="width:150px; padding-right:5px;">Preferred Appointment Date:</th>
<td align="left" style="padding-left:5px; line-height: 20px;">'. $date .'</td>
</tr>
<tr style="height: 32px;">
<th align="right" style="width:150px; padding-right:5px;">E-mail:</th>
<td align="left" style="padding-left:5px; line-height: 20px;">'. $mail .'</td>
</tr>
<tr style="height: 32px;">
<th align="right" style="width:150px; padding-right:5px;">Website:</th>
<td align="left" style="padding-left:5px; line-height: 20px;">'. $website .'</td>
</tr>
<tr style="height: 32px;">
<th align="right" style="width:150px; padding-right:5px;">Comment:</th>
<td align="left" style="padding-left:5px; line-height: 20px;">'. $comment .'</td>
</tr>
</table>
</body>
</html>
';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
$headers .= 'From: ' . $mail . "\r\n";
send_mail($to,$subject,$message,$headers);
}
} else {
echo json_encode(array('info' => 'error', 'msg' => __MESSAGE_EMPTY_FILDS__));
}
?>