如何?:提交后会弹出甜蜜警报 - PHP代码

时间:2016-05-26 13:32:20

标签: php html css sweetalert

我刚刚发现sweetalert非常好,但是我实现它很麻烦,因为我对PHP等很陌生=)

在一个人提交电子邮件之后,如何在SAME页面上管理成功PopUp?

我将<script src="sweet-alert.min.js"></script>放在我的html文件的底部,还包括<link rel="stylesheet" href="sweet-alert.css" />

我真的不明白,在提交的东西后,如何在同一页面上给出一个sweetalert成功弹出窗口:(((

      <?php 


  $from         = 'info@blabla.at';
  $sendTo       = 'info@blabla.at';
  $subject      = 'New message from contact form';
  $fields       = array(
    'name' => 'Name',
    'surname' => 'Surname',
    'phone' => 'Phone',
    'email' => 'Email',
    'message' => 'Message'); 

  name => Text to appear in email 
  $okMessage    = sweetAlert('Congratulations!', 'Your message has been successfully sent', 'success'); // WHY NO SWEETALERT? HOW IS THE SYNTAX =)
  $errorMessage = 'There was an error while submitting the form. Please try again later';

//让我们发送

  try {
    $emailText = "You have new message from contact form\n=============================\n";

    foreach ($_POST as $key => $value) {

        if (isset($fields[$key])) {
            $emailText .= "$fields[$key]: $value\n";
        }
    }

    mail($sendTo, $subject, $emailText, "From: " . $from);

    $responseArray = array(
        'type' => 'success',
        'message' => $okMessage
        );
}
catch (\Exception $e) {
    $responseArray = array(
        'type' => 'danger',
        'message' => $errorMessage
        );
}

if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
    $encoded = json_encode($responseArray);

    header('Content-Type: application/json');

    echo $encoded;
} else {
    echo $responseArray['message'];
}

0 个答案:

没有答案