1

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

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

got my vote for your latest edit. was half way through writing that out in an answer :)
Note for OP: you can't call php scripts with relative urls if you're not loading your web pages through a web server. You very well might have a web server, but if you're loading your html from c:\\Users\...\myhtml.html the relative path will point to c:\\Users\...\myphp.php which bypasses the web server (which will be listening on 127.0.0.1:8888 presumably)
@Quentin: a little bit too "Do what I might possibly mean" cracked me up! +1 for that... and No, it doesn't support that kind of requests, unless you use a framework (which means a lot of other stuff has to happen first)
2

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 ()
    {});
});

2 Comments

Looks like the error is a web server one, but the next problem he was going to encounter was the class methods one. :p
Most likely... this code is a bit like saying "I've got the blueprint of a house, but when it rains, I'm still getting wet"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.