ajax — чего-то не хватает в моей контактной форме для проверки подлинности в php или javascript

Вот мой HTML, PHP и скрипт.
Я получаю всю информацию правильно по электронной почте, за исключением оценки, которая представляет собой форму dropbox. Может кто-нибудь взглянуть на мой сценарий?

Спасибо

< script >
function validateForm() {
var x = document.getElementById('companyname').value;
if (x == "") {
document.getElementById('status').innerHTML = "empty";
return false;
}
x = document.getElementById('email').value;
if (x == "") {
document.getElementById('status').innerHTML = "Email cannot be empty";
return false;
} else {
var re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
if (!re.test(x)) {
document.getElementById('status').innerHTML = "Email format invalid";
return false;
}
}
x = document.getElementById('address').value;
if (x == "") {
document.getElementById('status').innerHTML = "empty";
return false;
}
x = document.getElementById('phone').value;
if (x == "") {
document.getElementById('status').innerHTML = "empty";
return false;
}
x = document.getElementById('name').value;
if (x == "") {
document.getElementById('status').innerHTML = "empty";
return false;
}
x = document.getElementById('estimate').value;
if (x == "") {
document.getElementById('status').innerHTML = "empty";
return false;
}
document.getElementById('status').innerHTML = "Sending...";
formData = {
'companyname': $('input[name=companyname]').val(),
'email': $('input[name=email]').val(),
'address': $('input[name=address]').val(),
'phone': $('input[name=phone]').val(),
'name': $('input[name=name]').val(),
'estimate': $('input[name=estimate]').val(),
};


$.ajax({
url: "mail.php",
type: "POST",
data: formData,
success: function(data, textStatus, jqXHR) {

$('#status').text(data.message);
if (data.code) //If mail was sent successfully, reset the form.
$('#contact-form').closest('form').find("input[type=text], textarea").val("");
},
error: function(jqXHR, textStatus, errorThrown) {
$('#status').text(jqXHR);
}
});


} <
/script>
<?php
$companyname = $_POST['companyname'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$address = $_POST['address'];
$name = $_POST['name'];
$estimate = $_POST['estimate'];
header('Content-Type: application/json');
if ($companyname === ''){
print json_encode(array('message' => '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 ($address === ''){
print json_encode(array('message' => 'empty', 'code' => 0));
exit();
}
if ($phone === ''){
print json_encode(array('message' => 'empty', 'code' => 0));
exit();
}
if ($name === ''){
print json_encode(array('message' => 'empty', 'code' => 0));
exit();
}
if ($estimate === ''){
print json_encode(array('message' => 'empty', 'code' => 0));
exit();
}
$content="From: $companyname \nEmail: $email \nAddress: $address \nPhone: $phone \nName: $name \nEstimate: $estimate";
$recipient = "info@itnecommerce.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();
?>
<form id="contact-form" name="contact-form" action="mail.php" method="POST" onsubmit="return validateForm()">
<div class="col-md-6">
<div class="form-group">
<label for="companyname"><span style="color: #000">companyname</span></label>
<input type="text" name="companyname" class="form-control form-control-sm" id="companyname" placeholder="put company name">
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="address"><span style="color: #000">address</span></label>
<input type="text" name="address" class="form-control form-control-sm" id="address" placeholder="put address.">
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="email"><span style="color: #000">email</span></label>
<input type="text" name="email" class="form-control form-control-sm" id="email" placeholder="example@gmail.com">
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="phone"><span style="color: #000">cell</span></label>
<input type="text" name="phone" class="form-control form-control-sm" id="phone" placeholder="800-123-4567">
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="name"><span style="color: #000">name</span></label>
<input type="text" name="name" class="form-control form-control-sm" id="name" placeholder="put name.">
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="estimate"><span style="color: #000">estimate</span></label>
<select class="form-control form-control-sm" name="estimate" id="estimate">
<option value="1">$1~$1000</option>
<option value="2">$1000~$3000</option>
<option value="3">$3000~$5000</option>
<option value="4">$5000~$10000</option>
</select>
</div>
</div>
</form>

1

Решение

Задача ещё не решена.

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector