943,722 Members | Top Members by Rank

Ad:
Dec 23rd, 2008
0

Call a specific PHP function with AJAX?

Expand Post »
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:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. function MakeRequest()
  2. {
  3. var xmlHttp = getXMLHTTP();
  4.  
  5. xmlHttp.onreadystatechange = function()
  6. {
  7. if(xmlHttp.readyState == 4)
  8. {
  9. document.getElementById('provinceDiv').innerHTML =
  10. xmlHttp.responseText;
  11. }
  12. }
  13.  
  14. xmlHttp.open("GET", "FindProvinces.php", true);
  15. xmlHttp.send(null);
  16. }

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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bdicasa is offline Offline
7 posts
since Sep 2008
Dec 24th, 2008
0

Re: Call a specific PHP function with AJAX?

I am sure PHP has some sort of MVC architecture implementation wherein the controller handles the requests and delegates it to an appropriate entity.
javascript Syntax (Toggle Plain Text)
  1. // Here /operations is mapped to a PHP file which acts as a
  2. // controller and performs the delegation activity based on
  3. // the operation requested, here, find-provinces
  4. xmlHttp.open("GET", "/operations/find-provinces", true);
  5. 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.
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,871 posts
since Jun 2006
Jul 7th, 2010
1
Re: Call a specific PHP function with AJAX?
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!
Reputation Points: 10
Solved Threads: 1
Newbie Poster
LordWEB is offline Offline
8 posts
since Jun 2010
Jul 7th, 2010
0
Re: Call a specific PHP function with AJAX?
Your answer comes a bit late.
This thread had been dead for over a year and a half when you jumped in.
fxm
Reputation Points: 40
Solved Threads: 74
Posting Pro
fxm is offline Offline
591 posts
since Apr 2010
Aug 1st, 2010
0
Re: Call a specific PHP function with AJAX?
NO It was'nt late ,It helps me alot, thank you so much.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
tarmin68 is offline Offline
1 posts
since Jul 2010

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: Why I cannot play a sound file when i click the button when the player is hidden?
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: How to get Javascript script path?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC