Jump to content



Welcome to KnowledgeSutra - Dear Guest , Please Register here to get Your own website. - Ask a Question / Express Opinion / Reply w/o Sign-Up!
- - - - -

How To Use A Link To Call Function In Php?


13 replies to this topic

#1 leiaah

    Super Member

  • Kontributors
  • PipPipPipPipPipPipPipPipPip
  • 436 posts
  • Location:Koronadal City, Philippines
  • Interests:music, web stuff, movies, pop culture

Posted 29 January 2006 - 02:37 PM

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. :) Thanksh.

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

<html>
<body>
<p align="center">
	<a href="what goes here?!">Display itmes</a>
</p>
</body>
</html>


#2 moldboy

    Privileged Member

  • Kontributors
  • PipPipPipPipPipPipPipPipPip
  • 516 posts
  • Location:Canada Eh?!?

Posted 29 January 2006 - 08:04 PM

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".

#3 Tyssen

  • Kontributors
  • PipPipPipPipPipPipPipPipPipPipPip
  • 1,161 posts
  • Location:Brisbane, QLD

Posted 29 January 2006 - 10:25 PM

Wouldn't it be something like this?

<a href="<? = nameOfYourFunction(variablePassedToTheFunction) ?>">Display items</a>


#4 fffanatics

    Privileged Member

  • Kontributors
  • PipPipPipPipPipPipPipPipPip
  • 936 posts
  • Location:West Chester, PA

Posted 29 January 2006 - 10:45 PM

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"

#5 Tyssen

  • Kontributors
  • PipPipPipPipPipPipPipPipPipPipPip
  • 1,161 posts
  • Location:Brisbane, QLD

Posted 29 January 2006 - 11:03 PM

fffanatics, on Jan 30 2006, 08:45 AM, said:

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,

<?php
  function display($x){
echo ($x - 10);
  }
?>
No need for any session variables in there.

#6 leiaah

    Super Member

  • Kontributors
  • PipPipPipPipPipPipPipPipPip
  • 436 posts
  • Location:Koronadal City, Philippines
  • Interests:music, web stuff, movies, pop culture

Posted 30 January 2006 - 05:06 AM

fffanatics, on Jan 30 2006, 06:45 AM, said:

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"

View Post


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.

#7 Spectre

    Privileged Member

  • Kontributors
  • PipPipPipPipPipPipPipPipPip
  • 873 posts

Posted 30 January 2006 - 03:43 PM

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:

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).

#8 leiaah

    Super Member

  • Kontributors
  • PipPipPipPipPipPipPipPipPip
  • 436 posts
  • Location:Koronadal City, Philippines
  • Interests:music, web stuff, movies, pop culture

Posted 04 February 2006 - 01:56 PM

Thanks Spectre! I've tried it out and it's exactly what I want. I wish I had thought of it before. :rolleyes:

#9 iGuest

    Hail Caesar!

  • Kontributors
  • PipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPip
  • 5,876 posts
  • Interests:Trap17 Free Web Hosting, No Ads

Posted 06 May 2008 - 01:45 AM

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

#10 iGuest

    Hail Caesar!

  • Kontributors
  • PipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPip
  • 5,876 posts
  • Interests:Trap17 Free Web Hosting, No Ads

Posted 08 August 2009 - 07:21 PM

How To Use A Link To Call Function In Php?How To Use A Link To Call Function In Php?

Lol.  Yeah, in case you haven't noticed, all PHP code never leaves the server.  If you expect a browser to ever be able to interpret that, well, let's just put it this way: Browsers themselves don't ever see any php code, and therefore have no parsers/runtimes for such.






Reply to this topic


This post will need approval from a moderator before this post is shown.

  


1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users