Hi guys,

I was wondering if it was possible to call a specific PHP function using AJAX instead of calling a whole page. An example:

function MakeRequest()
{
	var xmlHttp = getXMLHTTP();
 
	xmlHttp.onreadystatechange = function()
	{
		if(xmlHttp.readyState == 4)
		{
			document.getElementById('provinceDiv').innerHTML =
			xmlHttp.responseText;
		}
	}

	xmlHttp.open("GET", "FindProvinces.php", true);
	xmlHttp.send(null);
}

I already have a PHP function (in a class) that retrieves the provinces/states from the database based on the country. I was wondering if it was possible to call this function instead of having to make a new file (in this case FindProvinces.php) that calls the function. Thanks in advance.

I am sure PHP has some sort of MVC architecture implementation wherein the controller handles the requests and delegates it to an appropriate entity.

// Here /operations is mapped to a PHP file which acts as a 
// controller and performs the delegation activity based on
// the operation requested, here, find-provinces
xmlHttp.open("GET", "/operations/find-provinces", true);
xmlHttp.send(null);

Maybe posting this PHP related query in the PHP sub-forum might bring out some good responses since this seems more like a server-side url mapping/configuration issue.

You can call php functions with javascript and get responce back in javascript with jAPI Direct class.

jAPI Direct contains PHP and JavaScript script that enables you to call PHP methods direct from JavaScript just by typing their names.

For example you can call PHP method that looks like this:

class MySimpleMath {

public function Addition($firstParam, $seccondParam) {
$sum = $firstParam+$seccondParam; echo $sum;
}
}
...from JavaScript just with:

And you will have as a result: "3"

To use jAPI in your project you will have to fallow a few steps only.

First download jAPI project source from this address.

Make sure that you included in your html page all scripts properly. You also have to include this two scripts in your html file: jAPI.js, jAPI-Remote.php.

jAPI-Remote.php actually can be any php script from which you want to use their server side methods.

You have to include jAPI-Core.php script in this file (jAPI-Remote.php) by adding this line of code at the beggining of your script:

include("httpHandler/jAPI-CORE.php");

And at the end of your script you will have to create a new instance of jAPI Base Class like this:

//all classes names comma separated as jAPIBaseClass parameter which you want to use

new jAPIBaseClass('YourClass,AnotherClass');

So, that's it!

Your answer comes a bit late.
This thread had been dead for over a year and a half when you jumped in.

NO It was'nt late ,It helps me alot, thank you so much.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.