PDO对象超出范围,试图调用Javascript函数

The class object I'm trying to interact with is out of scope. I've tried a few things but still can't seem to solve it.

I still receive the error Fatal error: Call to a member function userExists() on a non-object in the console log.

The structure of the site is:

I have an init.php that has includes for all the classes and instantiates the objects.
This is included at the top of every page. I then have a header include that is also included on every page within the body. Within the header is a login box, and an inline event handler to a js function that calls Ajax to validate the login etc.

I understand that the object is out of scope in the function, I've tried to pass it in as a param and failed, as did making it global.

Could someone please suggest a solution?

Code:

init file:

//start session data

session_start();

require 'core/connect/dbConnect.php';
require 'core/classes/users.php';
require 'core/classes/general.php';
require 'core/classes/helper.php';
require 'include/facebook.php';

//instantiating the classes
$users   = new Users($db);
$general = new General();
$helper = new Helper();

error_reporting(E_ALL); ini_set('display_errors', 'On');

The HTML that calls the function:

<form method="post" action="" id="ourLoginFormID_JS">
                    <div class="ourContactFormElement2">
                        <label for="username">Username:</label>
                        <input type="text" id="username"  name="username" autocomplete="off" class="required" value="<?php if(isset($_POST['username'])) echo htmlentities($_POST['username']); ?>"  />
                    </div>

                    <div class="ourContactFormElement2">
                        <label for="password">Password:</label>
                        <input type="password" id="password" name="password" autocomplete="off" class="required"/>
                    </div>

                    <div class="ourContactFormElement2">
                        <label> &nbsp; </label>
                        <input type="submit" name="loginButton" id="loginButton" value="Login!" onclick="validLogin(); return false;"/>
                    </div>

                    <div id="statusLogin"></div>
                </form>

The Ajax / js function:

function validLogin(){

alert("adsad");
$('.error').hide();
var username = $('#username').val();
if(username == ""){
    $('label#usernameError').show();
    $('input#username').focus();
    return false;
}
$('.error').hide();
var password = $('#password').val();
if(password == ""){
    $('label#passwordError').show();
    $('input#password').focus();
    return false;
}
alert("12344242");
var params = {username: username, password: password};
var url = "../js/loginProcessAjax.php";

$("#statusLogin").show();
$("#statusLogin").fadeIn(400).html('<img src="images/loading.gif" />');

$.ajax({
    type: 'POST',
    url: url,
    data: params,
    dataType: 'json',
    beforeSend: function() {
        document.getElementById("statusLogin").innerHTML= 'checking...' ;
    },

    success: function(data) {
        alert("success Area ofAjax");

        $("#statusLogin").hide();

        if(data.success == true){
            alert("if data.success Area of Ajax");
            alert(data.message);

        }else{
            alert("data.message... " + data.message);//undefined
            $("#errorConsole").html(data.message);
        }

    },
    error: function( error ) {
        console.log(error);

    }
});
}

EDIT: Added php where ajax calls.

 <?php

 global $users;

if($_POST){

 if($users->userExists($username) === false){
    $data['message'] = "User doesn't exist";
    $data['success'] = false;

 ....etc etc....
}else{

    $login = $users->login($username, $password);

    if($login === false){

        $data['message'] = 'Incorrect Password or username';
        $data['success'] = false;
    }else{
        $data['success'] = true;
        //destroy old session and create new - prevents session fixation attacks
        session_regenerate_id(true);
        //all details are correct - the method returns the id to be sotred as a session
        $_SESSION['id'] = $login;
    }


}

echo json_encode($data);

}

Using global doesn't make your variable global, it just make your local variable visible in global space, so global $users; doesn't magically pull your $users from you init.php file to

The thing you are misunderstanding is the origin of your ajax request and the destination. In this case:

  • You have include 'init.php' in the origin of the ajax request.
  • It has nothing to do with the destination of the ajax request, in this case loginProcessAjax.php. You can imagine it as open that file from browser, so of course your $users is null

So solution is to include_once 'init.php'; in the beginning of loginProcessAjax.php file. Hope it helps.

Solved by a wonderful guru in chat.

Adding init.php at the top of my loginProcessAjax.ph, returning false from form and adding post for username and password solved.