Forum Moderators: coopster

Message Too Old, No Replies

Calling a function

Another simple one...

         

madcat

4:53 pm on Apr 14, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This should be in the newbie section, but since it is PHP... is it not possible to call a function from a normal HTML link? My function is in index.php, the link is in the same file...I want to call the function through that link...

<a href="callFunction?" title=""></a>

Basically, how through a link can I refresh the page and pass the function variables.

httpwebwitch

5:46 pm on Apr 14, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



To pass variables into a function when the page is reloaded, you'll put them at the end of your URL, after a question mark (?). This part of the URL is usually called the "querystring", and it is accessed using the "GET" method. (those are two terms worth remembering).

Querystrings begin with a "?", and are made up of "name=value" pairs separated by ampersands (&).

your link will look something like this:
<a href="index.php?var1=foo&var2=bar">hello world</a>

When the page is reloaded with the querystring, your variables can be accessed in the $HTTP_GET_VARS array:

<?php
echo $HTTP_GET_VARS['var1']; // prints "foo"
echo $HTTP_GET_VARS['var2']; // prints "bar"
?>

so, if you want these values sent into a function you put this in your script:

<?php
dothis($HTTP_GET_VARS['var1'],$HTTP_GET_VARS['var2']);

function dothis($var1,$var2){
echo $var1." ".$var2;
}
?>

I hope this helps.

[edited by: jatar_k at 5:47 pm (utc) on April 14, 2004]
[edit reason] removed last comment [/edit]

jatar_k

5:49 pm on Apr 14, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You can't call a function from the client side as you demonstrate. javascript could do that since it is a client side language. PHP is a server side language so with out sending data to the server it knows nothing about what is going on and can have no influence on it.