我想制作一个订阅者表格,我将此代码与此 javascript 一起使用
<?php
require_once(ABSPATH . WPINC . '/registration.php');
global $wpdb, $user_ID;
if($_POST){
//We shall SQL escape all inputs
$username = $wpdb->escape($_POST['username']);
if(empty($username)) {
echo "*Please enter username.";
exit();
}
$email = $wpdb->escape($_POST['email']);
if(!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/", $email)) {
echo "*Please enter a valid email.";
exit();
}
$random_password = wp_generate_password( 12, false );
$status = wp_create_user( $username, $random_password, $email );
if ( is_wp_error($status) )
echo "Username or email already exists. Please try another one.";
else {
$from = get_option('admin_email');
$headers = 'From: '.$from . "\r\n";
$subject = "Registration successful";
$msg = "Registration successful.\nYour login details\nUsername: $username\nPassword: $random_password";
wp_mail( $email, $subject, $msg, $headers );
echo "Please check your email for login details.";
}
exit();
} else {
?>
<?php
if(get_option('users_can_register')) { //Check whether user registration is enabled by the administrator
?>
<div style="width:250px;height:auto;margin-top:5px;margin-bottom:15px;background-color:#043D90; padding-top:5px;padding-bottom:5px;
border-radius:10px;-moz-border-radius:10px;-webkit-border-radius:10px; color:#fff;float:left;">
<h3 style="margin-left:20px;margin-bottom:15px;margin-top:10px;font-weight:bold;">Sign up now for the Newsletter</h3>
<table>
<form id="wp_signup_form" action="" method="post" >
<tr>
<td><label style="margin-left:10px;">Name:</label></td>
<td><input style="width:180px; height:25px; margin-bottom:5px; background-color:#efefef;border:none;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;" type="text" name="username" class="text" value="" /><br />
</td></tr>
<tr>
<td><label style="margin-left:10px;">Email:</label></td>
<td><input style="width:180px; height:25px; margin-bottom:5px; background-color:#efefef;border:none;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;"type="text" name="email" class="text" value="" /> <br />
</td></tr></table>
<div id="result" style="margin-left:10px; color:#111111;font-weight:bold;"></div> <!-- To hold validation results -->
<div><input style="margin-right:20px;margin-top:10px;padding:5px; float:right;" type="submit" id="submitbtn" name="submit" value="Sign Up" /></div>
</form>
</div>
<?php
}
else echo "Registration is currently disabled. Please try again later.";
?>
<?php
} //end of if($_post)
?>
问题是当您没有输入姓名或电子邮件时,注册按钮上方会显示一个文本,但问题是它也会获得标题,并且可能是页面上的其余页面!我发现问题出在函数(msg)中的javascript中,但我无法解决。谁能帮助我。
<script type="text/javascript">
$("#submitbtn").click(function() {
$('#result').html('<img src="<?php bloginfo('template_url'); ?>/images/ajax-loader.gif" class="loader" />').fadeIn();
var input_data = $('#wp_signup_form').serialize();
$.ajax({
type: "POST",
url: "<?php echo "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; ?>",
data: input_data,
success: function(msg){
$('.loader').remove();
$('<div>').html(msg).appendTo('div#result').hide().fadeIn('slow');
}
});
return false;
});
</script>