HTML link to call PHP function

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
User avatar
verabug
Forum Newbie
Posts: 16
Joined: Tue Jun 11, 2002 7:53 pm
Location: San Francisco, CA
Contact:

HTML link to call PHP function

Post 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?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
User avatar
verabug
Forum Newbie
Posts: 16
Joined: Tue Jun 11, 2002 7:53 pm
Location: San Francisco, CA
Contact:

Post by verabug »

But I *am* using PHP to generate the HTML and Javascript. Hence the "echo." Is there still a way then?
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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.
User avatar
e+
Forum Commoner
Posts: 44
Joined: Mon Jun 17, 2002 7:07 am
Location: Essex, UK

Post 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
User avatar
verabug
Forum Newbie
Posts: 16
Joined: Tue Jun 11, 2002 7:53 pm
Location: San Francisco, CA
Contact:

Post 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?
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
User avatar
verabug
Forum Newbie
Posts: 16
Joined: Tue Jun 11, 2002 7:53 pm
Location: San Francisco, CA
Contact:

Post by verabug »

Okay, I think I got it now. :idea:
You guys rock! Thanks.
Post Reply