PHP变量echos为空,但它包含$ _POST ["变量"],它回显正确的值

时间:2018-01-18 17:22:17

标签: javascript php ajax forms

问题是,当我将ajax中的已发布变量的变量赋值为$_POST["user"]时,它没有显示任何内容。但是当我回应$_POST["variable"]时,它显示了我正确的价值。我想知道怎么可能?它发生在所有发布的变量上。

因为我的用户名检查(如果条件)总是说需要3-20个字符而且它永远不会是因为变量由于某种原因具有空值。

在我的网站上直播,直到问题解决: http://artisticdesigning.com/signup.php

<?php
if(isset($_POST["user"]))
{
include_once("Includes/database_link.php");

$first = $_POST["first"];
$last = $_POST["last"];
$email = $_POST["email"];
$userid = $_POST["user"];
$password = $_POST["password"];
$gender = $_POST["gender"];

$ip = preg_replace("#[^0-9.]#", "", getenv('REMOTE_ADDR'));

$sql = "SELECT id from users WHERE username='$userid' LIMIT 1";
$query = mysqli_query($cnctn, $sql);
$noofusers = mysqli_num_rows($query);

$sql = "SELECT id from users WHERE email='$email' LIMIT 1";
$query = mysqli_query($cnctn, $sql);
$noofemails = mysqli_num_rows($query);

if($first = "" || $last = "" || $email = "" || $password = "" || $userid = "" || $gender = "")
{
    echo "Form submission is missing values.";
    exit();
}
else if($noofusers > 0)
{
    echo "The username you entered is already taken.";
    exit();
}
else if($noofemails > 0)
{
    echo "Someone is already using this email address.";
    exit();
}
else if(strlen($userid) < 3 || strlen($userid) > 20)
{
    echo "Username should be 3-20 characters long.";
    exit();
}
else if(is_numeric($userid[0]))
{
    echo "Username cannot begin with a number";
    exit();
}
else
{
    $hashedPassword = password_hash($password, PASSWORD_DEFAULT);

    $sql = "INSERT INTO users (first, last, email, username, password, gender, ip, signup, lastlogin, notescheck) values('$first', '$last', '$email', '$userid', '$password', '$gender', '$ip', now(), now(), now())";
    $query = mysqli_query($cnctn, $sql);
    $uid = mysqli_insert_id($cnctn);
?>

HTML:

<form id="signup-form">
            <h2><a href="login.php">Login</a> &nbsp; or &nbsp;<u>Sign Up</u></h2>
            <br>

            <div id="signup-name">
                <div class="signup-input-div">
                    <input class="signup-input" type="text" name="firstname"  id="firstname" onMouseOver="signupPlaceholderCheck(0)" onMouseOut="signupPlaceholderCheck(0)" onFocus="signupPlaceholderCheck(0)" onBlur="signupPlaceholderCheck(0)" oninput="signupPlaceholderCheck(0)" required>
                    <label class="signup-placeholder">First Name</label>
                </div>
                <div class="signup-input-div">
                    <input class="signup-input" type="text" name="lastname" id="lastname" onMouseOver="signupPlaceholderCheck(1)" onMouseOut="signupPlaceholderCheck(1)" onFocus="signupPlaceholderCheck(1)" onBlur="signupPlaceholderCheck(1)" oninput="signupPlaceholderCheck(1)" required>
                    <label class="signup-placeholder">Last Name</label>
                </div>
            </div>

            <br>

            <div class="signup-input-div">
            <input class="signup-input" type="email" id="email"  name="email" maxlength="88" onMouseOver="signupPlaceholderCheck(2)" onMouseOut="signupPlaceholderCheck(2)" onFocus="signupPlaceholderCheck(2)" onBlur="signupPlaceholderCheck(2)" oninput="signupPlaceholderCheck(2)" required>
            <label class="signup-placeholder">Your Email</label>
            </div>

            <br>

            <div class="signup-input-div">
                <span class="popup-box">

                </span>
                <input class="signup-input" type="text" id="username" name="username" maxlength="20" pattern=".{3,20}" title="3 characters minimum" autocomplete="off" onMouseOver="signupPlaceholderCheck(3)" onMouseOut="signupPlaceholderCheck(3)" onFocus="signupPlaceholderCheck(3)" oninput="signupPlaceholderCheck(3)">
                <label class="signup-placeholder">Username</label>
            </div>

            <br>

            <div class="signup-input-div">
            <input class="signup-input" type="password" name="password" id="password" onMouseOver="signupPlaceholderCheck(4)" onMouseOut="signupPlaceholderCheck(4)" onFocus="signupPlaceholderCheck(4)" onBlur="signupPlaceholderCheck(4)" oninput="signupPlaceholderCheck(4)" required>
            <label class="signup-placeholder">Password</label>
            </div>

            <br>

            <div class="signup-input-div">
            <span class="popup-box">

            </span>

            <input class="signup-input" type="password" name="confirm-password" id="confirm-password" onMouseOver="signupPlaceholderCheck(5)" onMouseOut="signupPlaceholderCheck(5)" onFocus="signupPlaceholderCheck(5)" oninput="signupPlaceholderCheck(5)" onBlur="signupPlaceholderCheck(5)" required>
            <label class="signup-placeholder">Confirm your password</label>
            </div>

            <br>

            <select required name="gender" id="gender">
                <option>Gender</option>
                <option value="m">Male</option>
                <option value="f">Female</option>
                <option value="o">Other</option>
            </select>

            <br /><br />
            <span id="main-status"></span>
            <br /><br />

            <input type="submit" value="Sign Up" class="button" id="submit-button">
        </form>

AJAX:

function signup()
    {
        var ajax = new XMLHttpRequest();
        url = "signup.php";

        var first = _("firstname").value;
        var last = _("lastname").value;
        var email = _("email").value;
        var user = _("username").value;
        var password = _("password").value;
        var confirmpassword = _("confirm-password").value;
        var gender = _("gender").value;
        var btn = _("submit-button");

        var status = $("popup-box");

        if(first = "" || last == "" || email == "" || user == "" || password == "" || confirmpassword == "" ||  gender == "")
           {
                _("main-status").style.display="inline";
                _("main-status").innerHTML="Fill out all of the form data!";
           }

        else if(password != confirmpassword)
            {
                status[1].style.display="inline";
                status[1].innerHTML="Your Password fields do not match!";
            }
        else
            {
                ajax.open("POST", url, true);
                ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
                ajax.onreadystatechange = 
                    function()
                    {
                        if(ajax.readyState == 4 && ajax.status == 200)
                            {
                                var data = ajax.responseText;
                                if(data != "signup_success")
                                    {
                                        _("main-status").display="inline";
                                        _("main-status").innerHTML=data;
                                        btn.style.display="inline";
                                    }
                                else
                                    {
                                        _("signup-form").innerHTML="Thanks for Signing up "+first+", check your email inbox and junk mail box at <u>"+e+"</u> in a moment to complete the sign up process by activating your account. You will not be able to do anything on the site until you successfully activate your account.";
                                    }
                            }
                    }
                _("main-status").style.display="inline";
                btn.style.display="none";
                _("main-status").innerHTML="Please wait...";
                ajax.send("first="+first+"&last="+last+"&user="+user+"&email="+email+"&password="+password+"&gender="+gender);
            }
    }

1 个答案:

答案 0 :(得分:3)

比较和分配存在问题

if($first = "" || $last = "" || $email = "" || $password = "" || $userid = "" || $gender = "")
{
    echo "Form submission is missing values.";
    exit();
}

应该是

if($first == "" || $last == "" || $email == "" || $password == "" || $userid == "" || $gender == "")
{
    echo "Form submission is missing values.";
    exit();
}

请检查一下 http://sourcedaddy.com/php/differentiate-equal-signs.html