通过 javascript 提交表单时的 Bootstrap 成功警报
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32287271/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bootstrap success alert upon form submition via javascript
提问by Kerrial Beckett Newham
I've been searching the internet for hours to try to understand how to make a dismissible green alert slide down (non-modal) when a user submits a form. I'm thinking it has something to do with form validation but not sure. Please help.
我已经在互联网上搜索了几个小时,试图了解如何在用户提交表单时使可关闭的绿色警报向下滑动(非模态)。我认为它与表单验证有关,但不确定。请帮忙。
The code isn't working, because when I click submit on the form, nothing happens. The validation is not applying to the form. (the form action attribute has been left empty.) method="post"
代码不起作用,因为当我单击表单上的提交时,什么也没有发生。验证不适用于表单。(表单操作属性已留空。) method="post"
I am using a form processing script provided by the server (not sure if that makes a difference.)
我正在使用服务器提供的表单处理脚本(不确定这是否有所不同。)
so my question is: how can I "link" the submit button to send the form to the sever, then show a dismissible success alert via jQuery that will fit into a col-md-4?
所以我的问题是:如何“链接”提交按钮以将表单发送到服务器,然后通过适合 col-md-4 的 jQuery 显示可关闭的成功警报?
回答by Nana Partykar
What is happening.. when you submitted any page(form.php) to some other page (form-submit.php).. message is passed through URL using header function to form.php.. in form.php that message is picked using get method..
发生了什么.. 当您将任何页面 (form.php) 提交到其他页面 (form-submit.php) 时.. 消息通过 URL 使用标头函数传递到 form.php.. 在 form.php 中选择该消息使用get方法..
form.php
表单.php
<?
if($_GET['submitted'])
{?>
<div class="alert alert-success" role="alert">
Form Submitted Successfully
</div>
<?}?>
<form ... action='form-submit.php'>
.
.
</form>
form-submit.php
表单提交.php
// All submission query..
// at the end, after performing all necessary actions.. need to come to form.php page.. so we will use header("");
header("location:form.php?submitted=successfully");
回答by timgavin
Here's a simplified way of how you can do it within a Bootstrap modal. All we're doing here is checking if the form has been submitted via jQuery, then showing the message in a modal.
这是如何在 Bootstrap 模式中执行此操作的简化方法。我们在这里所做的只是检查表单是否已通过 jQuery 提交,然后以模式显示消息。
Here's how to incorporate that with PHP
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title></title>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0">
<link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<div id="messages" class="hide" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
<div id="messages_content"></div>
</div>
<form id="form" method="post">
<button class="btn btn-default" type="submit">Submit</button>
</form>
</div>
<script src="http://code.jquery.com/jquery.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<script>
$('#form').submit(function(e) {
$('#messages').removeClass('hide').addClass('alert alert-success alert-dismissible').slideDown().show();
$('#messages_content').html('<h4>MESSAGE HERE</h4>');
$('#modal').modal('show');
e.preventDefault();
});
</script>
</body>
</html>