JavaScript将空参数发送给PHP

时间:2017-05-09 03:44:26

标签: javascript php html mysql

我正在尝试为使用用户名和密码访问的网站创建用户区。我使用HTML作为前端,JavaScript作为后端,PHP作为服务器端。我正在使用Xampp来运行本地服务器和PHPMyAdmin来托管数据库。

HTML代码:

<!-- the Login Section -->
    <input type="text" name="userName" placeholder="username" id="usernameBar">
    <input type="password" name="passWord" placeholder="password" id="passwordBar">
    <button id="loginButton" onclick="Login();">Login</button>
    <p id="IncorrectP" title="Incorrect Username or Password" style="display: none">Invalid</p>

JavaScript的:

function Login(){

    //Connect to the PHP:
    var urlConnect = "checkLogin1.php";
    //Get the username and password:
    var usrUsername = document.getElementById("usernameBar").value;
    var usrPassword = document.getElementById("passwordBar").value;

    //Define the parameters to send to php
    var strParameters = "usrUsername="+usrUsername + "usrPassword="+usrPassword + "&sid=" + Math.random();

        //Define the options for the AJAX request
        var objOptions = {
        method: "post",
        parameters: strParameters,
        onSuccess: function(objXHR) {

            //If objXHR. responseText = Tenant:
            if(objXHR.responseText=='Tenant'){

                //Go to tenant space:
                alert("Success! (Tenant)");
                OpenTenantPage();

            } 
            //Else if objXHR.responseText = Staff:
            else if(objXHR.responseText=='Staff'){

                //Go to staff space:
                alert("Success! (Staff)");
                OpenStaffPage();

            }
            //Else if objXHR.responseText = Admin:
            else if(objXHR.responseText=='Admin'){

                //Go to admin space:
                alert("Success! (Admin)");
                OpenAdminPage();

            }
            else{

                //Run IncorrectLogin:
                alert("Error! No User Account Found!");
                IncorrectLogin();

            }
        }
    }

    // define the AJAX request object
    var objRequest = new Ajax.Request(urlConnect,objOptions);

}

PHP:

<?php

    //Link the username and password:
    $connect = mysqli_connect("localhost", "admin", "12345", "realestate") or die ('Connection to database failed: ' . mysql_error());
    //Extract variables for request parameters:
    extract($_REQUEST);

    //Try to log in as a tentant:
    $resTenantUser = mysqli_query($connect, "SELECT * FROM tenants WHERE Username='$usrUsername' AND Password='$usrPassword'") or die(mysql_error());
    //$resTenantUser = mysqli_query($connect, "SELECT * FROM tenants WHERE Username='Charb1' AND Password='123456' ") or die(mysql_error());
    //Set intCount to number of rows in result:
    $intCount = mysqli_num_rows($resTenantUser);

    if($intCount == 0){

        echo "Error!";

    }
    else{

        echo "Tenant";

    }

?>

我认为我的错误是JS没有将参数发送到PHP或它发送空参数。虽然我不能找到我的错误。

2 个答案:

答案 0 :(得分:3)

parameters必须采用JSON格式,而不是查询字符串格式。如下所示:
parameters:{usrUsername: usrUsername , usrPassword:usrPassword , sid : Math.random()}

答案 1 :(得分:0)

问题在于查询参数。你错过了&。试试这种方式。

var strParameters = "usrUsername="+usrUsername + "&usrPassword="+usrPassword + "&sid=" + Math.random();

或者

var strParameters = {"usrUsername" : usrUsername, "usrPassword" : usrPassword, "sid" : Math.random()}