我在页脚网站上发了一份订阅时事通讯的表格。代码是:
<h4>Newsletter Subscribe:</h4>
<form method="POST" action="<?php URL::show("Newsletter", "create") ?>">
<div class="col-md-12">
<div class="input-group">
<input type="text" class="form-control" name="subscriber" placeholder="john.doe@domain.com">
<input type="submit" class="btn btn-primary" value="Subscribe">
</div>
</div>
</form>
当我点击提交按钮时,将调用NewsletterController中的createAction方法,如下所示:
public function createAction(){
if($_SERVER["REQUEST_METHOD"] == "POST"){
$validation = true;
$error = "";
if(trim($_POST["subscriber"]) == ""){
$validation = false;
$error = "The field 'subscriber' is required!";
}
if(trim($_POST["subscriber"]) != ""){
if(!preg_match('/^([a-z0-9]+([_\.\-]{1}[a-z0-9]+)*){1}([@]){1}([a-z0-9]+([_\-]{1}[a-z0-9]+)*)+(([\.]{1}[a-z]{2,6}){0,3}){1}$/i', $_POST["subscriber"])){
$validation = false;
$error = "The field 'subscriber' must contain a valid input!";
}
}
if($validation){
$checkSubscriber = $this->newsletterRepository->checkSubscriber($_POST["subscriber"]);
if(!$checkSubscriber){
$newsletter = new Newsletter();
$newsletter->setEmail($_POST["subscriber"]);
$result = $this->newsletterRepository->insert($newsletter);
if(!$result){
$error = "There was an error in the newsletter
}
}else{
$error = "The e-mail already registered for newsletter!";
}
}
}else{
URL::redirect("Restaurants", "listAll");
}
}
现在,在我完成所有验证并在数据库中插入数据后,我不想去另一个网页,如果一切正常,我想显示带有确认消息的弹出窗口或者如果有的话会显示错误消息是一个错误。像这样:
<div class="modal fade bd-example-modal-lg" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Newsletter Subscription</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div class="container-fluid">
<?php if($result): ?>
<p style="color:green">Newsletter subscription successfuly!</p>
<?php elseif($error): ?>
<p style="color:red"><?php echo $error; ?></p>
<?php endif; ?>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
但是我不知道在完成所有操作和验证后如何触发模态。有人可以帮忙吗?
答案 0 :(得分:0)
我能想到的最简单的方法之一就是使用javascript。验证成功完成后,您可以调用这样的javascript函数:
echo "<script type="text/javascript">the_function_you_want_to_call_from_javascript_file();
</script>";
在您的javascript文件中,您可以调用模态并进行自定义。 希望它有所帮助:)