Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Call Function?

Status
Not open for further replies.

james0816

Programmer
Jan 9, 2003
295
US
Can you create and call a function using PHP like you can in Java?

Example:

<?
Function test()
{ echo 'HI!'; }
?>

<a href="#" onclick="test()">CLICK</a>

 
Sorry, I thought the last line of your post was a signature line.

No, you can't do that. A onclick is a JavaScript, VBScript or other client-side scripting language construct. PHP runs on the server. There is no way to call PHP code that way.

You must submit data to a PHP script and accept the return. This can be done through links, forms, or something like AJAX.



Want the best answers? Ask the best questions! TANSTAAFL!
 
i was about to say.....didn't see it anywhere in the manual which lead to my post. thx anyway.
 
I'd guess, since PHP is case sensitive, that using Function rather than [red]f[/red]unction would cause an error in the server side scripting, too.

Lee
 
lol...my biggest code killers....

case sensitive and leaving out the ";
 

the following is an excerpt from the link posted by sleipnir214 above

Note: Function names are case-insensitive, though it is usually good form to call functions as they appear in their declaration.
 
James,

Back to your question:

The difference (well, at least one difference) between JS and PHP is :

JS is interpreted by the browser -ie. client side. Could be on the same webpage (dynamic) or new webpage.

PHP is interpreted by the server -ie. serverside; always new webpage.

So what does this mean? Well, to do what you imply, this is how:

A HTML file containing this:
Code:
<a href="test.php" >CLICK</a>

... will call the php script test.php:

Code:
<?
function test() {
  echo 'HI!';
}

test();
?>

This will result in a new document displaying (only):

Code:
HI!

Does this answer your question?

Regards
 
the more robust, and IMHO proper, way to look at this is, as dkdude implies, that Javascript will only operate on the client side, and PHP will only operate on the server side. However, there are multiple ways for the two to "interact".

one way is like dkdude implies, that javascript, or simply HTML <a> tag clicking, can send a request via the browser to the server php script, and based on any number of factors, including script name, or parameters passed, the script can determine which PHP function to execute.

AJAX (asynchronous javascript and xml) is another example of a more complicated, but in some ways, more elegant, way to look at your problem.

Basically, the premise is that you would have Javascript make a background call (not a full page refresh like the previous example) to a PHP script, and again, give it some info about what function to execute. the difference would be that the PHP script would transparently return it's results to your javascript, which could then decide what to do with it (display it, parse it, etc).

one way that i've seen things like this implemented is in special PHP scripts which take a set of your PHP functions (or class objects) and automatically outputs dynamically created javascript that will, using AJAX methods, have basically stub functions in your javascript with the same name as the corresponding PHP functions/object methods. In that way, you simple call the function in Javascript, and it takes care of bridging the gap and sending that call to PHP, which executes the appropriate function, and returns the result to your javascript function, which then gives it back to you.

While complicated and not for the faint-of-heart, this is the closest we can come to giving you a solution that is really robust and extensible, and thus most likely to be useable for whatever it is you are trying to do.

Check out this package for an example of what i'm talking about:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top