Jump to content
xisto Community
leiaah

How To Use A Link To Call Function In Php?

Recommended Posts

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>

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

Ok i ran into the same problem when i was first creating my website hosted here at Xisto. 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"

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

Ok i ran into the same problem when i was first creating my website hosted here at Xisto.  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"

225159[/snapback]


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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Is there any way call JAVASCRIPT function in PHP without any event How To Use A Link To Call Function In Php?

Is there any way call JAVASCRIPT function in PHP without any event

I am trying to call JAVASCRIPT function in php code what there is problem  with  the given function is  called automatically after some interval..

anyone please help me

Thank

-reply by ajay

Share this post


Link to post
Share on other sites
Go to another page by onclick()How To Use A Link To Call Function In Php?

I have a form.There I have written 2 radio buttons & 2 submit buttons.When I click on the radio button,its respective submit button is activated.But the problem is that,when I click the submit button,it doesn't open the page that I require.Also there is another problem.When I click on one radio button,its submit button is activate,but I want the other submit button should not be visible.

-reply by Smruti Ranjan Patri

Share this post


Link to post
Share on other sites

This post is very old but I would thank you, it's help me too :-)
Thanks.

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

Share this post


Link to post
Share on other sites

id just like to say the same, old post but helped with exactly what i wanted to do

thanks!




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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.