避免用户两次输入相同记录的最佳方法是什么。我的插件中有以下内容
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='http://www.srcmini.com/<
?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='http://www.srcmini.com/#' 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">
&
times;
<
/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的错误消息)。
推荐阅读
- 如何删除自定义帖子类型网址中的slug()
- 如何将jQuery代码添加到divi主题(该功能似乎已关闭?)
- CSS和图像无法正常工作
- 编辑WordPress博客文章页面-Avada WordPress主题
- 自定义WordPress tag.php页面..试图获取标签信息
- 自定义帖子类型固定链接返回404
- WordPress父页面和子页面上的自定义CSS
- 自定义背景图片不起作用
- Shell之正则表达式之(sortuniqtrcutspliteval命令)