Wordpress wp_在JavaScript中创建用户
我想制作一个订户表单,我将此代码与此javascript一起使用Wordpress wp_在JavaScript中创建用户,wordpress,Wordpress,我想制作一个订户表单,我将此代码与此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']);
<?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)
?>
问题是,如果您没有输入姓名或电子邮件,将显示一条文本
你点击了“注册”按钮,但问题是它也会得到标题,并且可能是页面上方的剩余页面!
我发现问题出在functionmsg中的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>
在我们继续之前,请注意wp includes/registration.php在WordPress 3.1中已被弃用 关于验证: 简单的方法是在提交jQuery表单之前进行表单验证。有一个jQuery插件可以做这样的事情。见: 我需要确定“页面的其余部分也正在加载”。从代码中,我只看到每个验证的错误消息。因此,味精只能带来它,而不能带来其他东西。但是你可能需要用骰子来关闭它;而不是退出;其中exit只是停止执行脚本,die也会将错误发送到javascript 编辑:1
<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){
var textMsg = $(msg).find('#msg').html(); // Replace #msg with the element which you add the message
$('.loader').remove();
$('<div>').html(textMsg).appendTo('div#result').hide().fadeIn('slow');
}
});
return false;
});
</script>
谢谢你,我试着去死;而不是退出;但是问题仍然存在。上面代码中的验证工作正常,除了加载整个主页!$。ajax{type:POST,url:,data:input_data,success:functionmsg{$'.loader'.remove;$.htmlmsg.appendTo'divresult.hide.fadeIn'slow'}也许您可以尝试过滤响应。请参阅我的注释,我已经对其进行了编辑以包含代码。