【发布时间】:2016-07-28 04:12:21
【问题描述】:
所以我为带有提交按钮的投票页面设置了一个表单。它适用于表单和 php 代码,每次有人为特定值按下投票按钮时,它将按 1 计数。
因为我使用提交按钮来计票,所以用户可以向按钮发送垃圾邮件,因为没有什么可以阻止他们,例如有人可以按“wordpress”按钮 10 次,因此 wordpress 的值变为 +10。为了防止这种情况,我想在 Jquery 中使用禁用按钮功能。我已经设法让该函数与 jquery 代码一起工作,但不幸的是它不会处理上面的 PHP 代码(通过表单)。
问题
所以我的问题是,如何在提交表单并处理表单的 php 代码后禁用提交按钮?
AJAX
是否可以使用 AJAX 来实现?如何?我对 AJAX 的了解为 0,不知道这是否有助于解决我的问题。
<?php
$votestring = 'Klik <a href="/sofeed/uitslagsofeed.php">hier</a> om naar de resultaten te gaan';
if(isset($_POST['wordpress'])) {
$vote_wordpress = "update stemmen set value1=value1+1";
$run_wordpress = mysqli_query($dbCon, $vote_wordpress);
if ($run_wordpress){
echo 'U heeft uw stem uitgebracht op ' . $answer1 .'!<br>';
echo $votestring;
}
}
if(isset($_POST['laravel'])) {
$vote_laravel = "update stemmen set value2=value2+1";
$run_laravel = mysqli_query($dbCon, $vote_laravel);
if ($run_laravel){
echo 'U heeft uw stem uitgebracht op ' . $answer2 .'!<br>';
echo $votestring;
}
}
if(isset($_POST['html'])) {
$vote_html = "update stemmen set value3=value3+1";
$run_html = mysqli_query($dbCon, $vote_html);
if ($run_html){
echo 'U heeft uw stem uitgebracht op ' . $answer3 .'!<br>';
echo $votestring;
}
}
if(isset($_POST['css'])) {
$vote_css = "update stemmen set value4=value4+1";
$run_css = mysqli_query($dbCon, $vote_css);
if ($run_css){
echo 'U heeft uw stem uitgebracht op ' . $answer4 .'!<br>';
echo $votestring;
}
}
if(isset($_POST['bootstrap'])) {
$vote_bootstrap = "update stemmen set value5=value5+1";
$run_bootstrap = mysqli_query($dbCon, $vote_bootstrap);
if ($run_bootstrap){
echo 'U heeft uw stem uitgebracht op ' . $answer5 .'!<br>';
echo $votestring;
}
}
?>
<div class="row">
<form name="stemmen" id="formstemmen" action="stemmensofeed.php" method="post">
<div class="col-xs-12 col-sm-3 col-md-2 col-lg-1" style="margin-bottom: 10px;">
<?php if (!empty($answer1)) { echo '<input type="submit" id="wordpress" class="btn btn-primary" name="wordpress" value='.$answer1.'>'; } ?>
</div>
<div class="col-xs-12 col-sm-3 col-md-2 col-lg-1" style="margin-bottom: 10px;">
<?php if (!empty($answer2)) { echo '<input type="submit" class="btn btn-primary" name="laravel" value='.$answer2.'>'; } ?>
</div>
<div class="col-xs-12 col-sm-3 col-md-2 col-lg-1" style="margin-bottom: 10px;">
<?php if (!empty($answer3)) { echo '<input type="submit" class="btn btn-primary" name="html" value='.$answer3.'>'; } ?>
</div>
<div class="col-xs-12 col-sm-3 col-md-2 col-lg-1" style="margin-bottom: 10px;">
<?php if (!empty($answer4)) { echo '<input type="submit" class="btn btn-primary" name="css" value='.$answer4.'>'; } ?>
</div>
<div class="col-xs-12 col-sm-3 col-md-2 col-lg-1" style="margin-bottom: 10px;">
<?php if (!empty($answer5)) { echo '<input type="submit" class="btn btn-primary" name="bootstrap" value='.$answer5.'>'; } ?>
</div>
</form>
</div>
<!-- jQuery 2.2.0 -->
<script src="admin/plugins/jQuery/jQuery-2.2.0.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="bootstrap-sass/assets/javascripts/bootstrap.js"></script>
<script>
$('input#wordpress').click(function(e) {
e.preventDefault();
var aaa = $(this);
aaa.prop('disabled', true);
setTimeout(function() {
aaa.prop('disabled', false);
}, 3000);
});
</script>
编辑
$(function () {
$('form').on('submit', function (e) {
e.preventDefault();
$.ajax({
type: 'post',
url: 'stemmensofeed.php',
data: $('form').serialize(),
success: function () {
$('input').prop('disabled', true);
alert('Your vote is saved!');
}
});
});
});
所以我粘贴了一些 AJAX 代码而不是 jquery 代码。这将起作用,因为提交按钮将在提交后禁用。但是,它仍然没有处理 php 代码。
【问题讨论】:
-
在
beforeSend中设置按钮属性disabled并在success回调中删除相同的属性。 -
你也应该在
button click而不是form submit上调用你的Ajax代码,即$('input#wordpress').click( //form submit ajax code here。在这种情况下最好使用<input type="button"。
标签: javascript php jquery ajax