Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/238.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从JavaScript Ajax调用方调用PHP函数_Php_Jquery - Fatal编程技术网

从JavaScript Ajax调用方调用PHP函数

从JavaScript Ajax调用方调用PHP函数,php,jquery,Php,Jquery,我需要在php类中对我的php函数进行ajax调用 文件夹如下所示: controllers (folder) -----User.php (php file) View (folder) -----js (folder) ---------myjavascriptfile.js include 'user.php'; if($_GET['method']){ $user = new User(); //because this is the name of your class $us

我需要在php类中对我的php函数进行ajax调用

文件夹如下所示:

controllers (folder)
-----User.php (php file)
View (folder)
-----js (folder)
---------myjavascriptfile.js
include 'user.php'; 

if($_GET['method']){

$user = new User(); //because this is the name of your class
$username = $_GET['name'];//I suspect you're sanitizing this input somehow before quering the    database


    switch ($_GET['method']) {
        case "newUser":
            echo $user->newUser($username);//this will echo asdf because the newUser() method in your User class returns asdf;
            break;
        case "deleteUser":
            echo $user->deleteUser($username); //you would need to make the deleteUser method in your user class
            break;
        case "etc":
            echo "etc";
            break;
    }

}
    $.ajax({
        type: "GET",
        url: "/controllers/user-process.php",
        data: { method: "newUser", name: "John" }
    }).done(function( msg ) {
        alert( "Data Saved: " + msg );
    });
在myjavascriptfile.js中 我有这个:

$.ajax({
            type: "GET",
            url: "../controllers/User.php/newUser",
            data: { name: "John" }
        }).done(function( msg ) {
            alert( "Data Saved: " + msg );
        });
错误 我得到的错误是newUser不存在,尽管这是我的User.php


抱歉,如果这是一个愚蠢的问题,我在没有框架的php中不是很好:

您的AJAX调用应该是一个URL,而不是一个文件系统位置。输入要在浏览器地址栏中加载该页面的内容。例如:

$.ajax({
        type: "GET",
        url: "http://example.com/controllers/User.php?newUser",
        data: { name: "John" }
    }).done(function( msg ) {
        alert( "Data Saved: " + msg );
    });
需要在调用的URL中提供所有内容,因为PHP无权访问您的Javascript,Javascript无权访问您的PHP

然后,PHP页面需要返回Javascript所需的所有内容,通常为JSON或XML。因此,在User.php脚本中,您可以更新数据库或执行任何需要执行的操作。它必须从$\u GET数组值中计算出所有内容。这可能是函数名,也可能只是值。如果要将结果返回到Javascript,则会从脚本中回显这些结果

您的User.php脚本可能是:

<?php
$name = $_GET['newUser'];
// Some code to open a database, check if the name is valid, etc., here..
echo "user name '$name'";
?>

以下是代码的生命周期:

JavaScript由浏览器解析 执行JavaScript JavaScript加载jQuery jQuery执行您发送的代码段。 代码从位于../controllers/User.php/newUser的Web服务器请求资源 您的Web服务器返回404,因为该文件不存在,因为您声明不使用框架。 如果您将框架配置为使该路由调用类上的方法,则框架可以,但您没有,您只有PHP中的类定义。它不会在任何地方初始化,也不会调用任何实例上的方法

你的PHP什么都不做。你的PHP永远不会被调用


此外,您的相对路径是相对于执行它的页面的,而不是JS文件的位置。

您在其中的类是一个现存的表示,表示在它之后构造的对象将能够做什么以及如何实现。它本身不可用

要解决这个问题,您需要在另一个文件中添加一些PHP,该文件可以调用实际代码。一个真正的小而清晰的路由器,如果你不介意的话,你可以使用它

两者都非常易于使用,它们的作用是在AJAX调用或任何HTTP请求与一些可运行代码(通常是匿名函数或方法)之间起到粘合作用,这样,您就不需要自己建立这种关系,而需要处理调用这些URL的实际预期行为


看看他们

我想你不确定你要的是什么。您需要一个ajax查询来将数据获取到类中的php方法。因此,在ajax查询中调用php文件,该php文件调用该类并对数据运行该方法

这就是ajax访问类的方式。制作另一个名为user-process.php的文件,并将其放入控制器文件夹中

<?php

include 'user.php';

if($_GET['name']){  // because you're posting {name: 'John'} in your ajax
    $user = new User(); //because this is the name of your class
    $username = $_GET['name']; //I suspect you're sanitizing this input somehow before quering the database
    echo $user->newUser($username);  //this will echo asdf because the newUser() method in your User class returns asdf;
    die();

}

?>
如果希望包含某种切换机制,可以将user-process.php文件设置为如下所示:

controllers (folder)
-----User.php (php file)
View (folder)
-----js (folder)
---------myjavascriptfile.js
include 'user.php'; 

if($_GET['method']){

$user = new User(); //because this is the name of your class
$username = $_GET['name'];//I suspect you're sanitizing this input somehow before quering the    database


    switch ($_GET['method']) {
        case "newUser":
            echo $user->newUser($username);//this will echo asdf because the newUser() method in your User class returns asdf;
            break;
        case "deleteUser":
            echo $user->deleteUser($username); //you would need to make the deleteUser method in your user class
            break;
        case "etc":
            echo "etc";
            break;
    }

}
    $.ajax({
        type: "GET",
        url: "/controllers/user-process.php",
        data: { method: "newUser", name: "John" }
    }).done(function( msg ) {
        alert( "Data Saved: " + msg );
    });
对于ajax调用,您需要添加如下方法数据:

controllers (folder)
-----User.php (php file)
View (folder)
-----js (folder)
---------myjavascriptfile.js
include 'user.php'; 

if($_GET['method']){

$user = new User(); //because this is the name of your class
$username = $_GET['name'];//I suspect you're sanitizing this input somehow before quering the    database


    switch ($_GET['method']) {
        case "newUser":
            echo $user->newUser($username);//this will echo asdf because the newUser() method in your User class returns asdf;
            break;
        case "deleteUser":
            echo $user->deleteUser($username); //you would need to make the deleteUser method in your user class
            break;
        case "etc":
            echo "etc";
            break;
    }

}
    $.ajax({
        type: "GET",
        url: "/controllers/user-process.php",
        data: { method: "newUser", name: "John" }
    }).done(function( msg ) {
        alert( "Data Saved: " + msg );
    });

我知道你在做什么,我想你对函数的调用有误解。将函数名附加到URL不会强制php运行该函数。即,点击index.php/hello不会运行函数hello{echo hello;}

这不是php的工作方式

您必须做的是读取查询,即在PHP中读取传入的内容。如果您的url是user.php/newUser,那么您必须读取正在传入的newUser,并且在某种形式的switch语句或其他路由器中,一个调用此控制器和此方法的核心文件等告诉php运行该方法

编辑:

在全局变量$\u SERVER['PATH\u INFO']中,可以说,您将找到路由,即如果您点击user.php/newUser,那么该全局变量将返回/newUser

您可以这样做,读取函数名并去掉斜杠,然后运行函数(如果可以找到):

$methodName = str_replace("/", $_SERVER['PATH_INFO']);
if(function_exists($methodName)) $methodName();

这是一种非常简单的方法,而广泛使用的框架显然要复杂得多。这将让您了解如何读取预期路由的URL,以及如何测试方法是否存在,然后使用动态名称调用方法。

您有指向该方法的路由吗?@Rangad没有。我应该吗?那么,你怎么期望php知道在什么url上调用什么方法呢?可能是因为我不了解您的应用程序布局。但据我所知,你只需要一个有方法的类。当您访问该文件时,不会执行任何操作。我假设他没有使用任何框架,但他期望框架的典型行为:@Matteo是的,没错,我习惯于在laravel中工作,但在这种情况下不可能使用框架,因为
这是一个简单的页面,相对URI不是问题所在,与文件系统无关。我清楚地知道,Marco Dinatsoli正试图实现Javascript和PHP脚本之间的通信,并试图给出相对文件系统路径。这是错误的,因为Javascript是在浏览器上运行的,而不是在服务器上,并且没有访问文件系统的权限。他可以通过正确的URL找到完全相同的脚本。因此,在某种程度上,它与文件系统有关,或者至少与正确访问文件系统有关。但这不是OP所问的问题。他不知道如何调用控制器中的函数。你说的是对的,但这是另一回事。他试图给出一个相对的文件系统路径-不,这是一个相对的URL。相对URL是好的。这可能是一个错误的相对URL,因为URL需要与脚本运行所在页面的URL相对,而不是与脚本相对,但解决方案是将其设置正确,而不仅仅是将其替换为绝对URLone@Quentin,你是对的,它总是被解释为一个URL,所以本质上它是一个URL,但他根据对目录结构的描述给出了相对于Javascript文件的文件系统路径。只有当他的web根URL指向他的js文件夹时,这才是正确的solution@Matteo你说得对。Carlos Vergara建议使用Silex,我也建议将其作为一个微观框架。老实说,我的建议是使用一个框架,当我对项目的需求一无所知时,我不想强迫自己对OP发表意见。但即使不使用框架,问题也可以解决,我猜OP不想使用框架。所以我想他会喜欢一个简单的solution@MarcoDinatsoli你的问题是你不知道$instance=newuser$结果=$instance->newUser;echo$result?使用一个框架。@MarcoDinatsoli你的类很好,但不好的是没有任何东西在使用它。类基本上是对象外观的打印输出。编写一些实际查询PHP请求的URL的内容,或者使用一些类似于我所指的小型库,并让您的代码使用该类生成一些对象。现在,您的代码和请求是分离的,没有任何东西可以将它们联系在一起。库总是更好,但您也可以使用ifstrpos$\u SERVER['REQUEST\u URI']、'/newUser'!==false{/*stuff*/}我只想调用那个函数Please,我不能使用任何路由或外部库,我只需要调用那个函数,你能帮我一下吗如果你不能编写自己的路由代码,而且你没有提供任何路由代码,你很可能会失败。调用该函数是什么意思?我需要从javascript ajax调用该函数。这很容易,请给我举一个开关/箱子的例子好吗?谢谢你能检查一下这个问题吗,它是一样的,你也可以在那里加上你的答案,用例子pleaseedited答案来反映你想做什么的简单方式。。Marco,我无法为你的问题添加答案,因为它被标记为与此问题重复;只要给我/新用户,我怎么能得到参数?在我的例子中,只有一个参数,就是名称