User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 391,590 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,678 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 39562 | Replies: 6
Reply
Join Date: Oct 2004
Posts: 5
Reputation: umcookeg is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 1
umcookeg umcookeg is offline Offline
Newbie Poster

How to call a PHP function from Javascript and return the results back into Javascrip

  #1  
Mar 27th, 2005
Hi,

I have been working on trying to figure out, how I can get a web page to call a PHP function and have the results returned to the client for processing, for about two weeks. I hope I am in the right forum (ie. Javascript). I will also post in PHP and SOAP forums as well just to be sure.
Here is the issue

PS: I don't have code to post because I really don't know how to implement this.

1. UserA loads a webpage from ServerA. In it it has a javascript function
which "on web page loading" calls a remote function on another web server, ServerB. This remote function is written in PHP and accesses a MySQL database, the results of the DB query is an array.
2. This array gets returned by the remote server to the javascript function that made the initial call. The array then gets processed by the javascript function.

Now I know that Javascript is client side and PHP is server side. I also discovered that to make this happen I will need to use some sort of RPC functionality. Some sites/ideas that I have found on the net are............

1. XML-RPC www.ashleyIT.com using JSRS
2. and possibly SOAP.

I have spent the last few days trying to get ashleyIT's XML-RPC implementation to work but the documentation is scarce, the site and forum look like they aren't being used anymore, and I am starting to wonder if there is not a more popular/modern implementation that I should be considering.

Question

So now the question. Does anyone know what implementation of RPC I could use? I am researching SOAP but so far I have not found a SOAP implementation using Javascript calling PHP functions. Is there anything else that I might be able to use other than XML-RPC and SOAP?


Thanks for your time.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Nov 2003
Location: Toronto, Canada
Posts: 354
Reputation: Gary King will become famous soon enough Gary King will become famous soon enough 
Rep Power: 6
Solved Threads: 5
Colleague
Gary King's Avatar
Gary King Gary King is offline Offline
PHP/vBulletin Guru

Re: How to call a PHP function from Javascript and return the results back into Javas

  #2  
Mar 27th, 2005
Next time? DON'T POST THE SAME THREAD IN MULTIPLE FORUMS

Thanks!
Reply With Quote  
Join Date: Feb 2005
Posts: 354
Reputation: DanceInstructor is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 12
DanceInstructor's Avatar
DanceInstructor DanceInstructor is offline Offline
Posting Whiz

Re: How to call a PHP function from Javascript and return the results back into Javas

  #3  
Mar 27th, 2005
Maybe you could use an iframe? You can make the frame small so the user may not even know it is there. Load a form into it initially to call your php function and then read the results with the javascript once it reloads....
Clear Mind Hosting and Web Design

If I've helped you please consider adding to my reputation.
Reply With Quote  
Join Date: Dec 2006
Posts: 6
Reputation: nemo5 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
nemo5 nemo5 is offline Offline
Newbie Poster

Solution Re: How to call a PHP function from Javascript and return the results back into Javas

  #4  
Dec 31st, 2006
The best way would be to make an ajax call, but the proposed iframe solution would work as well. You don't even have to make the iframe small, just set it's style attribute to:
style="visibility: hidden"
For a man without a destination no wind is favourable

http://www.vydavatelstvo-f.sk
Reply With Quote  
Join Date: Jun 2008
Location: Phoenix, AZ
Posts: 534
Reputation: R0bb0b is on a distinguished road 
Rep Power: 2
Solved Threads: 50
R0bb0b's Avatar
R0bb0b R0bb0b is offline Offline
Posting Pro

Re: How to call a PHP function from Javascript and return the results back into Javas

  #5  
Jun 11th, 2008
I would use an ajax call to the following script.

  1. $allowedfuncs = array([allowed function calls])
  2.  
  3. if(in_array($_get['funccall'], $allowedfuncs))
  4. {
  5. $func = $_get['funccall'];
  6. $output = $func(); // This calls $_get['funccall']()
  7. echo $output;
  8. }
  9. else
  10. {
  11. exit();
  12. }
  13.  
  14. /*
  15. functions go here
  16. */

//Or something along those lines. Don't see a need for an iframe.
Last edited by peter_budo : Jun 16th, 2008 at 7:51 am. Reason: Keep It Organized - please use [code] tags
Reply With Quote  
Join Date: Aug 2008
Posts: 1
Reputation: winotosw is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
winotosw winotosw is offline Offline
Newbie Poster

Re: How to call a PHP function from Javascript and return the results back into Javas

  #6  
8 Days Ago
Please see the http://satoewarna.com/jqsajax/ it will help you in calling PHP function/method from javascript.

Originally Posted by umcookeg View Post
Hi,

I have been working on trying to figure out, how I can get a web page to call a PHP function and have the results returned to the client for processing, for about two weeks. I hope I am in the right forum (ie. Javascript). I will also post in PHP and SOAP forums as well just to be sure.
Here is the issue

PS: I don't have code to post because I really don't know how to implement this.

1. UserA loads a webpage from ServerA. In it it has a javascript function
which "on web page loading" calls a remote function on another web server, ServerB. This remote function is written in PHP and accesses a MySQL database, the results of the DB query is an array.
2. This array gets returned by the remote server to the javascript function that made the initial call. The array then gets processed by the javascript function.

Now I know that Javascript is client side and PHP is server side. I also discovered that to make this happen I will need to use some sort of RPC functionality. Some sites/ideas that I have found on the net are............

1. XML-RPC www.ashleyIT.com using JSRS
2. and possibly SOAP.

I have spent the last few days trying to get ashleyIT's XML-RPC implementation to work but the documentation is scarce, the site and forum look like they aren't being used anymore, and I am starting to wonder if there is not a more popular/modern implementation that I should be considering.

Question

So now the question. Does anyone know what implementation of RPC I could use? I am researching SOAP but so far I have not found a SOAP implementation using Javascript calling PHP functions. Is there anything else that I might be able to use other than XML-RPC and SOAP?


Thanks for your time.
Reply With Quote  
Join Date: Aug 2008
Posts: 236
Reputation: langsor is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 20
langsor langsor is offline Offline
Posting Whiz in Training

Re: How to call a PHP function from Javascript and return the results back into Javas

  #7  
8 Days Ago
Originally Posted by nemo5 View Post
The best way would be to make an ajax call, but the proposed iframe solution would work as well. You don't even have to make the iframe small, just set it's style attribute to:
style="visibility: hidden"
Worth considering if you do this, is that 'hidden' elements still affect the page flow and layout -- thus creating a break in the flow of visible elements.

I don't have experience in XML-RPC or SOAP but if I understand correctly, the HttpRequest object does not allow cross-site requests. I believe this is going to be included in future releases of some browsers though. Correct me if I'm wrong here.

You can always make an Ajax call to a locally hosted PHP script and have it query the remotely served database.

You can make an Ajax query to a locally hosted PHP script and have it make a call to a remotely hosted PHP script and set up a conversation that way.

You can use an iframe as mentioned above

Of course there are a multitude of Ajax libraries available these days -- not all created equal though in terms of robustness or ease of implementation. Or you could learn how to roll your own and gain invaluable experience that way.

Another fun non-Ajax (read old-skool) trick is to use JavaScript to call the PHP file as a JavaScript include file -- see example bellow.

page: test.html
<html>
<head>
<script type="text/javascript">
window.onload = init_loader;

function init_loader (){
  var head = document.getElementsByTagName('head').item(0);
  document.getElementById('loader').onclick = function () {
    var script = document.createElement('script');
    script.setAttribute( 'type', 'text/javascript' );
    script.setAttribute( 'src', 'remote.php?value=my message' );
    head.insertBefore( script, head.firstChild );
  };
};
</script>
</head>
<body>
</body>
<a id="loader" href="#">Load Script</a>
</html>

page: remote.php
<?php

// get the request
$query = $_GET['value'];
$query = "'".addslashes( $query )."'";

// print back as plain javascript
print <<<ENDLINE
alert( $query );
ENDLINE;

?>

What ever way you do it should be a learning experience ... :-)
Last edited by langsor : 8 Days Ago at 7:20 pm.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb PHP Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the PHP Forum

All times are GMT -4. The time now is 11:14 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC