从JavaScript Ajax调用者调用PHP函数

时间:2014-12-10 21:01:43

标签: php jquery

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

文件夹是这样的:

controllers (folder)
-----User.php (php file)
View (folder)
-----js (folder)
---------myjavascriptfile.js

myjavascriptfile.js内 我有这个:

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

错误

我得到的错误是newUser不存在,尽管这是我的User.php

<?php

class User {
    function newUser($name){
        return "asdf";
    }
} 

抱歉,如果这是一个愚蠢的问题,我在没有框架的情况下不是那么好:))

4 个答案:

答案 0 :(得分:3)

以下是您的代码所发生的生命周期:

  1. JavaScript由浏览器解析
  2. 执行JavaScript
  3. JavaScript加载jQuery
  4. jQuery执行您发送的代码段。
  5. 代码从您的网络服务器请求资源&#34; ../ controllers / User.php / newUser&#34;
  6. 您的网络服务器返回404,因为该文件不存在,因为您声明不使用框架。
  7. 一个框架可以(如果你配置它)使该路由调用类上的方法,但是你没有这个,你只需要在PHP中定义一个类。它没有在任何地方初始化,也没有调用任何实例上的方法。

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

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

答案 1 :(得分:2)

我可以看到你正在尝试做什么,我认为你对如何调用函数有误解。将函数名称附加到URL不会强制php运行该函数。即点击index.php/hello将无法运行function hello() { echo "hello"; }

这不是php的工作原理..

你必须做的是阅读查询,即在PHP中读取传入的内容。如果你的url是user.php / newUser,那么你必须读取newUser正在传入,并在某种类型的switch语句中或者(路由器,调用此控制器和此方法的核心文件等)告诉PHP运行该方法。

编辑:

在全局变量$_SERVER['PATH_INFO']中你可以找到“路线”,即如果你点击了user.php / newUser那么全局变量/newUser

你可以做这样的事情,它读取函数名称(并删除斜杠),然后运行函数,如果它可以找到它:

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

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

答案 2 :(得分:1)

我认为你不确定你要求的是什么。你需要一个ajax查询来获取类中的php方法的数据。所以你在你的ajax查询中调用你的php文件,该php文件调用该类并在数据上运行该方法。

这是ajax访问类的方式。创建另一个名为user-process.php的文件,并将其放在controllers文件夹中。

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

}

?>

PHP在您的服务器上运行。 Javascript在客户端上运行。 Javascript不知道php文件在哪里,也不能直接调用它们的一部分,因为它没有直接访问权限,如果确实如此,你甚至不需要进行ajax调用。

用你的AJAX调用上面的脚本:

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

如果你想要包含某种切换机制,你可以像这样制作你的user-process.php文件:

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调用,您需要添加如下方法数据:

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

答案 3 :(得分:-2)

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

$.ajax({
        type: "GET",
        url: "http://example.com/controllers/User.php?newUser",
        data: { name: "John" }
    }).done(function( msg ) {
        alert( "Data Saved: " + msg );
    });

由于PHP无法访问您的Javascript且Javascript无法访问您的PHP,因此需要在调用的URL中提供所有内容。

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

也许您的User.php脚本可能是:

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