我有一个html表单,在提交时会弹出一个警告框。这是通过php和javascript完成的。现在,警报只是标准警报,但我想设置警报样式。我有一个自定义的CSS和自定义javascript文件(alert.css
和alert.min.js
),它为我提供了不同的样式警报框。
此外,swal
是替换javascript alert
我遇到的问题是,在我的简单php脚本中,我似乎无法引用这些文件来弹出自定义警告框
如何正确引用自定义alert.min.js
文件?
这是(非常简单)PHP:
<?php
if (isset($_POST['submit'])) {
$to = "email@email.com";
$from = "email@whatever.com";
$name = $_POST['name'];
$email = $_POST['email'];
$subject = "Contact Form Submission";
$message = "Name - " . $name . "\n" . "Message - " . $_POST['message'];
$headers = "From:" . $from;
$headers = 'From: email@whatever.com' . "\r\n" .
'Reply-To: ' . $email . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$URL = "http://www.google.com/";
if ( ! filter_var($from, FILTER_VALIDATE_EMAIL)) {
echo "Please enter a valid email";
}
else {
mail($to,$subject,$message,$headers);
echo " <script src="alert.min.js">swal('Thanks!', 'We'll get back to you shortly.');</script>";
echo '<META HTTP-EQUIV="refresh" content="0;URL=' . $URL . '">';
echo "<script type='text/javascript'>document.location.href='{$URL}';</script>";
}
}
答案 0 :(得分:1)
实际上您使用的是SweetAlert。
所以,你要在html中更新你的回复,因为你也要包括sweetalert
css才能正常工作而css会转到主题部分。
像这样更新你的其他部分
else {
mail($to, $subject, $message, $headers);
echo "
<html>
<head>
<title>Document</title>
<link rel='stylesheet' href='sweetalert.css'>
</head>
<body>
<script src='sweetalert.min.js'></script>
<script>sweetAlert('Thanks!', \"We'll get back to you shortly.\");</script>
<script>document.location.href = '{$URL}';</script>
</body>
</html>";
}
答案 1 :(得分:0)
考虑一下你的php文件正在生成一个经典的HTML页面。 这是一个好习惯,首先编写所需的HTML文件(并测试它),然后让PHP生成它。
以下是生成的HTML应该如何(对于脚本部分!)。
<script src="alert.min.js"></script>
<script>function swal(arg1,arg2){alert(arg1+arg2);}
swal('Thanks!', 'We\'ll get back to you shortly.');
document.location.href='google.com';</script>
现在,要从php生成此HTML代码,让我们修改程序的最后一部分:
if (!filter_var($from, FILTER_VALIDATE_EMAIL)) {
echo "Please enter a valid email";
} else {
mail($to,$subject,$message,$headers);
echo "
<META HTTP-EQUIV='refresh' content='0;URL=$URL'>
<script src='alert.min.js'></script>
<script>
swal('Thanks!', 'We\'ll get back to you shortly.');
document.location.href='{$URL}'
</script>";
}
HTML测试页:
<script src="alert.min.js"></script>
<script>function swal(arg1,arg2){alert(arg1+arg2);}</script>
<button type="button" onclick="swal('Thanks!', 'We\'ll get back to you shortly.')">swal Test</button>