0

我想制作一个订阅者表格,我将此代码与此 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>
4

1 回答 1

0

在我们继续之前,请注意 wp-includes/registration.php 在 WordPress 3.1 中已弃用。

关于验证:简单的方法是在提交之前进行 jQuery 表单验证。有一个 jQuery 插件可以做这样的事情。请参阅:http ://docs.jquery.com/Plugins/Validation

我需要确定“页面的其余部分也正在加载”。从代码中,除了每次验证的错误消息外,我什么都看不到。所以,msg必须只带它,而不是别的。但是您可能需要关闭它,die();而不是exit();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>
于 2012-06-15T01:42:55.427 回答