简体   繁体   English

PHP:对PHP的AJAX请求

[英]PHP: AJAX request to PHP

So I'm having a problem with including a file on a PHP page called from an AJAX request: 所以我在从AJAX请求中调用的PHP页面上包含文件时遇到了问题:

$(document).ready(function() {
$("#image-status").html('Processing images...');
});

function process_images(files) {
$.ajax ({
    type: "POST",
    url: "includes/imageprocessor.php",
    data: {files: JSON.stringify(files)},
    success: function(data){  
        $("#image-status").html(data);
    }
});
}

I'm trying this require_once on imageprocessor.php: 我正在imageprocessor.php上尝试这个require_once:

require_once("db_connect.php");

Which is also in the include directory, and works: I just used it on another page to run an insert that worked just fine. 它也位于include目录中,并且可以正常工作:我只是在另一个页面上使用它来运行一个效果很好的插入。 But when I try to run: 但是当我尝试运行时:

function get_files($id) {
$query = $db->prepare("SELECT * FROM files INNER JOIN file_profile_join ON files.id = file_profile_join.file_id WHERE file_profile_join.profile_id = " . $id . " GROUP BY files.id");
try {
    $query->execute();
    $rows = $query->fetch();
} catch (PDOException $e) {
    die($e->getMessage());
}
}

later on in imageprocessor.php, it errors out: 稍后在imageprocessor.php中,它会出错:

Fatal error: Call to a member function prepare() on a non-object in /home/yogabopvolume26/theprivatesector.org/epic/includes/imageprocessor.php on line 90

Like it's not requiring db_connect in the first place (that's where $db is declared). 就像它首先不需要db_connect(在那儿声明$ db)。 But it's also not returning an error that it can't find db_connect.php. 但是它也不会返回找不到db_connect.php的错误。

basename(__DIR__) 

on imageprocessor.php (echoed through the AJAX request) returns "include." (通过AJAX请求回显)在imageprocessor.php上返回“ include”。

I've tried using both an absolute URL and root path in the require_once, but then it neither errors out nor continues with imageprocessor.php: no echoes from that file after the require_once are returned. 我试过在require_once中使用绝对URL和根路径,但是随后它既不会出错也不会继续使用imageprocessor.php:在require_once返回之后,该文件没有回声。 I also can't get the current session from session_start (I can print_r $_SESSION from the originary page [profile.php], so the session is active). 我也无法从session_start获取当前会话(我可以从原始页面[profile.php]中使用print_r $ _SESSION,因此该会话处于活动状态)。 Maybe they're related? 也许他们有关系吗? I can't figure out what's wrong. 我不知道怎么了。

Hope your db_connect.php is something link: 希望您的db_connect.php是链接:

try {
    $db = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
} catch (PDOException $e) {
    print "Error!: " . $e->getMessage() . "<br/>";
    die();
}

And if you are using the connection object outside for your connection file, then you have to access that object into your particular file with the use of global 而且,如果将外部连接对象用于连接文件,则必须使用global将该对象访问到特定文件中

And as i know, you getting this error because you didn't called the object $db in your file imageprocessor.php , try with this: 据我所知,您收到此错误是因为您没有在文件imageprocessor.php调用对象$db ,请尝试以下操作:

function get_files($id) {
    global $db;
    $query = $db->prepare("SELECT * FROM files INNER JOIN file_profile_join ON files.id = file_profile_join.file_id WHERE file_profile_join.profile_id = " . $id . " GROUP BY files.id");
    try {
        $query->execute();
        $rows = $query->fetch();
    } catch (PDOException $e) {
        die($e->getMessage());
    }
}

Call to a member function someFunc() on a non-object in...... 在非对象中调用成员函数someFunc()……

This type of error happens when you want to use the someFunc() method of any object, but unfortunately your object is not a object. 当您要使用任何对象的someFunc()方法时,都会发生这种类型的错误,但是不幸的是您的对象不是对象。

Suppose 假设

$obj = New SomeObj();
$nextObj = $obj->someMethod(); // << Suppoes someMethod() returns a object if successfull
$nextObj->newMethod();

In this code, the $nextObj->newMethod() will work only when $nextObj is an object. 在此代码中,仅当$nextObj是对象时, $nextObj->newMethod()才能工作。 But we cannot be sure about that because $obj->someMethod(); 但是我们不能确定,因为$obj->someMethod(); will return an object only if successful, and returns otherwise when not successful. 仅在成功时才返回对象,否则在不成功时返回。

A good way to prevent that would be. 防止这种情况的好方法是。

$nextObj = $obj->someMethod(); // << Suppoes someMethod() returns a object if successfull
if(is_object($nextObj)){
    $nextObj->newMethod();
} else {
    //Now you know that there is error while executing $obj->someMethod();
}

Another Thing: Since you have a function function get_files($id) . 另一件事:由于您具有函数function get_files($id) You can yourself see that $db variable is not defined within the scope of your function. 您自己可以看到$db变量未在函数范围内定义。 So, $db contains nothing for now. 因此,$ db目前不包含任何内容。 AS in the other answers, you should use global $db or can pass the $db object as an argument to your function. 在其他答案中,应使用global $db或可以将$db对象作为参数传递给函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM