1,105,320 Community Members

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

Member Avatar
umcookeg
Newbie Poster
5 posts since Oct 2004
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
1
 

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.

Member Avatar
Gary King
PHP/vBulletin Guru
414 posts since Nov 2003
Reputation Points: 37 [?]
Q&As Helped to Solve: 7 [?]
Skill Endorsements: 0 [?]
Team Colleague
 
-1
 

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

Thanks!

Member Avatar
DanceInstructor
Posting Whiz
355 posts since Feb 2005
Reputation Points: 19 [?]
Q&As Helped to Solve: 15 [?]
Skill Endorsements: 0 [?]
 
0
 

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....

Member Avatar
nemo5
Newbie Poster
6 posts since Dec 2006
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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"
Member Avatar
R0bb0b
Posting Shark
995 posts since Jun 2008
Reputation Points: 344 [?]
Q&As Helped to Solve: 92 [?]
Skill Endorsements: 3 [?]
 
0
 

I would use an ajax call to the following script.

$allowedfuncs = array([allowed function calls])

if(in_array($_get['funccall'], $allowedfuncs))
{
    $func = $_get['funccall'];
    $output = $func();        // This calls $_get['funccall']()
    echo $output;
}
else
{
    exit();
}

/*
functions go here
*/

//Or something along those lines. Don't see a need for an iframe.

Member Avatar
winotosw
Newbie Poster
1 post since Aug 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

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.

Member Avatar
langsor
Posting Whiz
389 posts since Aug 2008
Reputation Points: 20 [?]
Q&As Helped to Solve: 36 [?]
Skill Endorsements: 0 [?]
 
0
 

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 ... :-)

Member Avatar
poste9
Newbie Poster
1 post since Dec 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
leadguitaralda
Newbie Poster
1 post since Jan 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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
include(**URL OF EXTERNAL PHP CODE**)
?>

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)

Member Avatar
vandenzergen
Light Poster
32 posts since Dec 2008
Reputation Points: -17 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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. :D

Member Avatar
LordWEB
Newbie Poster
8 posts since Jun 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi, you can try this ;)

http://code.google.com/p/japi-project/

With this you can direct call php functions from javascript ;)

Member Avatar
uros09
Newbie Poster
1 post since Jul 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

html.page

<head>
...
<script type="text/javascript" src="http://jquery.com/src/jquery-latest.js"></script>
...
</head>
<body>
...
<form action="" method="GET">
<div id="blacklist">
<input type="button" name="blacklist" id="button_blacklist" value="Blacklist" onClick="blacklist1()" />
</div>
</form>
...
<script type="text/javascript" >
function blacklist1(){
        var phone="12345678";
        alert(phone);
        var id="23";
        alert(id);
        $("#comandni_div").load("prenos.php?number="+phone+"&id="+id,function() {
        var reciverParameters = $("#comandni_div").html();
        var parser = new Array();
        parser = reciverParameters.split(",");
        alert(parser);
                });
}
...

</script>
...
<div id="comandni_div" style="width:1px; height:1px; overflow:hidden;"></div>
...

prenos.php

<?php
$number = $_GET['number'];
$id = $_GET['id'];
...
echo ("$number,$id");  // Push variables back to receiverParameters where we parse that
?>
Member Avatar
rajarajan07
Nearly a Posting Virtuoso
1,448 posts since May 2008
Reputation Points: 123 [?]
Q&As Helped to Solve: 242 [?]
Skill Endorsements: 3 [?]
 
0
 

You are not able to call the php function from javascript directly. May be Lordweb suggesstion will work for you with the help of that plugin. Or otherwise you can the call the php file from javascript using

location('update.php?id=2','_self');
Member Avatar
LordWEB
Newbie Poster
8 posts since Jun 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
1
 

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! :)

If you have any question, feel free to ask.
[email removed]

Member Avatar
EverWebby
Light Poster
46 posts since Jan 2010
Reputation Points: 1 [?]
Q&As Helped to Solve: 5 [?]
Skill Endorsements: 0 [?]
 
0
 

run them as eternal files communicating with each other with urlencoded query strings

Member Avatar
Dutchguy
Newbie Poster
1 post since Oct 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Why not use the easy-to-understand jQuery function $.ajax?

$(function(){
            $("input").keypress(function(){
                var name = $(this).next('#exception');
                $.ajax({
                    type: 'POST',
                    url: 'yourfile.php',
                    data: $(this).serialize(),
                    success: exception
                });
                function exception(data, status){
                    $(name).html(data);
                }
            });
        });
Member Avatar
rajeevphp2011
Light Poster
26 posts since Nov 2011
Reputation Points: -3 [?]
Q&As Helped to Solve: 6 [?]
Skill Endorsements: 1 [?]
 
0
 
You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article