Welcome Guest ( Log In | Register)



 
Reply to this topicStart new topic
> How To Use A Link To Call Function In Php?
leiaah
post Jan 29 2006, 02:37 PM
Post #1


Super Member
*********

Group: Members
Posts: 436
Joined: 21-January 05
From: Koronadal City, Philippines
Member No.: 3,358



The title says it all, really. How do you call a function using <a href=" function name"> in PHP? I'm doing a project and I stumbled upon this problem. I don't want to use query string in the href part like <a href="?del()"> since that would mess up the other part of my code. Can anyone pleae help me? I've pasted the code below. smile.gif Thanksh.

CODE
<?php
  function display($x){
//coding goes here.
  }
?>

<html>
<body>
<p align="center">
<a href="what goes here?!">Display itmes</a>
</p>
</body>
</html>
Go to the top of the page
 
+Quote Post
moldboy
post Jan 29 2006, 08:04 PM
Post #2


Privileged Member
*********

Group: Members
Posts: 518
Joined: 29-April 05
From: Canada Eh?!?
Member No.: 6,408



I don't think you can, as PHP is rendered server side, you can with JavaScript. I can't tell if you've considered making your site go back to itself with a post var, something like index.php?function=what_you_want, then in the code only execute the function if the variable $function == "what_you_want".
Go to the top of the page
 
+Quote Post
Tyssen
post Jan 29 2006, 10:25 PM
Post #3



***********

Group: Members
Posts: 1,161
Joined: 9-May 05
From: Brisbane, QLD
Member No.: 6,818



Wouldn't it be something like this?

CODE
<a href="<? = nameOfYourFunction(variablePassedToTheFunction) ?>">Display items</a>
Go to the top of the page
 
+Quote Post
fffanatics
post Jan 29 2006, 10:45 PM
Post #4


Privileged Member
*********

Group: [HOSTED]
Posts: 937
Joined: 14-April 05
From: West Chester, PA
Member No.: 5,636



Ok i ran into the same problem when i was first creating my website hosted here at trap17. What you have to do is link back to the same page with either a post or a session variable now included. So your code for the website would stay the same except you would have an if statement checking to see if _SESSION[...] or _POST[...] existed. If it did then would now just run the function you wanted to run. So all you have to do is make the href="yourpage.php?login=yes"
Go to the top of the page
 
+Quote Post
Tyssen
post Jan 29 2006, 11:03 PM
Post #5



***********

Group: Members
Posts: 1,161
Joined: 9-May 05
From: Brisbane, QLD
Member No.: 6,818



QUOTE(fffanatics @ Jan 30 2006, 08:45 AM)
What you have to do is link back to the same page with either a post or a session variable now included. So your code for the website would stay the same except you would have an if statement checking to see if _SESSION[...]  or _POST[...] existed. If it did then would now just run the function you wanted to run. So all you have to do is make the href="yourpage.php?login=yes"

Eh? Why would you have to do that? If the function is based on a session or post variable, you might have to, but if it's not you wouldn't. For instance,

CODE
<?php
 function display($x){
echo ($x - 10);
 }
?>

No need for any session variables in there.
Go to the top of the page
 
+Quote Post
leiaah
post Jan 30 2006, 05:06 AM
Post #6


Super Member
*********

Group: Members
Posts: 436
Joined: 21-January 05
From: Koronadal City, Philippines
Member No.: 3,358



QUOTE(fffanatics @ Jan 30 2006, 06:45 AM)
Ok i ran into the same problem when i was first creating my website hosted here at trap17.  What you have to do is link back to the same page with either a post or a session variable now included. So your code for the website would stay the same except you would have an if statement checking to see if _SESSION[...]  or _POST[...] existed. If it did then would now just run the function you wanted to run. So all you have to do is make the href="yourpage.php?login=yes"
*



Can you give some sample coding here? I used a function because I don't want to redirect it to another page. The action is done in the function, in the page itself and not in another page. When I click the link, it should seem like nothing happened in the page but the function does its job.
Go to the top of the page
 
+Quote Post
Spectre
post Jan 30 2006, 03:43 PM
Post #7


Privileged Member
*********

Group: Members
Posts: 874
Joined: 30-July 04
Member No.: 246



It's not possible to call a function directly from a link, per se (as has been noted, PHP is server side), but you can construct a link to use GET variables to instruct the script to execute a certain function.

For example:

CODE
if( isset($_GET['function']) ) {
 switch( $_GET['function'] ) {
    case 'dosomething':
       dosomething();
    break;
    case 'dosomethingelse':
       dosomethingelse();
    break;
 }
}


And then link to script.php?function=dosomething

Hope that helps.

Oh, and Tyssen, using the link you originally supplied would cause nameOfYourFunction() to be executed whenever the script is, not just when the user clicks the link. Anything between <?php ?> (and, depending on server configurationm, <? ?>) is going to be processed as soon as the PHP engine encounters it within the script, so at the client's end, the link would end up pointing to whatever output nameOfYourFunction() returned (if any).
Go to the top of the page
 
+Quote Post
leiaah
post Feb 4 2006, 01:56 PM
Post #8


Super Member
*********

Group: Members
Posts: 436
Joined: 21-January 05
From: Koronadal City, Philippines
Member No.: 3,358



Thanks Spectre! I've tried it out and it's exactly what I want. I wish I had thought of it before. smile.gif
Go to the top of the page
 
+Quote Post
iGuest
post May 6 2008, 01:45 AM
Post #9


Trap Double Mocha Member
***************

Group: Members
Posts: 2,360
Joined: 21-September 07
Member No.: 50,369



Same PHP function link
How To Use A Link To Call Function In Php?

Hi, I've tried nearly everything on my script, my problem is that everytime I have the forma action (to get the isset($_get...) it reloads all my variables creating issues of course because I have two different forms. My script goes something like this (obviously I'm not applying syntax here):

Html:

Form action:process.Php method post
<input send>
<input preview>


No problems there.

Now the process.Php has several conditionals:

If(isset(send)){
SendFunction();
}

If(isset(preview)){
<input go back> this part works!
<input send> and here I need to use same function without resending everything
}

I can't use a form post in the last one because it'll process all script again. So I want to href link it to the function.

Please help!



-question by Taliaha

<