Hi,
I'd like to call a php function when a link is clicked in a web page.
I'm wondering if it can be done similar to calling javascript functions:
Or however?PHP Code:<a href='#' onclick='function_name()'>Click Me</a>
Thanks
| SitePoint Sponsor |
Hi,
I'd like to call a php function when a link is clicked in a web page.
I'm wondering if it can be done similar to calling javascript functions:
Or however?PHP Code:<a href='#' onclick='function_name()'>Click Me</a>
Thanks





Nope. PHP is a preprocessor; all PHP is parsed before the page is served. You will have to either
- Do it in JavaScript
- Open a new window and run the function (no changes will show on teh calling page until it reloads) or
- Load a new page.
TuitionFree — a free library for the self-taught
Anode Says... — Blogging For Your Pleasure
What would have to be in the HTML code to call a SQL query from a webpage by clicking on a button?
"<a href="dowhatever.php">Click me</a>"
what, dowhatever.php, does, is up to you.
Oh ok. I understand. So the .php file will have my sql code in it correct? Can I just paste the sql code right into the .php file or are there other things that I have to do to it?
on javascript:
<a href='javascript:function_name()'>Click Me</a>





You'll need to establish a connection to the database, do the SQL query, process the results, or display them and finally allow your visitor somewhere to go. As well as all the normal error catching stuff.Originally Posted by Charles
A Little Knowledge Is A Very Dangerous Thing.......
That Makes Me A Lethal Weapon !!!!!!!!
Contract PHP Programming


So if you have a few functions, you need a few php scripts... as in:
"<a href="dofunction1.php">Click me for function 1</a>"
"<a href="dofunction2.php">Click me for function 2</a>"
"<a href="dofunction3.php">Click me for function 3</a>"
If you make a file with all your functions in it... "functions.php" and you want to call the separate functions in that file, is that possible?
as in something like this:
"<a href="functions.php?target=function1">Click me for function 1</a>"
"<a href="functions.php?target=function2">Click me for function 2</a>"
"<a href="functions.php?target=function3">Click me for function 3</a>"
or something like this:
<?php
echo "click <a href=\"" . function1() . "\">me</a> for the function";
?>
This thread is almost 6 years old
How did you find it?![]()
Guido - Community Team Advisor
Do you know where the (database) error is? Add it to the list!
Thinking Web: Voices of the Community
Blog - Free Flash Slideshow Widget

I don't know exactly what you are after, but if this to refresh a part of page in the Ajax manner then you could take a look at the Xajax project.
That takes care of the JS for you so you just do;
<a onclick"xajax.myfunction('argument')">Is this a five minute argument?</a>
and it calls this on the backend:
Requires a bit of setting up though, and a bit of a change of mind-setPHP Code:function myfunction(){
//
return "yes";
}
and reasonable JS and PHP skills.
Otherwise bulevardi's comments are sound.


Well, I didn't want to make a new thread for a topic that's already made up. So I used the search function
There were also 2 other threads for this topic, but they didn't get me an answer. This one had the most replies and I thought to bump this one instead of making a new one.

But what is your question then?but they didn't get me an answer.
I didn't even notice you were asking something, I thought you were giving an answer
No, not necessarily. As you say immediately after, you can put all functions in one script:
Yes, this is possible. But in functions.php you'll have to accept 'target' and elaborate it's value, calling the right function. The function won't be called automatically.If you make a file with all your functions in it... "functions.php" and you want to call the separate functions in that file, is that possible?
as in something like this:
"<a href="functions.php?target=function1">Click me for function 1</a>"
"<a href="functions.php?target=function2">Click me for function 2</a>"
"<a href="functions.php?target=function3">Click me for function 3</a>"
All PHP code is executed BEFORE the page is sent to the client. Here function1() will be replaced with the result of that function call. It doesn't mean that function1() will be executed when the user clicks on the link.or something like this:
<?php
echo "click <a href=\"" . function1() . "\">me</a> for the function";
?>
Guido - Community Team Advisor
Do you know where the (database) error is? Add it to the list!
Thinking Web: Voices of the Community
Blog - Free Flash Slideshow Widget


There's something like this:
But than you need a form everytime as anchor
And the input gives buttons...
Code:<?php function function1() { if(!empty($_POST['send'])) { // here comes the function you want to call // for example: an echo or a query to a mysql database } } function function2() { if(!empty($_POST['send'])) { // here comes the function you want to call } } ?> <form action="function1();" method="post"> <input type="submit" name="send" value="click to call function1" /> </form> <br /> <form action="function2();" method="post"> <input type="submit" name="send" value="click to call function2" /> </form>


Hmm, yeah, forgot to ask the question again. Same as the thread starter: "how to call a php function from a link".
And I started giving it a try by posting examples of how I tried myself, but they didn't work out.
If anybody has other methods for this? Because maybe there are new ideas, 3 years later![]()
No, this doesn't work. In 'action' you have to put the name of the page to which the form values must be sent, for example 'functions.php'.Code:<form action="function1();" method="post"> <input type="submit" name="send" value="click to call function1" /> </form>
Inside that page (which can also be the page with the form itself), you'll have to accept the form values ($_POST).
Guido - Community Team Advisor
Do you know where the (database) error is? Add it to the list!
Thinking Web: Voices of the Community
Blog - Free Flash Slideshow Widget


functions.php:
page with links:PHP Code:<?php
function function1() {
{
// here comes the function you want to call
}
function function2() {
{
// here comes the function you want to call
}
function function3() {
{
// here comes the function you want to call
}
if (isset($_GET['target']))
{
switch ($_GET['target']) {
case 'function1':
function1();
break;
case 'function2':
function2();
break;
case 'function3':
function3();
break;
}
}
HTML Code:<a href="functions.php?target=function1">Click me for function 1</a> <a href="functions.php?target=function2">Click me for function 2</a> <a href="functions.php?target=function3">Click me for function 3</a>
Guido - Community Team Advisor
Do you know where the (database) error is? Add it to the list!
Thinking Web: Voices of the Community
Blog - Free Flash Slideshow Widget


To avoid I have to add cases when I have new functions, I do something like this:
I get the wrong messages... When the page is just loaded, I get the text:Code:<?php $fun = $_GET['fun']; if (empty($fun)) { function1(); } else { nothing(); } function nothing() { echo "this comes when nothing is clicked"; } function function1() { echo "this shows function one"; } function function2() { echo "this shows function two"; } ?> <a href="?fun=function1">call function 1</a> <a href="?fun=function2">call function 2</a>
this shows function one call function 1 call function 2
And if I click one of these links, the left message changes to "this comes when nothing is clicked". AARGH
Just switch function1() and nothing() in your if statement![]()
Guido - Community Team Advisor
Do you know where the (database) error is? Add it to the list!
Thinking Web: Voices of the Community
Blog - Free Flash Slideshow Widget


This works exactly how I want it to be.
With the call that is made by the red line:
I'm wondering why I couldn't find something like this elsewhere on the net.Code:<?php $fun = $_GET['fun']; if (empty($fun)) { nothing(); } else { $fun(); } function nothing() { echo ""; } function function1() { echo "this shows function one"; } function function2() { echo "this shows function two"; } ?> <a href="?fun=function1">call function 1</a> <a href="?fun=function2">call function 2</a>
I can't see why other people would recommend it with Javascript, or Ajax and stuff like that.
Guido - Community Team Advisor
Do you know where the (database) error is? Add it to the list!
Thinking Web: Voices of the Community
Blog - Free Flash Slideshow Widget

If you are referring to my comments, then as is clear, I was mistakenly responding to the Original Posters (OP) question:Originally Posted by bulevardi
And I thought I that the trouble I had taken to clearly explained that the advice I offered was properly flagged as being conditional upon the user perhaps wishing to use Ajax.Originally Posted by OP
"I don't know exactly what you are after, but if this to refresh a part of page in the Ajax manner ... "
That is because in certain circumstances you can use Javascript to call a PHP function.
Bookmarks