php - PDO 对象超出范围,尝试调用 Javascript 函数

标签 php javascript html ajax

我尝试与之交互的类对象超出了范围。我已经尝试了一些方法,但似乎仍然无法解决它。

我仍然收到错误 fatal error :在控制台日志中的非对象上调用成员函数 userExists()。

网站的结构是:

我有一个 init.php,其中包含所有类并实例化对象。
它包含在每个页面的顶部。 然后我有一个标题包含,它也包含在正文中的每个页面上。 header 内有一个登录框,以及一个 js 函数的内联事件处理程序,该函数调用 Ajax 来验证登录等。

我知道该对象超出了函数的范围,我尝试将其作为参数传递,但失败了,就像将其设为全局一样。

有人可以提出解决方案吗?

代码:

初始化文件:

//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');

调用该函数的 HTML:

<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>

Ajax/js功能:

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);

    }
});
}

编辑:添加了ajax调用的php。

 <?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);

}

最佳答案

使用 global 不会使您的变量成为全局变量,它只会使您的局部变量在全局空间中可见,因此 global $users; 不会神奇地拉动您的 $users 从您的 init.php 文件到

您误解的是ajax请求的来源和目的地。在这种情况下:

  • 您已在 ajax 请求的源中包含“init.php”
  • 它与 ajax 请求的目的地无关,在本例中是 loginProcessAjax.php。您可以想象它是从浏览器打开该文件,所以当然您的 $usersnull

因此解决方案是在 loginProcessAjax.php 文件的开头添加 include_once 'init.php'; 。希望对您有所帮助。

关于php - PDO 对象超出范围,尝试调用 Javascript 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17756507/

相关文章:

php - 如何将不同的html页面值存储到数据库的同一行中?

javascript - 需要更新 iframe 两次才能正常工作

javascript - 单个网页缓存预防

html - 如何制作水平导航栏?

javascript - HTML 发布数组 仅发布第一个值

php - 在 PHP 中使用正则表达式解析 token

php - Joomla 1.5 : How to find number of concurrent/active users in my joomla based intranet system

javascript - 带有查询参数的表单提交

javascript - 尝试在 JavaScript/jQuery 中添加点击事件

html - AWS S3 相同图像,不同尺寸