Page 1 of 1

calling a function from a link?

Posted: Sat Aug 03, 2002 9:15 pm
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;
?>

Posted: Sun Aug 04, 2002 12:23 am
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.