[英]How to display error in modal form using ajax and php if validation fail while redirecting to other page if success
[英]How to display ALERT massage AFTER redirecting to other page using php and jscript?
问题很简单,
成功提交表单后,我有一个表单提交,我想在下一页打印“表单成功提交!”的警报消息。 ?
使用PHP和JScript
保存表单后,您可以使用以下命令在带有警报脚本的页面上重定向用户:
// PHP
header('Location: http://www.example.com/path_to_page');
然后在该页面上,您可以添加js
// Js (include jquery before it)
<script>
$(document).ready(function(){
alert('Form successfully submitted!');
});
</script>
如果您不使用jQuery,请使用:
<body onload="alert_call()">
<!-- your page content -->
<script>
function alert_call(){
alert('Form successfully submitted!');
}
</script>
</body>
我从Laravel(称为Flash消息)中学到了这种方法。 基本上,您在会话中设置了一些变量(您的命名选择)
//from your originating page
<?php
$_SESSION["flash"] = array();
$_SESSION["flash"]["message"] = "Your message";
$_SESSION["flash"]["status"] = "error";
?>
<?php
//to your destination page
if(isset($_SESSION["flash"])){
echo $_SESSION["flash"]["message"];
//or
echo "<script>alert('{$_SESSION["flash"]["message"]}');</script>";
unset($_SESSION["flash"]);
}
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.