Javascript AJAX表单未显示成功或错误消息
我以前问过这个问题,但我还是不明白。我做了一些改变,但不幸的是我仍然没有得到任何运气。表单本身可以工作,但当用户尝试发送表单时,它也应该显示错误或成功消息。但是它没有显示消息,而是发送表单而不显示它。看看我的代码: HTMLJavascript AJAX表单未显示成功或错误消息,javascript,php,jquery,html,ajax,Javascript,Php,Jquery,Html,Ajax,我以前问过这个问题,但我还是不明白。我做了一些改变,但不幸的是我仍然没有得到任何运气。表单本身可以工作,但当用户尝试发送表单时,它也应该显示错误或成功消息。但是它没有显示消息,而是发送表单而不显示它。看看我的代码: HTML <form action="" method="POST"> <ul class="form-style-1"> <li> <input type="text" id="mail-n
<form action="" method="POST">
<ul class="form-style-1">
<li>
<input type="text" id="mail-name" name="name" class="field-divided" maxlength="15" placeholder="Voornaam *" /> <input type="text" id="mail-lastname" name="lastname" class="field-divided" maxlength="15" placeholder="Achternaam" >
</li>
<li>
<input type="email" id="mail-email" name="email" placeholder="E-mail *" class="field-long" maxlength="40" >
</li>
<li>
<input type ="text" id="mail-phone" name="phone" placeholder="Telefoonnummer" class="field-long" maxlength = "15">
</li>
<li>
<select name="subject" id="mail-subject" class="field-select" >
<option disabled value="" selected hidden >--Onderwerp-- *</option>
<option value="Kennismakingsgesprek">Kennismakingsgesprek</option>
<option value="Meer informatie">Meer informatie</option>
<option value="activiteit">Aanmelding activiteit</option>
<option value="Vraag/klacht">Vraag/klacht</option>
<option value="Contact">Overig</option>
</select>
</li>
<li>
<textarea name="information" id="mail-information" placeholder =" Je bericht *"class="field-long field-textarea" maxlength="2000"></textarea>
</li>
<button class="mail-submit" id="mail-submit" type="submit" name="submit">Send e-mail</button>
<span class="form-message"></span>
</ul>
</form>
PHP(位于PHP文件中)
if(isset($\u POST['submit'])){
$email_to=“#””;
$email_subject=“#””;
$name=$_POST['name'];
$lastname=$\u POST['lastname'];
$email=$_POST['email'];
$phone=$_POST['phone'];
$subject=$_POST['subject'];
$information=$_POST['information'];
$errorEmpty=false;
$errorEmail=false;
if(空($name)| |空($email)| |空($subject)| |空($information)){
回音“欢迎光临!”;
$errorEmpty=true;
}
elseif(!filter\u var($email,filter\u VALIDATE\u email)){
回音“Geefeen geldig电子邮件!”;
$errorEmail=true;
}
否则{
$formcontent=“Naam:$name\n\n Achternaam:$lastname\n\n Email:$Email\n\n Telefoon:$phone\n\n Onderwerp:$subject\n\n Informatie:$information”;
$mailheader=“From:”..$\u POST[“email”]。“\r\n”;
$headers=“From:”.htmlspecialchars($\u POST['name'])。“\r\n”;
$headers.=“回复:”.$\u POST['email']。“\r\n”;
$headers.=“MIME版本:1.0\r\n”;
$headers.=“内容类型:text/html;字符集=ISO-8859-1\r\n”;
邮件($email_to,$subject,$formcontent,$mailheader);
echo“电子邮件已发送!”;
}
}
这是我使用的AJAX脚本:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
这里发生了两个javascript事件。您有表单提交事件,该事件由type=“submit”按钮触发。该按钮触发了一个
onclick
事件。相反,您可以向表单中添加一个Id,并将其列在一个onSubmit
事件中,例如
<form id="email-form">
...
</form>
$("#the-form").submit(function(event) {
event.preventDefault();
...
});
...
$(“#表格”)。提交(功能(事件){
event.preventDefault();
...
});
然后您应该能够发出
ajax
请求 这里发生了两个javascript事件。您有表单提交事件,该事件由type=“submit”按钮触发。该按钮触发了一个onclick
事件。相反,您可以向表单中添加一个Id,并将其列在一个onSubmit
事件中,例如
<form id="email-form">
...
</form>
$("#the-form").submit(function(event) {
event.preventDefault();
...
});
...
$(“#表格”)。提交(功能(事件){
event.preventDefault();
...
});
然后您应该能够发出
ajax
请求 这不可能在一个文件中。你需要两个
像这样试试。。。如果有新问题,请编辑您的问题
请注意提交事件处理程序。。。而不是单击处理程序
page.html:
<form action="" method="POST">
<ul class="form-style-1">
<li>
<input type="text" id="mail-name" name="name" class="field-divided" maxlength="15" placeholder="Voornaam *" /> <input type="text" id="mail-lastname" name="lastname" class="field-divided" maxlength="15" placeholder="Achternaam" >
</li>
<li>
<input type="email" id="mail-email" name="email" placeholder="E-mail *" class="field-long" maxlength="40" >
</li>
<li>
<input type ="text" id="mail-phone" name="phone" placeholder="Telefoonnummer" class="field-long" maxlength = "15">
</li>
<li>
<select name="subject" id="mail-subject" class="field-select" >
<option disabled value="" selected hidden >--Onderwerp-- *</option>
<option value="Kennismakingsgesprek">Kennismakingsgesprek</option>
<option value="Meer informatie">Meer informatie</option>
<option value="activiteit">Aanmelding activiteit</option>
<option value="Vraag/klacht">Vraag/klacht</option>
<option value="Contact">Overig</option>
</select>
</li>
<li>
<textarea name="information" id="mail-information" placeholder =" Je bericht *"class="field-long field-textarea" maxlength="2000"></textarea>
</li>
<button class="mail-submit" id="mail-submit" type="submit" name="submit">Send e-mail</button>
<span class="form-message"></span>
</ul>
</form>
<script>
$("form").on("submit",function(event){ // Submit handler!
event.preventDefault();
var name = $("#mail-name").val();
var lastname = $("#mail-lastname").val();
var email = $("#mail-email").val();
var phone = $("#mail-phone").val();
var subject = $("#mail-subject").val();
var information = $("#mail-information").val();
$.post("contact.php",
{
name: name,
lastname: lastname,
email: email,
phone: phone,
subject: subject,
information: information,
submit: "yes"
},
function(data){
$(".form-message").html( data );
}
);
});
</script>
if (isset($_POST['submit'])) {
$email_to = "#";
$email_subject = "#";
$name = $_POST['name'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$subject = $_POST['subject'];
$information = $_POST['information'];
$errorEmpty = false;
$errorEmail = false;
if (empty($name) || empty($email) || empty($subject) || empty($information)) {
echo "<span class='form-error'>Voer alle velden in!</span>";
$errorEmpty = true;
}
elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
echo "<span class='form-error'>Geef een geldig E-mail!</span>";
$errorEmail = true;
}
else {
$formcontent=" Naam: $name \n\n Achternaam: $lastname \n\n Email: $email \n\n Telefoon: $phone \n\n Onderwerp: $subject \n\n Informatie: $information";
$mailheader = "From: ".$_POST["email"]."\r\n";
$headers = "From: ". htmlspecialchars($_POST['name']) ." <" . $_POST['email'] . ">\r\n";
$headers .= "Reply-To: " . $_POST['email'] . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
mail($email_to, $subject, $formcontent, $mailheader);
echo "<span class='form-success'>E-mail has been sent!</span>";
}
}
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=yes" />
<meta charset="UTF-8" />
<!-- The above 3 meta tags must come first in the head -->
<title>Demo</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$('#submit').click(function (event) {
$.ajax({
method: 'post',
dataType: 'html',
url: 'send-email.php',
data: {
'name': $('#name').val(),
'email': $('#email').val()
},
success: function (response, textStatus, jqXHR) {
$('.messages').html(response);
},
error: function (jqXHR, textStatus, errorThrown) {
var message = 'An error occurred during your request. Please try again, or contact us.';
$('.messages').html('<div class="error">' + message + '</error>');
}
});
});
});
</script>
</head>
<body>
<div class="messages"></div>
<div class="contact-form">
<input type="text" id="name" name="name" />
<input type="email" id="email" name="email" />
<button type="button" id="submit" name="submit">
Send e-mail
</button>
</div>
</body>
</html>
<?php
$response = '';
$emailSent = FALSE;
/*
* ==========================
* Validate the posted values
* ==========================
*/
if (!isset($_POST['name']) || empty($_POST['name'])) {
$errors[] = 'Please provide a name.';
}
if (!isset($_POST['email']) || empty($_POST['email'])) {
$errors[] = 'Please provide an email.';
} elseif (!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$errors[] = 'Invalid email.';
}
/*
* ======================================
* Send the email if all values are valid
* ======================================
*/
if (!isset($errors)) {
$name = $_POST['name'];
$email = $_POST['email'];
// Send the email here, using the posted values...
$emailSent = TRUE;
}
/*
* ==============================================================
* Assign the corresponding message (with error or success class)
* ==============================================================
*/
if (isset($errors)) {
foreach ($errors as $error) {
$response .= '<div class="error">' . $error . '</div>';
}
} elseif ($emailSent) {
$response .= '<div class="success">The email was successfully sent.</div>';
}
/*
* ==================
* Print the response
* ==================
*/
echo $response;
-
-
-
-
--OnDerERP--*
肯尼斯马金斯格雷克
米尔信息
A焊接活动
Vraag/klacht
过量
-
发送电子邮件
$(“表单”)。在(“提交”上,函数(事件){//提交处理程序!
event.preventDefault();
var name=$(“#邮件名称”).val();
var lastname=$(“#邮件lastname”).val();
var email=$(“#mail email”).val();
var phone=$(“#邮件电话”).val();
var subject=$(“#邮件主题”).val();
var信息=$(“#邮件信息”).val();
$.post(“contact.php”,
{
姓名:姓名,,
lastname:lastname,
电邮:电邮,,
电话:电话,,
主题:主题,,
信息:信息,,
提交:“是”
},
功能(数据){
$(“.form message”).html(数据);
}
);
});
contact.php:
<form action="" method="POST">
<ul class="form-style-1">
<li>
<input type="text" id="mail-name" name="name" class="field-divided" maxlength="15" placeholder="Voornaam *" /> <input type="text" id="mail-lastname" name="lastname" class="field-divided" maxlength="15" placeholder="Achternaam" >
</li>
<li>
<input type="email" id="mail-email" name="email" placeholder="E-mail *" class="field-long" maxlength="40" >
</li>
<li>
<input type ="text" id="mail-phone" name="phone" placeholder="Telefoonnummer" class="field-long" maxlength = "15">
</li>
<li>
<select name="subject" id="mail-subject" class="field-select" >
<option disabled value="" selected hidden >--Onderwerp-- *</option>
<option value="Kennismakingsgesprek">Kennismakingsgesprek</option>
<option value="Meer informatie">Meer informatie</option>
<option value="activiteit">Aanmelding activiteit</option>
<option value="Vraag/klacht">Vraag/klacht</option>
<option value="Contact">Overig</option>
</select>
</li>
<li>
<textarea name="information" id="mail-information" placeholder =" Je bericht *"class="field-long field-textarea" maxlength="2000"></textarea>
</li>
<button class="mail-submit" id="mail-submit" type="submit" name="submit">Send e-mail</button>
<span class="form-message"></span>
</ul>
</form>
<script>
$("form").on("submit",function(event){ // Submit handler!
event.preventDefault();
var name = $("#mail-name").val();
var lastname = $("#mail-lastname").val();
var email = $("#mail-email").val();
var phone = $("#mail-phone").val();
var subject = $("#mail-subject").val();
var information = $("#mail-information").val();
$.post("contact.php",
{
name: name,
lastname: lastname,
email: email,
phone: phone,
subject: subject,
information: information,
submit: "yes"
},
function(data){
$(".form-message").html( data );
}
);
});
</script>
if (isset($_POST['submit'])) {
$email_to = "#";
$email_subject = "#";
$name = $_POST['name'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$subject = $_POST['subject'];
$information = $_POST['information'];
$errorEmpty = false;
$errorEmail = false;
if (empty($name) || empty($email) || empty($subject) || empty($information)) {
echo "<span class='form-error'>Voer alle velden in!</span>";
$errorEmpty = true;
}
elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
echo "<span class='form-error'>Geef een geldig E-mail!</span>";
$errorEmail = true;
}
else {
$formcontent=" Naam: $name \n\n Achternaam: $lastname \n\n Email: $email \n\n Telefoon: $phone \n\n Onderwerp: $subject \n\n Informatie: $information";
$mailheader = "From: ".$_POST["email"]."\r\n";
$headers = "From: ". htmlspecialchars($_POST['name']) ." <" . $_POST['email'] . ">\r\n";
$headers .= "Reply-To: " . $_POST['email'] . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
mail($email_to, $subject, $formcontent, $mailheader);
echo "<span class='form-success'>E-mail has been sent!</span>";
}
}
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=yes" />
<meta charset="UTF-8" />
<!-- The above 3 meta tags must come first in the head -->
<title>Demo</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$('#submit').click(function (event) {
$.ajax({
method: 'post',
dataType: 'html',
url: 'send-email.php',
data: {
'name': $('#name').val(),
'email': $('#email').val()
},
success: function (response, textStatus, jqXHR) {
$('.messages').html(response);
},
error: function (jqXHR, textStatus, errorThrown) {
var message = 'An error occurred during your request. Please try again, or contact us.';
$('.messages').html('<div class="error">' + message + '</error>');
}
});
});
});
</script>
</head>
<body>
<div class="messages"></div>
<div class="contact-form">
<input type="text" id="name" name="name" />
<input type="email" id="email" name="email" />
<button type="button" id="submit" name="submit">
Send e-mail
</button>
</div>
</body>
</html>
<?php
$response = '';
$emailSent = FALSE;
/*
* ==========================
* Validate the posted values
* ==========================
*/
if (!isset($_POST['name']) || empty($_POST['name'])) {
$errors[] = 'Please provide a name.';
}
if (!isset($_POST['email']) || empty($_POST['email'])) {
$errors[] = 'Please provide an email.';
} elseif (!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$errors[] = 'Invalid email.';
}
/*
* ======================================
* Send the email if all values are valid
* ======================================
*/
if (!isset($errors)) {
$name = $_POST['name'];
$email = $_POST['email'];
// Send the email here, using the posted values...
$emailSent = TRUE;
}
/*
* ==============================================================
* Assign the corresponding message (with error or success class)
* ==============================================================
*/
if (isset($errors)) {
foreach ($errors as $error) {
$response .= '<div class="error">' . $error . '</div>';
}
} elseif ($emailSent) {
$response .= '<div class="success">The email was successfully sent.</div>';
}
/*
* ==================
* Print the response
* ==================
*/
echo $response;
if(isset($\u POST['submit'])){
$email_to=“#””;
$email_subject=“#””;
$name=$_POST['name'];
$lastname=$\u POST['lastname'];
$email=$_POST['email'];
$phone=$_POST['phone'];
$subject=$_POST['subject'];
$information=$_POST['information'];
$errorEmpty=false;
$errorEmail=false;
if(空($name)| |空($email)| |空($subject)| |空($information)){
回音“欢迎光临!”;
$errorEmpty=true;
}
elseif(!filter\u var($email,filter\u VALIDATE\u email)){
回音“Geefeen geldig电子邮件!”;
$errorEmail=true;
}
否则{
$formcontent=“Naam:$name\n\n Achternaam:$lastname\n\n Email:$Email\n\n Telefoon:$phone\n\n Onderwerp:$subject\n\n Informatie:$information”;
$mailheader=“From:”..$\u POST[“email”]。“\r\n”;
$headers=“From:”.htmlspecialchars($\u POST['name'])。“\r\n”;
$headers.=“回复:”.$\u POST['email']。“\r\n”;
$headers.=“MIME版本:1.0\r\n”;
$headers.=“内容类型:text/html;字符集=ISO-8859-1\r\n”;
邮件($email_to,$subject,$formcontent,$mailheader);
echo“电子邮件已发送!”;
}
}
不能放在一个文件中。你需要两个
像这样试试。。。如果有新问题,请编辑您的问题
请注意提交事件处理程序。。。而不是单击处理程序
page.html:
<form action="" method="POST">
<ul class="form-style-1">
<li>
<input type="text" id="mail-name" name="name" class="field-divided" maxlength="15" placeholder="Voornaam *" /> <input type="text" id="mail-lastname" name="lastname" class="field-divided" maxlength="15" placeholder="Achternaam" >
</li>
<li>
<input type="email" id="mail-email" name="email" placeholder="E-mail *" class="field-long" maxlength="40" >
</li>
<li>
<input type ="text" id="mail-phone" name="phone" placeholder="Telefoonnummer" class="field-long" maxlength = "15">
</li>
<li>
<select name="subject" id="mail-subject" class="field-select" >
<option disabled value="" selected hidden >--Onderwerp-- *</option>
<option value="Kennismakingsgesprek">Kennismakingsgesprek</option>
<option value="Meer informatie">Meer informatie</option>
<option value="activiteit">Aanmelding activiteit</option>
<option value="Vraag/klacht">Vraag/klacht</option>
<option value="Contact">Overig</option>
</select>
</li>
<li>
<textarea name="information" id="mail-information" placeholder =" Je bericht *"class="field-long field-textarea" maxlength="2000"></textarea>
</li>
<button class="mail-submit" id="mail-submit" type="submit" name="submit">Send e-mail</button>
<span class="form-message"></span>
</ul>
</form>
<script>
$("form").on("submit",function(event){ // Submit handler!
event.preventDefault();
var name = $("#mail-name").val();
var lastname = $("#mail-lastname").val();
var email = $("#mail-email").val();
var phone = $("#mail-phone").val();
var subject = $("#mail-subject").val();
var information = $("#mail-information").val();
$.post("contact.php",
{
name: name,
lastname: lastname,
email: email,
phone: phone,
subject: subject,
information: information,
submit: "yes"
},
function(data){
$(".form-message").html( data );
}
);
});
</script>
if (isset($_POST['submit'])) {
$email_to = "#";
$email_subject = "#";
$name = $_POST['name'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$subject = $_POST['subject'];
$information = $_POST['information'];
$errorEmpty = false;
$errorEmail = false;
if (empty($name) || empty($email) || empty($subject) || empty($information)) {
echo "<span class='form-error'>Voer alle velden in!</span>";
$errorEmpty = true;
}
elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
echo "<span class='form-error'>Geef een geldig E-mail!</span>";
$errorEmail = true;
}
else {
$formcontent=" Naam: $name \n\n Achternaam: $lastname \n\n Email: $email \n\n Telefoon: $phone \n\n Onderwerp: $subject \n\n Informatie: $information";
$mailheader = "From: ".$_POST["email"]."\r\n";
$headers = "From: ". htmlspecialchars($_POST['name']) ." <" . $_POST['email'] . ">\r\n";
$headers .= "Reply-To: " . $_POST['email'] . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
mail($email_to, $subject, $formcontent, $mailheader);
echo "<span class='form-success'>E-mail has been sent!</span>";
}
}
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=yes" />
<meta charset="UTF-8" />
<!-- The above 3 meta tags must come first in the head -->
<title>Demo</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$('#submit').click(function (event) {
$.ajax({
method: 'post',
dataType: 'html',
url: 'send-email.php',
data: {
'name': $('#name').val(),
'email': $('#email').val()
},
success: function (response, textStatus, jqXHR) {
$('.messages').html(response);
},
error: function (jqXHR, textStatus, errorThrown) {
var message = 'An error occurred during your request. Please try again, or contact us.';
$('.messages').html('<div class="error">' + message + '</error>');
}
});
});
});
</script>
</head>
<body>
<div class="messages"></div>
<div class="contact-form">
<input type="text" id="name" name="name" />
<input type="email" id="email" name="email" />
<button type="button" id="submit" name="submit">
Send e-mail
</button>
</div>
</body>
</html>
<?php
$response = '';
$emailSent = FALSE;
/*
* ==========================
* Validate the posted values
* ==========================
*/
if (!isset($_POST['name']) || empty($_POST['name'])) {
$errors[] = 'Please provide a name.';
}
if (!isset($_POST['email']) || empty($_POST['email'])) {
$errors[] = 'Please provide an email.';
} elseif (!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$errors[] = 'Invalid email.';
}
/*
* ======================================
* Send the email if all values are valid
* ======================================
*/
if (!isset($errors)) {
$name = $_POST['name'];
$email = $_POST['email'];
// Send the email here, using the posted values...
$emailSent = TRUE;
}
/*
* ==============================================================
* Assign the corresponding message (with error or success class)
* ==============================================================
*/
if (isset($errors)) {
foreach ($errors as $error) {
$response .= '<div class="error">' . $error . '</div>';
}
} elseif ($emailSent) {
$response .= '<div class="success">The email was successfully sent.</div>';
}
/*
* ==================
* Print the response
* ==================
*/
echo $response;
-
-
-
-
--OnDerERP--*
肯尼斯马金斯格雷克
米尔信息
A焊接活动
Vraag/klacht
过量
-
发送电子邮件
$(“表单”)。在(“提交”上,函数(事件){//提交处理程序!
event.preventDefault();
var name=$(“#邮件名称”).val();
var lastname=$(“#邮件lastname”).val();
var email=$(“#mail email”).val();
var phone=$(“#邮件电话”).val();
变量主题=$(“#