Javascript空白通过表单传递

时间:2014-05-17 16:13:55

标签: javascript php mysql

我有一个注册表单,它使用AJAX将数据传递给PHP脚本并将其输入MySQL数据库。

然而,我遇到的问题是当有人输入这样的用户名时: “用户名测试”空间未正确解析,因此当您通过链接查看该用户的个人资料时,它将无法正常工作。

所以我的问题是如果你知道如何在Javascript和PHP中解析一个空间,但我很确定PHP的一面是好的。

Firebug在提交表单时输出:

参数application / x-www-form-urlencoded emailSignUp nickdimartino91@gmail.com passwordSignUp nidima10 usernameSignUp用户名测试 资源 usernameSignUp =用户名+测试与安培; passwordSignUp = nidima10&安培; emailSignUp = nickdimartino91%40gmail.com

我在PHP脚本中回显了用户名字段的值,并在用户名中返回一个空格。然而,它并没有以这种方式进入数据库。有什么问题?

更新:不确定我做了什么,但现在一切正常!

注册表单:

    <form action="<?php echo $_SERVER['PHP_SELF'];?>" id="signUpForm" method="post" name="signUpForm">
        <input class="formInput" id="usernameSignUp" name="usernameSignUp" placeholder="Username" required="required" type="text" style="margin-right: 6px;">
        <input class="formInput" id="passwordSignUp" name="passwordSignUp" placeholder="Password" required="required" type="password" /><br>
        <input class="formInput" id="emailSignUp" name="emailSignUp" placeholder="Email" required="required" type="email" style="margin-bottom: 10px; margin-top: 10px; width: 326px;"><br>
        <input class="formButton" id="signUpSubmit" name="signUpSubmit" type="submit" value="Sign Up">
        <div id="signUpLoading" style="display: none; height: auto; margin-top: 10px; width: auto;"><img alt="Loading" src="/images/loading.gif"><span class="loadingMessage">Signing up...</span></div>
        <div id="signUpResponse" style="display: none; height: auto; margin-top: 10px; width: auto;"></div>
    </form>

JS位用于执行AJAX:

    <script>
    $(document).ready(function() {
        $("#signUpForm").submit(function() {
            $(document).ajaxStart(function() {
                $("#signUpLoading" ).show();
            });
            $.ajax({
                url:"/ajax/signUp.php", // Action of the form
                data:$("#signUpForm").serialize(), // Send forms data to server
                dataType:"JSON", // Take response as JSON
                type:"POST", // Send form by post or get
                complete:function(result) {
                    $(document).ajaxStop(function() {
                        $("#signUpLoading" ).hide();
                    });
                    $("#signUpResponse").html(result.responseText);
                    $("#signUpResponse").slideDown("slow");
                }
            });
            return false;  // Default submit return false
        });
    });
    </script>

signUp.php

    <?php
    require('../core/core.php');
    if (!empty($_POST)) {
        $username = $_POST['usernameSignUp'];
        $password = $_POST['passwordSignUp'];
        $email = strtolower($_POST['emailSignUp']);
        if (empty($username)) {
            $errors[] = '<img alt="Error" src="/images/error.png"><span class="errorMessage">Please enter a username.</span>';
        }
        elseif (empty($password)) {
            $errors[] = '<img alt="Error" src="/images/error.png"><span class="errorMessage">Please enter a password.</span>';
        }
        elseif (empty($email)) {
            $errors[] = '<img alt="Error" src="/images/error.png"><span class="errorMessage">Please enter your email address.</span>';
        }
        elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
            $errors[] = '<img alt="Error" src="/images/error.png"><span class="errorMessage">Please enter a valid email address.</span>';
        }
        elseif (strlen($username) < 2 || strlen($username) > 32) {
            $errors[] = '<img alt="Error" src="/images/error.png"><span class="errorMessage">Your username must be between 2 and 32 characters long.</span>';
        }
        elseif (strlen($password) < 6 || strlen($password) > 32) {
            $errors[] = '<img alt="Error" src="/images/error.png"><span class="errorMessage">Your password must be between 6 and 32 characters long.</span>';
        }
        elseif ($users->userExists($username) === true) {
        $errors[] = '<img alt="Error" src="/images/error.png"><span class="errorMessage">The username you\'ve entered is unavailable.</span>';
        }
        elseif ($users->emailExists($email) === true) {
            $errors[] = '<img alt="Error" src="/images/error.png"><span class="errorMessage">The email you\'ve entered is already associated with another account.</span>';
        }
        else {
            $hashedPassword = password_hash($password, PASSWORD_BCRYPT, ['cost' => 12]);
            $users->signUp($username, $hashedPassword, $email);
            echo '<img alt="Success" src="/images/success.png"><span class="successMessage">Thanks for joining the LiveGrooves Community!<br>We\'ve sent you an email with your account information, an activation link, and some tips on getting started.</span>';
            return true;
        }
    }
    if (!empty($errors)) {
        echo '<p>' . implode('</p><p>', $errors) . '</p>';
    }
    ?>

0 个答案:

没有答案