html -код < /p>
Send
< /code>
php code (contact.php) < /p>
header('Content-type: application/json');
if($_POST) {
$to_email = "nishat.sayyed0403@gmail.com"; //Recipient email, Replace with own email here
//check if its an ajax request, exit if not
if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {
$output = json_encode(array( //create JSON data
'type' => 'error',
'text' => 'Sorry Request must be Ajax POST'
));
die($output); //exit script outputting json data
}
//Sanitize input data using PHP filter_var().
$user_name = filter_var($_POST["name"], FILTER_SANITIZE_STRING);
$user_email = filter_var($_POST["email"], FILTER_SANITIZE_EMAIL);
$phone_number = filter_var($_POST["phone"], FILTER_SANITIZE_NUMBER_INT);
$message = filter_var($_POST["message"], FILTER_SANITIZE_STRING);
//additional php validation
if(strlen($user_name) < 4) { // If length is less than 4 it will output JSON error.
$output = json_encode(array(
'type' => 'error',
'text' => 'Name is too short or empty!'
));
die($output);
}
if(!filter_var($user_email, FILTER_VALIDATE_EMAIL)) { //email validation
$output = json_encode(array(
'type' => 'error',
'text' => 'Please enter a valid email!'
));
die($output);
}
if(!filter_var($phone_number, FILTER_SANITIZE_NUMBER_FLOAT)) { //check for valid numbers in phone number field
$output = json_encode(array(
'type' => 'error',
'text' => 'Enter only digits in phone number'
));
die($output);
}
if(strlen($message) < 3) { //check emtpy message
$output = json_encode(array(
'type' => 'error',
'text' => 'Too short message! Please enter something.'
));
die($output);
}
//email subject
$subject = 'New mail via contact form';
//email body
$message_body = $message . "\r\n\r\n-" . $user_name . "\r\n\r\nEmail : " . $user_email . "\r\nPhone Number : " . $phone_number;
//proceed with PHP email.
$headers = 'From: ' . $user_name . '' . "\r\n" . 'Reply-To: ' . $user_name . '' . "\r\n" . 'X-Mailer: PHP/' . phpversion();
$send_mail = mail($to_email, $subject, $message_body, $headers);
echo "From contact.php";
if(!$send_mail) {
//If mail couldn't be sent output error. Check your PHP email configuration (if it ever happens)
$output = json_encode(array(
'type' => 'error',
'text' => 'Could not send mail! Please check your PHP mail configuration.'
));
die($output);
} else {
$output = json_encode(array(
'type' => 'success',
'text' => 'Hi ' . $user_name . ', thank you for your email, we will get back to you shortly.'
));
echo ($output);
}
}
< /code>
ajax code < /p>
$(function() {
// validate contact form on keyup and submit
$("#contactForm").validate({
rules: {
name: {
required: true,
minlength: 2,
lettersonly: true
},
email: {
required: true,
minlength: 6,
email: true
},
phone: {
required: true,
digits: true,
minlength: 10,
maxlength: 15
},
message: {
required: true,
minlength: 6
}
},
messages: {
name: {
required: "Please enter your name",
minlength: "Minimum 2 characters",
lettersonly: "Only letters please!"
},
email: {
required: "Please enter your email address",
minlength: "Minimum 6 characters",
email: "That's an invalid email"
},
phone: {
required: "Please enter your phone number",
digits: "Only digits (no spaces)",
minlength: "Minimum 10 characters",
maxlength: "Maximum 15 characters"
},
message: {
required: "Please enter your message",
minlength: "Minimum 6 characters"
}
},
success: function(label) {
label.addClass("valid").text("Perfect!");
},
submitHandler: function(element) {
var ajaxform = $(element),
url = ajaxform.attr('action'),
type = ajaxform.attr('method'),
data = {};
$(ajaxform).find('[name="submit"]').html(' Sending...');
ajaxform.find('[name]').each(function(index, value) {
var field = $(this),
name = field.attr('name'),
value = field.val();
data[name] = value;
});
$.ajax({
url: url,
type: type,
data: data,
success: function(response) {
if (response.type == 'success') {
$("#contactForm").before("×" + response.text + "");
$(ajaxform).each(function() {
this.reset();
$(this).find('[name="submit"]').html(' Send');
}).find('.valid').each(function() {
$(this).remove('label.valid');
})
} else if (response.type == 'error') {
$("#contactForm").before("×" + response.text + "");
$(ajaxform).find('[name="submit"]').html(' Send');
}
}
});
return false;
}
});
< /code>
Проблема в том, что всякий раз, когда я нажимаю кнопку «Отправить». Действие не выполняется. Ни положительный, ни отрицательный. Ничего не происходит.>
Подробнее здесь: https://stackoverflow.com/questions/453 ... mit-button
Ajax Действие не происходит после нажатия кнопки отправки ⇐ Html
Программисты Html
-
Anonymous
1750285028
Anonymous
html -код < /p>
[i]
[/i] Send
< /code>
php code (contact.php) < /p>
header('Content-type: application/json');
if($_POST) {
$to_email = "nishat.sayyed0403@gmail.com"; //Recipient email, Replace with own email here
//check if its an ajax request, exit if not
if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {
$output = json_encode(array( //create JSON data
'type' => 'error',
'text' => 'Sorry Request must be Ajax POST'
));
die($output); //exit script outputting json data
}
//Sanitize input data using PHP filter_var().
$user_name = filter_var($_POST["name"], FILTER_SANITIZE_STRING);
$user_email = filter_var($_POST["email"], FILTER_SANITIZE_EMAIL);
$phone_number = filter_var($_POST["phone"], FILTER_SANITIZE_NUMBER_INT);
$message = filter_var($_POST["message"], FILTER_SANITIZE_STRING);
//additional php validation
if(strlen($user_name) < 4) { // If length is less than 4 it will output JSON error.
$output = json_encode(array(
'type' => 'error',
'text' => 'Name is too short or empty!'
));
die($output);
}
if(!filter_var($user_email, FILTER_VALIDATE_EMAIL)) { //email validation
$output = json_encode(array(
'type' => 'error',
'text' => 'Please enter a valid email!'
));
die($output);
}
if(!filter_var($phone_number, FILTER_SANITIZE_NUMBER_FLOAT)) { //check for valid numbers in phone number field
$output = json_encode(array(
'type' => 'error',
'text' => 'Enter only digits in phone number'
));
die($output);
}
if(strlen($message) < 3) { //check emtpy message
$output = json_encode(array(
'type' => 'error',
'text' => 'Too short message! Please enter something.'
));
die($output);
}
//email subject
$subject = 'New mail via contact form';
//email body
$message_body = $message . "\r\n\r\n-" . $user_name . "\r\n\r\nEmail : " . $user_email . "\r\nPhone Number : " . $phone_number;
//proceed with PHP email.
$headers = 'From: ' . $user_name . '' . "\r\n" . 'Reply-To: ' . $user_name . '' . "\r\n" . 'X-Mailer: PHP/' . phpversion();
$send_mail = mail($to_email, $subject, $message_body, $headers);
echo "From contact.php";
if(!$send_mail) {
//If mail couldn't be sent output error. Check your PHP email configuration (if it ever happens)
$output = json_encode(array(
'type' => 'error',
'text' => 'Could not send mail! Please check your PHP mail configuration.'
));
die($output);
} else {
$output = json_encode(array(
'type' => 'success',
'text' => 'Hi ' . $user_name . ', thank you for your email, we will get back to you shortly.'
));
echo ($output);
}
}
< /code>
ajax code < /p>
$(function() {
// validate contact form on keyup and submit
$("#contactForm").validate({
rules: {
name: {
required: true,
minlength: 2,
lettersonly: true
},
email: {
required: true,
minlength: 6,
email: true
},
phone: {
required: true,
digits: true,
minlength: 10,
maxlength: 15
},
message: {
required: true,
minlength: 6
}
},
messages: {
name: {
required: "Please enter your name",
minlength: "Minimum 2 characters",
lettersonly: "Only letters please!"
},
email: {
required: "Please enter your email address",
minlength: "Minimum 6 characters",
email: "That's an invalid email"
},
phone: {
required: "Please enter your phone number",
digits: "Only digits (no spaces)",
minlength: "Minimum 10 characters",
maxlength: "Maximum 15 characters"
},
message: {
required: "Please enter your message",
minlength: "Minimum 6 characters"
}
},
success: function(label) {
label.addClass("valid").text("Perfect!");
},
submitHandler: function(element) {
var ajaxform = $(element),
url = ajaxform.attr('action'),
type = ajaxform.attr('method'),
data = {};
$(ajaxform).find('[name="submit"]').html('[i][/i] Sending...');
ajaxform.find('[name]').each(function(index, value) {
var field = $(this),
name = field.attr('name'),
value = field.val();
data[name] = value;
});
$.ajax({
url: url,
type: type,
data: data,
success: function(response) {
if (response.type == 'success') {
$("#contactForm").before("[url=#]×[/url]" + response.text + "");
$(ajaxform).each(function() {
this.reset();
$(this).find('[name="submit"]').html('[i][/i] Send');
}).find('.valid').each(function() {
$(this).remove('label.valid');
})
} else if (response.type == 'error') {
$("#contactForm").before("[url=#]×[/url]" + response.text + "");
$(ajaxform).find('[name="submit"]').html('[i][/i] Send');
}
}
});
return false;
}
});
< /code>
Проблема в том, что всякий раз, когда я нажимаю кнопку «Отправить». Действие не выполняется. Ни положительный, ни отрицательный. Ничего не происходит.>
Подробнее здесь: [url]https://stackoverflow.com/questions/45309852/ajax-action-not-happening-after-clicking-the-submit-button[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия