944,150 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 160336
  • PHP RSS
You are currently viewing page 1 of this multi-page discussion thread
Mar 27th, 2005
1

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

Expand 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.
Similar Threads
Reputation Points: 10
Solved Threads: 1
Newbie Poster
umcookeg is offline Offline
5 posts
since Oct 2004
Mar 27th, 2005
0

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

Next time? DON'T POST THE SAME THREAD IN MULTIPLE FORUMS

Thanks!
Team Colleague
Reputation Points: 53
Solved Threads: 5
PHP/vBulletin Guru
Gary King is offline Offline
360 posts
since Nov 2003
Mar 27th, 2005
0

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

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....
Reputation Points: 17
Solved Threads: 14
Posting Whiz
DanceInstructor is offline Offline
355 posts
since Feb 2005
Dec 31st, 2006
0

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

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:
PHP Syntax (Toggle Plain Text)
  1. style="visibility: hidden"
Reputation Points: 10
Solved Threads: 0
Newbie Poster
nemo5 is offline Offline
6 posts
since Dec 2006
Jun 11th, 2008
0

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

I would use an ajax call to the following script.

php Syntax (Toggle Plain Text)
  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 8:51 am. Reason: Keep It Organized - please use [code] tags
Reputation Points: 358
Solved Threads: 89
Posting Shark
R0bb0b is offline Offline
986 posts
since Jun 2008
Aug 12th, 2008
0

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

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

Click to Expand / Collapse  Quote originally posted by umcookeg ...
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
winotosw is offline Offline
1 posts
since Aug 2008
Aug 12th, 2008
0

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

Click to Expand / Collapse  Quote originally posted by nemo5 ...
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:
PHP Syntax (Toggle Plain Text)
  1. 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
PHP Syntax (Toggle Plain Text)
  1. <html>
  2. <head>
  3. <script type="text/javascript">
  4. window.onload = init_loader;
  5.  
  6. function init_loader (){
  7. var head = document.getElementsByTagName('head').item(0);
  8. document.getElementById('loader').onclick = function () {
  9. var script = document.createElement('script');
  10. script.setAttribute( 'type', 'text/javascript' );
  11. script.setAttribute( 'src', 'remote.php?value=my message' );
  12. head.insertBefore( script, head.firstChild );
  13. };
  14. };
  15. </script>
  16. </head>
  17. <body>
  18. </body>
  19. <a id="loader" href="#">Load Script</a>
  20. </html>

page: remote.php
PHP Syntax (Toggle Plain Text)
  1. <?php
  2.  
  3. // get the request
  4. $query = $_GET['value'];
  5. $query = "'".addslashes( $query )."'";
  6.  
  7. // print back as plain javascript
  8. print <<<ENDLINE
  9. alert( $query );
  10. ENDLINE;
  11.  
  12. ?>

What ever way you do it should be a learning experience ... :-)
Last edited by langsor; Aug 12th, 2008 at 8:20 pm.
Reputation Points: 30
Solved Threads: 36
Posting Whiz
langsor is offline Offline
389 posts
since Aug 2008
Dec 16th, 2008
0

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

Click to Expand / Collapse  Quote originally posted by umcookeg ...
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.
You can do this easily using JRC method without soap or xml-rpc... only JSON and a phew of OOP code.

see it in action at: call php with javascript demo page
this code is explained at call php with javascript
Reputation Points: 10
Solved Threads: 0
Newbie Poster
poste9 is offline Offline
1 posts
since Dec 2008
Jan 23rd, 2010
0
Re: How to call a PHP function from Javascript and return the results back into Javascrip
Click to Expand / Collapse  Quote originally posted by umcookeg ...
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.
I am currently trying to find out if it is possible to call a php function from javascript, but I am currently unsuccessful. I do however know how to fix your problem
use the include() function.

put the following into the head section of your page
PHP Syntax (Toggle Plain Text)
  1. <?php
  2. include(**URL OF EXTERNAL PHP CODE**)
  3. ?>
its as simple as that
You can also use require() instead
the difference is that if there is an error with getting the external script, it will stop running the php.
(you can include php, javascript, html, and pretty much everything else with the include() function)
Reputation Points: 10
Solved Threads: 0
Newbie Poster
leadguitaralda is offline Offline
1 posts
since Jan 2010
Jan 25th, 2010
0
Re: How to call a PHP function from Javascript and return the results back into Javascrip
Did you mean that... ahm...

How can PHP and JavaScript act like they are friends and communicate to each other like they can share their variables at anytime they want?
I mean ahm, pass the data from PHP to Javascript and Javascript to PHP? Is that what you meant?

If Yes, then I think that is very possible. If No, then I don't know how to help you.
Reputation Points: -7
Solved Threads: 0
Light Poster
vandenzergen is offline Offline
32 posts
since Dec 2008
Message:
Previous Thread in PHP Forum Timeline: how to update a password in mysql using procedure
Next Thread in PHP Forum Timeline: Can anyone help me about fpdf multicell problem?





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


Follow us on Twitter


© 2011 DaniWeb® LLC