避免用户两次输入相同记录的最佳方法是什么。我的插件中有以下内容
jQuery(document).ready(function(){
jQuery("#submit").click(function(){
var points = jQuery("#points]").val();
jQuery.ajax({
type: 'POST',
url: MyAjax.ajaxurl,
data: {"action": "updateRedeemPoints", "points":points},
success: function(data){
alert(data);
}
});
});
我的功能只是张贴到我想要阻止它插入数据库两次,但它应该产生一个友好的消息框比javascript默认,或者是否可以使用引导通知。
wp_localize_script( 'gogreen', 'MyAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php')));
function updateRedeemPoints(){
$userId = get_current_user_id();
$mykey = 'player_id';
$single = true;
$playerId = get_user_meta( $userId, $mykey, $single );
$points=$_POST['points'];
global $wpdb;
$wpdb->insert(
'4hSIc_pods_player_ranking',
array(
'points_to_redeem' =>$points,
'player_id'=>$playerId,
'date_to_be_awarded' => current_time('mysql', 1),
'approved'=>'false'
));
$headers = "From: " . $form_data['name'] . " <" . $form_data['email'] . ">\n";
$headers .= "Content-Type: text/plain; charset=UTF-8\n";
$headers .= "Content-Transfer-Encoding: 8bit\n";
wp_mail( 'davidbuckleyni.co.uk', 'test', 'test', $headers );
顺便说一下,如果它有所作为,我正在使用引导模型。
echo "<div id='thanks' class='modal fade' tabindex='-1' role='dialog' aria-labelledby='myModalLabel' aria-hidden='true'>";
echo "<div class='modal-dialog'>";
echo "<div class='modal-content'>";
echo "<div class='modal-header'>";
echo "<button type='button' class='close' data-dismiss='modal' aria-hidden='true'>×</button>";
echo "<h4 class='modal-title'>Redeem Points</h4>";
echo "</div>";
echo "<form class='form-horizontal' class='contact' name='redemmpointsForm' id='redemmpointsForm' >";
echo " <div class='form-group'>";
echo "<h3>You may only redeem the maxium points of : <?php echo $maxpoints;?></h3>";
echo "<input type='hidden' name='playerid' value='<?php echo $playerId;;?>' />";
echo "<input type='number' valuemax='<?php echo $maxpoints;?>' name='points' id='points' class='form-control' placeholder='How many points do you wish to redeem.' />";
echo "<label class='control-label col-md-4' for='Comments'>Comments?</label>";
echo "<input type='text' name='comments' />";
echo " </div>";
echo " <div class='form-group'>";
echo " <div class='col-md-6'>";
echo " <input type='button' class='btn btn-success' name='submit' id='submit' Text='Submit'>";
echo " <a href='#' class='btn' data-dismiss='modal'>Close</a>";
echo " </div>";
echo " </div>";
echo "</form>";
echo " </div>";
echo " </div>";
echo " </div>";
答案 0 :(得分:1)
您可以在AJAX请求之前禁用提交按钮,并在收到服务器的成功响应时向用户显示消息。
由于您已经使用了bootstrap,因此您可以使用Bootstrap警报来获取友好的成功消息。
例如,包含类似
的内容<div class="alert alert-success alert-dismissible" id="points-success" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
<strong>Success!</strong> Great! Your points have been redeemed.
</div>
在您的表单中,默认隐藏在css中,并在Ajax成功回调中显示它。
jQuery(document).ready(function(){
jQuery("#submit").click(function(){
var points = jQuery("#points]").val();
jQuery(this).prop("disabled", true);
jQuery.ajax({
type: 'POST',
url: MyAjax.ajaxurl,
data: {"action": "updateRedeemPoints", "points":points},
success: function(data){
jQuery("#points-success").show();
}
});
});
在Ajax错误回调中,您应该再次启用该按钮,并向用户显示错误消息(例如,警告错误)。