Sweetalert(Ajx-PHP表单)仅在输入字段为空时显示成功(alert)

Sweetalert(Ajx-PHP表单)仅在输入字段为空时显示成功(alert),php,jquery,forms,email,sweetalert2,Php,Jquery,Forms,Email,Sweetalert2,我尝试了很多不同的方法,但是没有运气。每次我提交表单时,即使在空字段上也会触发成功swal警报 这是我的HTML <!-- Content --> <div class="content"> <form role="form" id="form" action="assets/php/mail.php" method="post"> <div class="row 50%"> <div cla

我尝试了很多不同的方法,但是没有运气。每次我提交表单时,即使在空字段上也会触发成功swal警报

这是我的HTML

<!-- Content -->
<div class="content">
    <form role="form" id="form" action="assets/php/mail.php" method="post">

        <div class="row 50%">
            <div class="6u 12u(mobile)">
                <label for="name"></label>
                <input id="name" type="text" name="name" placeholder="Name"/>
                <span></span>
            </div>
            <div class="6u 12u(mobile)">
                <label for="email"></label>
                <input id="email" type="email" name="email" placeholder="Email"/>
            </div>
        </div>

        <div class="row 50%">
            <div class="12u">
                <textarea id="message" name="message" placeholder="Message" rows="7"></textarea>
            </div>
        </div>

        <div class="row">
            <div class="12u">
                <ul class="buttons">
                    <li><input id="submit-button" type="submit" class="special" value="Send" /></li>
                </ul>
            </div>
        </div>

    </form>
</div>

这是JS。提交表单后,我将使用它清除表单,这是我让表单工作的唯一方法。

$(document).ready(function(){
$('#form').on('submit',function(e) {  //Don't foget to change the id form
$.ajax({
  url:'assets/php/mail.php', //===PHP file name====
  data:$(this).serialize(),
  type:'POST',
  success:function(data){
    console.log(data);
    //Success Message == 'Title', 'Message body', Last one leave as it is
    swal({
      type: 'success',
      title: 'Submmited',
      text: 'Your message has been send!'
    }).then((result) => {
        if (result.value) {
          $("#name").val("");
            $("#email").val("");
            $("#message").val("");
          swal()
         }
      });
  },
  error:function(data){
    //Error Message == 'Title', 'Message body', Last one leave as it is
    swal({
      type: 'error',
      title: 'Oops...',
      text: 'Something went wrong!',
      footer: '<a href>Why do I have this issue?</a>'
    })
  }
});

e.preventDefault(); //This is to Avoid Page Refresh and Fire the Event "Click"

});
});
$(文档).ready(函数(){
$('#form')。在('submit')上,函数(e){//不要忘记更改id表单
$.ajax({
url:'assets/php/mail.php',//==php文件名====
数据:$(this).serialize(),
类型:'POST',
成功:功能(数据){
控制台日志(数据);
//成功消息=='Title','messagebody',最后一条消息保持原样
游泳({
键入:“成功”,
标题:“子提交”,
文字:“您的邮件已发送!”
})。然后((结果)=>{
if(result.value){
$(“#名称”).val(“”);
$(“#email”).val(“”);
$(“#消息”).val(“”);
swal()
}
});
},
错误:函数(数据){
//错误消息=='Title','messagebody',最后一条消息保持原样
游泳({
键入:“错误”,
标题:“哎呀……”,
短信:“出了点问题!”,
页脚:“”
})
}
});
e、 preventDefault();//这是为了避免页面刷新并触发事件“单击”
});
});
这是我的PHP,我为此使用了很多不同的PHP代码,但这是最适合我的代码。

$name = strip_tags(trim($_POST["name"]));
$name = str_replace(array("\r","\n"),array(" "," "),$name);
$email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
$message = trim($_POST["message"]);

// Check the data.
if (empty($name) OR empty($message) OR !filter_var($email, FILTER_VALIDATE_EMAIL)) {
    header("Location: ../../contact.html");
    exit;
}

$recipient = "php_test@knobcode.com";

// Set the email subject.
$subject = "New contact from $name";

// Build the email content.
$email_content = "Name: $name\n";
$email_content .= "Email: $email\n\n";
$email_content .= "Message:\n$message\n";


$email_headers = "From: $name <$email>";

mail($recipient, $subject, $email_content, $email_headers);

header("Location: ../../contact.html");
$name=strip_标签(trim($_POST[“name”]);
$name=str_replace(数组(“\r”、“\n”)、数组(“、”)、$name);
$email=filter\u var(trim($\u POST[“email”]),filter\u SANITIZE\u email);
$message=trim($_POST[“message”]);
//检查数据。
if(空($name)或空($message)或!filter\u var($email,filter\u VALIDATE\u email)){
标题(“位置:../../contact.html”);
出口
}
$recipient=“php_test@knobcode.com";
//设置电子邮件主题。
$subject=“来自$name的新联系人”;
//构建电子邮件内容。
$email\u content=“Name:$Name\n”;
$email\u content.=“email:$email\n\n”;
$email\u content.=“邮件:\n$Message\n”;
$email\u headers=“From:$name”;
邮件($recipient、$subject、$email\u content、$email\u headers);
标题(“位置:../../contact.html”);

尝试检查
submit
处理程序中的输入:

$('#form').on('submit',function(e) {
  e.preventDefault();
  if( $(this).find("[name='name']").val()!="" &&  // If the 3 aren't empty, it proceeds.
      $(this).find("[name='email']").val()!="" &&
      $(this).find("[name='message']").text()!=""
  ){
    // The rest of your code here... 

  }else{
    alert("Please fill the form before submitting!");  // Or use a swal...
  }
});

关于Ajax:Ajax重新请求成功时,无论发送的数据是什么,
success
回调都会触发。当Ajax请求无法完成时,
错误
回调将触发。。。比如因为错误的URL或其他原因。

在PHP中重定向到contact.html的内容是相同的。前端怎么知道字段是空的?所以我必须重定向到另一个页面?不,但是你必须有一些方法来区分成功和失败。如果你对两者的反应都是一样的,你期望它如何工作?我试图重定向到索引,但仍然是一样的。请原谅我的无知,我在这方面完全是个傻瓜。