避免用户两次输入相同记录的最佳方法是什么。我的插件中有以下内容
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>";
#1
你可以在AJAX请求之前禁用”提交”按钮, 并在收到服务器的成功响应后向用户显示一条消息。
由于你已经在使用引导程序, 因此可以将引导程序警报用于友好的成功消息。
例如, 包含类似
<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错误回调中, 你应该再次启用该按钮, 并向用户显示错误消息(例如, 带有alert-error的错误消息)。
评论前必须登录!
注册