leiaah 0 Report post Posted January 29, 2006 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
moldboy 0 Report post Posted January 29, 2006 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
Tyssen 0 Report post Posted January 29, 2006 Wouldn't it be something like this? <a href="<? = nameOfYourFunction(variablePassedToTheFunction) ?>">Display items</a> Share this post Link to post Share on other sites
fffanatics 0 Report post Posted January 29, 2006 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
Tyssen 0 Report post Posted January 29, 2006 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
leiaah 0 Report post Posted January 30, 2006 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
Spectre 0 Report post Posted January 30, 2006 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=dosomethingHope 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
leiaah 0 Report post Posted February 4, 2006 Thanks Spectre! I've tried it out and it's exactly what I want. I wish I had thought of it before. Share this post Link to post Share on other sites
iGuest 3 Report post Posted May 6, 2008 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
iGuest 3 Report post Posted August 8, 2009 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
iGuest 3 Report post Posted December 26, 2009 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
iGuest 3 Report post Posted January 28, 2010 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
anoroll 0 Report post Posted April 30, 2011 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=dosomethingHope 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
iGuest 3 Report post Posted November 2, 2011 (edited) several ways:1- ajax // no need to reloade your page2-$_GET (ex: if($_GET[x]==1)func(); or if(!empty($_GET[x]))func($_GET[x]);3-javascript Edited April 27, 2012 by moderator (see edit history) Share this post Link to post Share on other sites
iGuest 3 Report post Posted July 26, 2012 id just like to say the same, old post but helped with exactly what i wanted to dothanks! 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=dosomethingHope 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