Page 1 of 1

HTML link to call PHP function

Posted: Mon Jun 17, 2002 7:36 pm
by verabug
I have a PHP file that dynamically creates some HTML links. When one of the links is clicked, I would like to call a PHP function that lives in the same file.
Here is my PHP statement that creates a link:

Code: Select all

echo "<a href='approve.php'>approve</a>";
Instead of calling the file approve.php, I would like to call a PHP function called approve(). How do I do that?

Posted: Mon Jun 17, 2002 8:06 pm
by volka

Posted: Tue Jun 18, 2002 1:49 am
by twigletmac
Just to reiterate, PHP is server-side so all the processing happens on the server not in the user's browser. Unlike Javascript which is client-side (processing done in user's browser effectively), you can't access functions or variables once the page has loaded unless you've used PHP to write them into Javascript.

Mac

Posted: Tue Jun 18, 2002 1:54 am
by verabug
But I *am* using PHP to generate the HTML and Javascript. Hence the "echo." Is there still a way then?

Posted: Tue Jun 18, 2002 2:04 am
by twigletmac
No. You cannot access a PHP function this way. Look at the source code when the page has loaded, HTML, Javascript but no PHP. When you generate a page you can echo PHP variables into Javascript functions in order to access those variables using Javascript. If you want access to a PHP function you need to go back to the server, ie. refresh the page, post a form...

Mac

Edit:
the php manual wrote:What distinguishes PHP from something like client-side JavaScript is that the code is executed on the server. If you were to have a script ... on your server, the client would receive the results of running that script, with no way of determining what the underlying code may be.

Posted: Tue Jun 18, 2002 3:21 am
by e+
As twigletmac says php is server side and you can't run it client side. This sort of thing sounds like you are trying to log the activity of certain links on your page. A common solution to this problem is to have the link go to a php script rather than directly out to the page intended. You don't have to keep the people on the page you are sending them to just have it run your php script you mention and redirect them to the url you want. You can even pass the url they are to be directed to in the url of the link to the php page you want to run. ie you link to

refreshme.php?url=www.somesite.com

hope this helps

Posted: Tue Jun 18, 2002 8:21 am
by verabug
Thank you all for your responses.
Will I have to access the variable url=www.somesite.com with the $_POST array again when it's sent from one PHP script to another?

Posted: Tue Jun 18, 2002 8:29 am
by twigletmac
No, the $_POST array is essentially for accessing information sent via an HTML form using the POST method. To access information in the query string of the URL you need to use the $_GET array.

Code: Select all

$_GET&#1111;'url']
Mac

Posted: Tue Jun 18, 2002 9:18 am
by verabug
Okay, I think I got it now. :idea:
You guys rock! Thanks.