calling a function from a link?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lexx
Forum Newbie
Posts: 21
Joined: Sat Jun 08, 2002 10:30 pm

calling a function from a link?

Post by lexx »

Say I have just a simple function that just outputs a string (for now)
First I out a link that suposed to call a function but it doesnt work...?
Anyone know how to do that?

Code: Select all

<?
echo "<a href="$PHP_SELF?call_user_func(test,'')">Call a function</a>";
function test()
&#123;
echo "WOW IT JUST CALLED A FUNCTION";
&#125;
?>
EricS
Forum Contributor
Posts: 183
Joined: Thu Jul 11, 2002 12:02 am
Location: Atlanta, Ga

Post by EricS »

Since you are calling the page itself in the url, then you could set a variable in the url rather than calling the function. Then create a switch statement that checks that variable and when it finds the value you specified it will call the function.

Code: Select all

<? 
echo "<a href="$PHP_SELF?id=1">Call a function</a>"; 

switch ($id) &#123;
case $id == 1:
test();
break;
&#125;

function test() 
&#123; 
echo "WOW IT JUST CALLED A FUNCTION"; 
&#125; 
?>
Remember, no code is run after the server sends the page to the browser. If you want code to run client side you will have to learn a client side language like Javascript.
Post Reply