我试图重定向,但它无法正常工作。有人可以帮帮我吗?
if(isset($_POST['submit'])){
if(send_report($_POST['id_truck'], $_GET['id'])){
echo '<script type="text/javascript">';
echo 'setTimeout(function () { swal("Success!","data successfully added","success");';
echo '}, 200); window.location.href = 'report.php' </script>';
} else {
echo '<script type="text/javascript">';
echo 'setTimeout(function () { swal("failed!","Something wrong","warning");';
echo '}, 200), ;</script>';
}
}
答案 0 :(得分:1)
您在包含在单引号中的字符串中使用单引号,将其转义:
echo '}, 200); window.location.href = \'report.php\' </script>';
修改强>
现在,当您的网页重定向时,您的sweetalert会正常工作,但您可能无法看到它。
swal()
返回Promise,当您点击sweetalert的OK按钮时将会解析,因此我们将不得不等待它重定向页面:
echo '<script type="text/javascript">';
echo 'setTimeout(function () {';
echo 'swal("Success!","data successfully added","success").then( function(val) {';
echo 'if (val == true) window.location.href = \'report.php\';';
echo '});';
echo '}, 200); </script>';