所以我遇到了问题,如果我点击足够快的subnmit按钮,我的表单就会被提交几次。我怎么才能阻止这一切?令牌是自动添加的,但我想这一点也没有帮助。表格示例:
<div class="row padding-10">
{!! Form::open(array('class' => 'form-horizontal margin-top-10')) !!}
<div class="form-group">
{!! Form::label('title', 'Title', ['class' => 'col-md-1 control-label padding-right-10']) !!}
<div class="col-md-offset-0 col-md-11">
{!! Form::text('title', null, ['class' => 'form-control']) !!}
</div>
</div>
<div class="form-group">
{!! Form::label('body', 'Body', ['class' => 'col-md-1 control-label padding-right-10']) !!}
<div class="col-md-offset-0 col-md-11">
{!! Form::textarea('body', null, ['class' => 'form-control']) !!}
</div>
</div>
<div class="col-md-offset-5 col-md-3">
{!! Form::submit('Submit News', ['class' => 'btn btn-primary form-control']) !!}
</div>
{!! Form::close() !!}
</div>我的NewsController存储方法:
public function store()
{
$validator = Validator::make($data = Input::all(), array(
'title' => 'required|min:8',
'body' => 'required|min:8',
));
if ($validator->fails())
{
return Redirect::back()->withErrors($validator)->withInput();
}
News::create($data);
return Redirect::to('/news');
}发布于 2014-12-29 02:55:40
一种方法是对按钮使用单击处理程序,首先禁用按钮,然后提交表单。
<script>
function submitForm(btn) {
// disable the button
btn.disabled = true;
// submit the form
btn.form.submit();
}
</script>
<input id="submitButton" type="button" value="Submit" onclick="submitForm(this);" />发布于 2014-12-29 04:11:05
使用PHP sessions将会话变量(例如,$_SESSION['posttimer'])设置为post上的当前时间戳。在实际处理PHP中的表单之前,检查$_SESSION['posttimer']变量是否存在,并检查某个时间戳差异(IE: 2秒)。这样,您可以很容易地筛选出多个提交。
// form.html
<form action="foo.php" method="post">
<input type="text" name="bar" />
<input type="submit" value="Save">
</form>
// foo.php
if (isset($_POST) && !empty($_POST))
{
if (isset($_SESSION['posttimer']))
{
if ( (time() - $_SESSION['posttimer']) <= 2)
{
// less then 2 seconds since last post
}
else
{
// more than 2 seconds since last post
}
}
$_SESSION['posttimer'] = time();
}原始员额
How to prevent multiple inserts when submitting a form in PHP?
发布于 2020-02-17 00:42:59
也要提交按钮的值,防止双重形式的提交?
如果您正在使用类型为submit的按钮,并且希望同时提交按钮的值,如果该按钮被禁用,则可以设置一个表单数据属性并随后进行测试。
// Add class disableonsubmit to your form
$(document).ready(function () {
$('form.disableonsubmit').submit(function(e) {
if ($(this).data('submitted') === true) {
// Form is already submitted
console.log('Form is already submitted, waiting response.');
// Stop form from submitting again
e.preventDefault();
} else {
// Set the data-submitted attribute to true for record
$(this).data('submitted', true);
}
});
});https://stackoverflow.com/questions/27682830
复制相似问题