douruoshen1449 2018-12-02 14:03
浏览 62

注册表单提交时忽略Javascript函数

I am currently working on a registration form using PHP, Ajax, Jquery etc, I have created a registration form and when I hit the register button, the account gets registered to the database but the button would not redirect the user to the next page unless a hard reload (f5) is taken place. (The session is still created as well).

I have theorized that a function in my main.js file is being ignored. The registration ajax page is as follows

<?php

    //allow the config
    define('__CONFIG__', true);

    //require the config
    require_once "../inc/config.php"; //possibly have to change the location

if($_SERVER['REQUEST_METHOD'] == 'POST') {
    //Always return JSON format
    //header('Content-Type: application/json');

    $return = [];

    $email = Filter::String( $_POST['email'] );
    $username = Filter::String($_POST['username']);
    $skills = Filter::String($_POST['skills']);
    //$captcha = Filter::String($_POST['captcha']);

    $user_found = User::Find($email, $username);
    $activationCode = DB::generateCode();
    $credit = 0;
    mysqli_report(MYSQLI_REPORT_ALL);


    //if($_SESSION['captcha'] === $captcha){
        // make sure the user does not exist.
        if($user_found) {
            // User exists
            // We can also check to see if they are able to log in.
            $return['error'] = "You already have an account";
            $return['is_logged_in'] = false;
        } else {
            // User does not exist, add them now.
            $password = password_hash($_POST['password'], PASSWORD_DEFAULT);
            // make sure the user CAN be added AND is added.
            $addUser = $con->prepare("INSERT INTO users(username, email, skills, password, credit, activationCode) VALUES(:username, :email, :skills, :password, :credit, :activationCode)");
            $addUser->bindParam(':username', $username, PDO::PARAM_STR);
            $addUser->bindParam(':credit', $credit, PDO::PARAM_STR);
            $addUser->bindParam(':activationCode', $activationCode, PDO::PARAM_STR);

            $addUser->bindParam(':email', $email, PDO::PARAM_STR);
            $addUser->bindParam(':skills', $skills, PDO::PARAM_STR);
            $addUser->bindParam(':password', $password, PDO::PARAM_STR);
            $addUser->execute();

            $user_id = $con->lastInsertId();

            $_SESSION['user_id'] = (int) $user_id;

            sendMail ($email, $name='', $username, $activationCode);




            $return['is_logged_in'] = true;
           // $return=header("Location:localhost://comp1687/dashboard.php?message=welcome");
            $return['redirect'] = "dashboard.php?message=welcome";
        }

        // return the proper information back to Javascript to redirect us.

        echo json_encode($return, JSON_PRETTY_PRINT);
    } else {
        //Kill the script. Redirect the user.
        exit('Invalid URL');
        }


 ?>

The code in the main.js is as follows

$(document)
.on("submit", "form.js-register", function(event) {
    event.preventDefault();

    var _form = $(this);
    var _error = $(".js-error", _form);

    var dataObj = {
        email: $("input[type='email']", _form).val(),
        password: $("input[type='password']", _form).val(),
        username: $("input[id='username']", _form).val(),
        skills: $("input[id='skills']", _form).val(),
    };

    if(dataObj.email.length < 6) {
        _error
            .text("please enter a valid email address").show()
            .show();
        return false;
    } else if (dataObj .password.length < 8) {
        _error
            .text("please enter a password that is at least 8 characters long.")
            .show();
            return false;

    }

    // Assuming the code gets this far, we can start the ajax process
    _error.hide();

    $.ajax({
        type: 'POST',
        url: 'comp1687/ajax/register.php', //possibly have to change the location
        data: dataObj,
        dataType: 'json',
        async: true,
    })
    .done(function(data) {
        // Whatever data is
        if(data.redirect !== undefined) {
            window.location = data.redirect;
            //window.location.replace(data.redirect);
        } else if(data.error !== undefined) {
            _error
                .text(data.error)
                .show();
        }
    })
    // .fail(function ajaxFailed(e) {
    //  //this failed
    //
    // })
    // .always(function ajaxAlwaysDoThis(data) {
    //  //always do
    //  console.log('Always');
    // })

    return false;
})
//
.on("submit", "form.js-login", function(event) {
    event.preventDefault();

    var _form = $(this);
    var _error = $(".js-error", _form);

    var dataObj = {
        username: $("input[type='text']", _form).val(),
        email: $("input[type='email']", _form).val(),
        skills: $("input[type='text']", _form).val(),
        password: $("input[type='password']", _form).val()

    };

    if(dataObj.email.length < 6) {
        _error
            .text("Please enter a valid email address")
            .show();
        return false;
    } else if (dataObj.password.length < 8) {
        _error
            .text("Please enter a password that is at least 8 characters long.")
            .show();
        return false;
    }

    // Assuming the code gets this far, we can start the ajax process
    _error.hide();

    $.ajax({
        type: 'POST',
        url: 'comp1687/ajax/login.php', //possibly have to change the location
        data: dataObj,
        dataType: 'json',
        async: true,
    })
    .done(function(data) {
        // Whatever data is
        if(data.redirect !== undefined) {
            window.location = data.redirect;
            //window.location.replace(data.redirect);
        } else if(data.error !== undefined) {
            _error
                .html(data.error)
                .show();
        }
    })
    // .fail(function ajaxFailed(e) {
    //  // This failed
    // })
    // .always(function ajaxAlwaysDoThis(data) {
    //  // Always do
    //  console.log('Always');
    // })

    return false;
})

The function that I feel like is being ignored is the .done function. Is there any obvious reasons that you would assume my registration button is not redirecting the user?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 2024-五一综合模拟赛
    • ¥15 下图接收小电路,谁知道原理
    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度
    • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
    • ¥15 ETLCloud 处理json多层级问题
    • ¥15 matlab中使用gurobi时报错
    • ¥15 这个主板怎么能扩出一两个sata口
    • ¥15 不是,这到底错哪儿了😭