简体   繁体   中英

Call a PHP function with Ajax

I have a PHP page that has has the following in it:

<?php

class CallsClass {

    var $conn1;
    var $dataTable = "";

    function calls() {    
        $this->conn1 = new Datasource("taylor", "dbproj", "root", "elves") or die("Could not connect");

        $s1 = "SELECT id, UPPER(SUBSTRING_INDEX(fullname,' ',1)) as fullname, oldcode FROM `researcher` WHERE `display` = '1' AND fullname <> 'Jenny Porteous' AND fullname <> 'Carey-Lee Lendrum' AND fullname <> 'Carys Gibson'";
        $result = $this->conn1->_execute($s1);

        while ($row = $this->conn1->_nextAssocRow($result)) {
            $fullName = $row['fullname'];
            $dataTable .= $fullName;
        }
        echo json_encode($dataTable);
    }

}

?>

I know want to call this with my Ajax function:

$(document).ready(function() {
                    $(function () 
                    {
                        $.ajax({                                      
                            url: 'Queries/CallsQuery.php/calls',            
                            dataType: 'json',              
                            success: function(result)         
                            {
                                //console.log(result);
                            } 
                        });

                    });

I get an error of "OPTIONS file:///C:/Users/wesley/Desktop/Highcharts%20example/Queries/CallsQuery.php/calls() Resource failed to load" in the Google Chrome Dev Tools, any reason why?

Thanks, Wesley

The short version: Because you aren't using a web server.


Browsers do not support PHP. In the context of the WWW it is a server side programming language.

You need a web server to:

  • Interpet a URI which passes data beyond the end of the filename
  • Execute the PHP

Additionally, I'm not aware of PHP having the capability to treat having /function_name on the end of a URI as "Run this function inside the only class in the file".

It might have that feature, I'm not a PHP expert, but it seems a little bit too "Do what I might possibly mean" even for PHP.

The usual approach for this would be to have a controller handler that would check the URI and then execute the appropriate function.

Edit: Didn't even read the error, but Quentin is right: looks like php isn't doing anything: get xampp, read the docs and set up a quick server on your local machine, I'm recommending xampp because it's just dead-simple to set up, but doing it from scratch is more informative, as always

How would you expect this to work? A class is inert code, unless it's been instantiated. You're calling a Member function, which is in the script you send your request to. This script defines a class and, as far as it's concerned does a cracking job, and goes home for a refreshing beer.

You need an ajax script, that processes your request, creates an instance and then calls the appropriate member functions


Off topic, but still: on your jQuery code: why are you nesting the ready callback?

$(function ()

is short for

$(document).ready(function ()

So this doesn't make sense to me:

$(document).ready(function ()
{
    $(function ()
    {});
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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