Topic: Setting teh style for error returns and success returns
tommyhutcheson free asked 6 years ago
https://mdbootstrap.com/components/bootstrap-contact-form/
Hello
what is the best way to set different styles for successful php returns and error returns.
<a class="btn btn-primary" onclick="validateForm()">Send</a>
<div class="alert hide" role="alert hide">
<div class="status" id="status"></div>
</div>
.errorCSS
{
background-color: red;
}
.successCSS
{
color: #004085;
background-color: #cce5ff;
border-color: #b8daff;
}
function validateForm() {
$.ajax({
url : "register.php",
type: "POST",
data :$('#registration-form').serialize(),
success: function(data, textStatus, jqXHR)
{
$('#status').text(data.message).addClass('successCSS');
if (data.code) //If mail was sent successfully, reset the form.
$('#registration-form').closest('form').find("input[type=text], textarea").val("");
},
error: function (jqXHR, textStatus, errorThrown)
{
$('#status').text(jqXHR).addClass('errorCSS');
}
});
}
<?php
$firstname = $_POST['firstname'];
$surname = $_POST['surname'];
$email = $_POST['email'];
header('Content-Type: application/json');
if ($firstname === ''){
print json_encode(array('message' => 'firstname cannot be empty', 'code' => 0));
exit();
}
if ($surname === ''){
print json_encode(array('message' => 'Surname 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();
}
}
$content="Email: $email nMessage: $message";
$recipient = "@gmail.com";
$mailheader = "From: $email rn";
mail($recipient, $subject, $content, $mailheader) or die("Error!");
print json_encode(array('message' => 'Registration successful !', 'code' => 1));
exit();
?>
FREE CONSULTATION
Hire our experts to build a dedicated project. We'll analyze your business requirements, for free.
Status
Opened
Specification of the issue
- ForumUser: Free
- Premium support: No
- Technology: MDB jQuery
- MDB Version: -
- Device: -
- Browser: -
- OS: -
- Provided sample code: No
- Provided link: No
Tags
Bartłomiej Malanowski staff commented 6 years ago
You can prepare two versions of the output message and use suitable version basing on the response code