一、前端代码方法一 Ajax 提交订阅
php
<form id="subscribe-form" class="alert-from-box subscribe-form" method="POST">
<div><input type="text" name="email" id="sub_email" placeholder="Email address*"><p class="email-error-tips"></p></div>
<div><input type="text" name="phone" id="sub_phone" placeholder="Phone number"><p class="phone-error-tips"></p></div>
<div><button type="submit" name="sub_submit">Get My Discount</button></div>
<input type="hidden" name="url" value="<?php echo $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'] ?>">
<input type="hidden" name="comment_post_ID" value="<?php echo get_the_ID() ?>" id="comment_post_ID">
<input type="hidden"name="comment_author_IP" value="<?php echo $_SERVER['REMOTE_ADDR']; ?>" id="comment_author_IP">
</form>
<script>
jQuery(function($){
$("#subscribe-form").on("submit", function(e){
e.preventDefault();
var data = {
action: "subscribe_user",
email: $(this).find('[name="email"]').val(),
phone: $(this).find('[name="phone"]').val()
};
$("#subscribe-msg").html("提交中...");
$.post("<?php echo admin_url('admin-ajax.php'); ?>", data, function(res){
if(res.status=='success'){
$('#subscribe-form button').text(res.msg);
$('#subscribe-form')[0].reset();
} else {
$('.email-error-tips').text(res.msg).css('color','red');
}
});
});
});
</script>
二、前端代码方法二 表单提交订阅
php
<form method="post" action="<?php echo admin_url('admin-post.php'); ?>">
<input type="hidden" name="action" value="subscribe_user_form">
<label>Email:</label>
<input type="email" name="email" required>
<label>Phone (optional):</label>
<input type="text" name="phone">
<button type="submit">Subscribe</button>
</form>
三,后端安装插件(https://download.csdn.net/download/qq_39339179/92342838)
