0

我拼命想让这段代码工作,只要我点击 Sוbmit 表单就会刷新并且没有任何反应。usernamecheck 函数工作得很好。

html和php,这个文件名是:sign_up_page.php -

<?php
session_start();
// If user is logged in, header them away
if(isset($_SESSION["username"])){
    header("location: message.php?msg=NO to that weenis");
    exit();
}
?><?php
// Ajax calls this NAME CHECK code to execute
if(isset($_POST["usernamecheck"])){
    include_once("php_includes/db_conx.php");
    $username = preg_replace('#[^a-z0-9]#i', '', $_POST['usernamecheck']);
    $sql = "SELECT id FROM users WHERE username='$username' LIMIT 1";
    $query = mysqli_query($db_conx, $sql); 
    $uname_check = mysqli_num_rows($query);
    if (strlen($username) < 3 || strlen($username) > 16) {
        echo '<strong style="color:#F00;">3 - 16 characters please</strong>';
        exit();
    }
    if (is_numeric($username[0])) {
        echo '<strong style="color:#F00;">Usernames must begin with a letter</strong>';
        exit();
    }
    if ($uname_check < 1) {
        echo '<strong style="color:#009900;">' . $username . ' is OK</strong>';
        exit();
    } else {
        echo '<strong style="color:#F00;">' . $username . ' is taken</strong>';
        exit();
    }

}
?>
<?php
// Ajax calls this REGISTRATION code to execute
if(isset($_POST["username"])){
    echo "entered !!!!!!";
    // CONNECT TO THE DATABASE
    include_once("php_includes/db_conx.php");
    // GATHER THE POSTED DATA INTO LOCAL VARIABLES
    $u = preg_replace('#[^a-z0-9]#i', '', $_POST['username']);
    $fn = preg_replace('#[^a-z0-9]#i', '', $_POST['firstn']);
    $ln = preg_replace('#[^a-z0-9]#i', '', $_POST['lastn']);
    $e = mysqli_real_escape_string($db_conx, $_POST['email']);
    $p = $_POST['pass1'];
    // GET USER IP ADDRESS
    $ip = preg_replace('#[^0-9.]#', '', getenv('REMOTE_ADDR'));
    // DUPLICATE DATA CHECKS FOR USERNAME AND EMAIL
    $sql = "SELECT id FROM users WHERE username='$u' LIMIT 1";
    $query = mysqli_query($db_conx, $sql); 
    $u_check = mysqli_num_rows($query);
    // -------------------------------------------
    $sql = "SELECT id FROM users WHERE email='$e' LIMIT 1";
    $query = mysqli_query($db_conx, $sql); 
    $e_check = mysqli_num_rows($query);
    // FORM DATA ERROR HANDLING
    if($u == "" || $e == "" || $p == "" || $ln == "" || $fn == ""){
        echo "The form submission is missing values.";
        exit();
    } else if ($u_check > 0){ 
        echo "The username you entered is alreay taken";
        exit();
    } else if ($e_check > 0){ 
        echo "That email address is already in use in the system";
        exit();
    } else if (strlen($u) < 3 || strlen($u) > 16) {
        echo "Username must be between 3 and 16 characters";
        exit(); 
    } else if (is_numeric($u[0])) {
        echo 'Username cannot begin with a number';
        exit();
    } else {
    // END FORM DATA ERROR HANDLING
        // Begin Insertion of data into the database
        // Hash the password and apply your own mysterious unique salt
        $cryptpass = crypt($p);
        include_once ("php_includes/randStrGen.php");
        $p_hash = randStrGen(20)."$cryptpass".randStrGen(20);
        // Add user info into the database table for the main site table
        $sql = "INSERT INTO users (username, email, password, firstname, lastname, ip, signup, lastlogin, notescheck)       
                VALUES('$u','$e','$p_hash','$fn','$ln','$ip',now(),now(),now())";
        $query = mysqli_query($db_conx, $sql); 
        $uid = mysqli_insert_id($db_conx);
        // Establish their row in the useroptions table
        $sql = "INSERT INTO useroptions (id, username, background) VALUES ('$uid','$u','original')";
        $query = mysqli_query($db_conx, $sql);

        echo "signup_success";
        exit();
    }
    exit();
}
?>


<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title>Memory Line - Sign Up Page</title>

        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link rel="stylesheet" href="../bootstrap/css/bootstrap.css">
        <link rel="stylesheet" href="../bootstrap/css/bootstrap-responsive.css">
    </head>
    <body>
        <script src="http://code.jquery.com/jquery-1.9.0.min.js"></script>
        <script src="../bootstrap/js/bootstrap.js"></script>
        <script src="js/sign_up_js.js"></script>
        <script src="js/main.js"></script>
        <script src="js/ajax.js"></script>




        <div class="navbar navbar-fixed-top">
            <div class="navbar-inner">
                <div class="container">
                    <a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
                        <span class="icon-th-list"></span>
                    </a>
                    <a href="#" class="brand">Memory Line</a>
                    <div class="nav-collapse collapse">
                        <ul class="nav pull-right" >
                            <li class="active"><a href="#">Home</a></li>
                            <li class="divider-vertical"></li>
                            <li><a href="#">About Us</a></li>
                            <li class="divider-vertical"></li>
                            <li><a href="#">Contact</a></li>
                        </ul>
                    </div>
                </div>
            </div>
        </div> <!--top navbar-->



        <div class="row-fluid">
            <div class="span4">
                <div class="well well-large">
                    <form>
                        <fieldset>
                            <h2>Login!</h2>
                            <input type="text" class="input-block-level" placeholder="Username"> <!--username-->
                            <input type="text" class="input-block-level" placeholder="Password"> <!--password-->
                            <button id="sumbit_btn" class="btn btn-primary" type="submit"  >Sumbit</button> <!--login btn-->
                        </fieldset>
                    </form>
                </div>
            </div> <!--login form-->


            <div class="span8">
                <div class="well well-large">
                    <h2>Sign Up!</h2>
                    <form >
                         <fieldset> 
                                <div class="span6">
                                    <input type="text" id="username" class="input-block-level"  placeholder="Username" name="username" onkeyup="restrict('username')" onblur="checkusername()" maxlength="16" >
                                    <input type="text" id="email" name="pass1" class="input-block-level"  placeholder="Password" maxlength="16">
                                    <input type="text" name="pass2" class="input-block-level"  placeholder="Repeat Password" maxlength="16">
                                    <input type="email" name="email" class="input-block-level" onkeyup="restrict('email')" placeholder="Email" maxlength="88">
                                    <span id="status"></span>
                                </div> 

                                <div class="span6">
                                    <input type="text" name="firstn" class="input-block-level" placeholder="First Name" maxlength="16">
                                    <input type="text" name="lastn" class="input-block-level" placeholder="Last Name" maxlength="16">
                                    <div class="input-append date datepicker" data-date-format="dd-mm-yyyy">
                                        <input type="date"><span class="add-on">Birth Day</span>
                                    </div>
                                     <button class="btn btn-primary span4 pull-right"  onclick="signup()">submit</button>
                                </div>
                          </fieldset> 
                     </form>
                </div>
            </div>
        </div>



    </body>
</html>

包含注册功能的 javascript :

function restrict(elem){
    var tf = _(elem);
    var rx = new RegExp;
    if(elem == "email"){
        rx = /[' "]/gi;
    } else if(elem == "username"){
        rx = /[^a-z0-9]/gi;
    }
    tf.value = tf.value.replace(rx, "");
}
function emptyElement(x){
    _(x).innerHTML = "";
}
function checkusername(){
    var u = _("username").value;
    if(u != ""){
        _("status").innerHTML = 'checking ...';
        var ajax = ajaxObj("POST", "sign_up_page.php");
        ajax.onreadystatechange = function() {
            if(ajaxReturn(ajax) == true) {
                _("status").innerHTML = ajax.responseText;
            }
        }
        ajax.send("usernamecheck="+u);
    }
}
function signup(){
    var username = _("username").value;
    var e = _("email").value;
    var p1 = _("pass1").value;
    var p2 = _("pass2").value;
    var fn = _("firstn").value;
    var ln = _("lastn").value;
    var status = _("status");
    if(username == "" || e == "" || p1 == "" || p2 == "" || fn == "" || ln == ""){
        status.innerHTML = "Fill out all of the form data";
    } else if(p1 != p2){
        status.innerHTML = "Your password fields do not match";
    } else {
        status.innerHTML = 'please wait ...';
        var ajax = ajaxObj("POST", "sign_up_page.php");
        ajax.onreadystatechange = function () {
            if (ajaxReturn(ajax) == true) {
                alert("ajax returned true");
                if (ajax.responseText != "signup_success") {
                    status.innerHTML = ajax.responseText;
                    status.innerHTML = 'some problem has accourd';
                } else {
                    window.scrollTo(0, 0);
                    status.innerHTML = "Succeed , check your Email Account for activation";

                }
            }

        }
        ajax.send("username="+username+"&e="+e+"&p="+p1+"&fn="+fn+"&ln="+ln);
    }

}

/* function addEvents(){
    _("elemID").addEventListener("click", func, false);
}
window.onload = addEvents; */

ajax.jx:

function ajaxObj( meth, url ) {
    var x = new XMLHttpRequest();
    x.open( meth, url, true );
    x.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    return x;
}
function ajaxReturn(x){
    if(x.readyState == 4 && x.status == 200){
        return true;    
    }
}
4

0 回答 0