【问题标题】:How to display php alert message in html using sweetalert如何使用 sweetalert 在 html 中显示 php 警报消息
【发布时间】:2020-12-21 05:12:34
【问题描述】:

这是我的联系表格。
success/fail 提交表单后,我需要在 sweetalert 中显示消息。错误/成功消息在 php alert 中。

<form class="contact-form" action="" method="post" id="form_id">
    <div class="form-group">
         <input type="text" name="name" class="form-control" placeholder="Full Name" required>
    </div>
    <div class="form-group">
        <input type="number" name="number" class="form-control" placeholder="Phone No." required>
    </div>
    <div class="form-group">
        <input type="text" name="country" class="form-control" placeholder="Country" required>
    </div>
    <div class="form-group">
        <input type="email" name="email" class="form-control" placeholder="Your Email Id" required>
    </div>
    <div class="form-group">
        <input type="date" name="date" class="form-control" placeholder="Travel Date" required>
    </div>
    <div class="form-group">
    <textarea class="form-control" name="message" placeholder="Your Message" style="height:140px"></textarea>
    </div>
    <button type="submit" class="btn btn-primary" name="submit" value="send">SEND MESSAGE</button>
</form>

这是我的php 代码

  • 用于发送邮件
use PHPMailer\PHPMailer\PHPMailer;
require_once 'phpmailer/Exception.php';
require_once 'phpmailer/PHPMailer.php';
require_once 'phpmailer/SMTP.php';

$mail = new PHPMailer(true);

$alert = '';
if(isset($_POST['submit'])){
  $name = $_POST['name'];
  $email = $_POST['email'];
  $number = $_POST['number'];
  $country = $_POST['country'];
  $message = $_POST['message'];
  $package=$_POST['package'];
  $date=$_POST['date'];

  try{
    $mail->isSMTP();
    $mail->Host = 'smtp.gmail.com';
    $mail->SMTPAuth = true;
    $mail->Username = ''; // Gmail address which you want to use as SMTP server
    $mail->Password = ''; // Gmail address Password
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
    $mail->Port = '587';

    $mail->setFrom(''); // Gmail address which you used as SMTP server
    $mail->addAddress(''); // Email address where you want to receive emails (you can use any of your gmail address including the gmail address which you used as SMTP server)

    $mail->isHTML(true);
    $mail->Subject = 'Message Received';
    $mail->Body = "<h3>Name : $name <br><br>Phone No: $number <br><br> Country: $country <br><br> Email: $email <br><br>Arrival Date: $date <br><br>Package: $package <br><br> Message: $message</h3>";

    $mail->send();
    $alert = 'alert-success';
  } catch (Exception $e){
    $alert = 'alert-error';
  }     
}

这是我的javascript 代码

  • swal 警报
  • alert-success检查
success:function(alert) {
    if(alert == 'alert-success'){
        swal("Good job!", "You clicked the button!", "success");
    }
    else swal("Something is missing!!!!!", "Try again","error");
}

【问题讨论】:

  • 您是否使用 ajax 发送表单提交?
  • 不,我需要使用ajax发送表单吗?
  • 我正在接收邮件,但我需要在完成后显示警报消息
  • 检查我的答案

标签: javascript php html sweetalert


【解决方案1】:

如果不使用 ajax,你可以这样做:

 <script>
 var alert = '<?php echo $alert;?>';        
 if(alert == 'alert-success'){
     alert('yes');//for test 
      swal("Good job!", "You clicked the button!", "success");
 }
 else if(alert=='alert-error'){
       alert('no');//for test
       swal("Something is missing!!!!!", "Try again","error");
   }
 </script>

【讨论】:

  • 没有..什么都没发生,但我正在接收邮件
  • 您必须将 php 脚本放在与表单相同的文件中。其次,我更新了问题并添加了警报,可能是 swal() 引发了一些错误,请检查您的控制台是否有错误。
  • 我有 在文件中
  • 也可以。现在只需检查您的控制台是否抛出一些错误。或尝试打印您的 以检查它包含的调试内容。在java脚本之前
  • ----显示“alert-success”消息
【解决方案2】:

你应该使用 ajax 来进行这个甜蜜的警报操作。

$.ajax({
  .... other settings you already have
  error: function (xhr, ajaxOptions, thrownError) {
    swal("success!", "Form has been submitted", "success");
  }
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-30
    • 2011-09-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多