Topic: Trying to get contact form working
webcatdesign asked 5 years ago
Expected behavior The form should send an email.
Actual behavior The email is always format invalid. I have attempted to follow the instructions via the Contact Form tutorial but always end up with the same error.
Resources (screenshots, code snippets etc.)
in action: www.webcatdesign.com.au
HTML CODE:
https://mdbootstrap.com/snippets/jquery/webcatdesign/1232275?action=forum_snippet
PHP in 'mail.php':
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$subject = $_POST['subject'];
header('Content-Type: application/json');
if ($name === ''){
print json_encode(array('message' => 'Name cannot be empty', 'code' => 0));
exit();
}
if ($email === ''){
print json_encode(array('message' => 'Email cannot be empty', 'code' => 0));
exit();
} else {
if (!filter_var($email, FILTER_VALIDATE_EMAIL)){
print json_encode(array('message' => 'Email format invalid.', 'code' => 0));
exit();
}
}
if ($subject === ''){
print json_encode(array('message' => 'Subject cannot be empty', 'code' => 0));
exit();
}
if ($message === ''){
print json_encode(array('message' => 'Message cannot be empty', 'code' => 0));
exit();
}
$content="From: $name \nEmail: $email \nMessage: $message";
$recipient = "webcat.design1@gmail.com";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $content, $mailheader) or die("Error!");
print json_encode(array('message' => 'Email successfully sent!', 'code' => 1));
exit();
?>
webcatdesign answered 5 years ago
For anyone else who comes across this and wants to know what is missing, I was using a template for the contact form that did not include the 'name' field in each of the form sections, after id.
Therefore, I was missing the 'name' section for each input.
<input type="text" id="subject" name="subject" class="form-control">
<label for="subject">Subject</label>
Hopefully this helps anyone who comes across this!
Bartłomiej Malanowski staff pro premium commented 5 years ago
We're glad you've found a solution to your issue!
FREE CONSULTATION
Hire our experts to build a dedicated project. We'll analyze your business requirements, for free.
Resolved
- User: Free
- Premium support: No
- Technology: MDB jQuery
- MDB Version: 4.8.10
- Device: PC
- Browser: Any
- OS: Windows 10
- Provided sample code: No
- Provided link: Yes