SitePoint Sponsor |
|
User Tag List
Results 1 to 25 of 38
Thread: Calling a function from a link
-
Feb 18, 2003, 08:22 #1
- Join Date
- Feb 2003
- Location
- England
- Posts
- 99
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Calling a function from a link
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:
PHP Code:<a href='#' onclick='function_name()'>Click Me</a>
Thanks
-
Feb 18, 2003, 08:25 #2
- Join Date
- Mar 2001
- Location
- Philadelphia, US
- Posts
- 2,205
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
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
-
Feb 18, 2003, 19:15 #3
- Join Date
- Feb 2003
- Location
- Some Where
- Posts
- 13
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
What would have to be in the HTML code to call a SQL query from a webpage by clicking on a button?
-
Feb 18, 2003, 19:53 #4
"<a href="dowhatever.php">Click me</a>"
what, dowhatever.php, does, is up to you.
-
Feb 18, 2003, 20:19 #5
- Join Date
- Feb 2003
- Location
- Some Where
- Posts
- 13
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
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?
-
Aug 27, 2005, 06:43 #6
- Join Date
- Aug 2005
- Posts
- 1
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
on javascript:
<a href='javascript:function_name()'>Click Me</a>
-
Aug 27, 2005, 08:10 #7
- Join Date
- May 2005
- Location
- S.W. France
- Posts
- 2,496
- Mentioned
- 1 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by Charles
A Little Knowledge Is A Very Dangerous Thing.......
That Makes Me A Lethal Weapon !!!!!!!!
Contract PHP Programming
-
Dec 12, 2008, 03:50 #8
- Join Date
- Dec 2008
- Location
- Brussels
- Posts
- 377
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
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";
?>
-
Dec 12, 2008, 04:05 #9
This thread is almost 6 years old
How did you find it?Guido - Community Team Leader
The Votes Are In: The Winners of the 2013 Community Awards are...
Blog - Free Flash Slideshow Widget
-
Dec 12, 2008, 04:12 #10
- Join Date
- Oct 2006
- Location
- France, deep rural.
- Posts
- 6,869
- Mentioned
- 17 Post(s)
- Tagged
- 1 Thread(s)
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:
PHP Code:function myfunction(){
//
return "yes";
}
and reasonable JS and PHP skills.
Otherwise bulevardi's comments are sound.
-
Dec 12, 2008, 04:12 #11
-
Dec 12, 2008, 04:16 #12
- Join Date
- Dec 2008
- Location
- Brussels
- Posts
- 377
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
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.
-
Dec 12, 2008, 04:18 #13
- Join Date
- Oct 2006
- Location
- France, deep rural.
- Posts
- 6,869
- Mentioned
- 17 Post(s)
- Tagged
- 1 Thread(s)
but they didn't get me an answer.
-
Dec 12, 2008, 04:27 #14
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:
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";
?>Guido - Community Team Leader
The Votes Are In: The Winners of the 2013 Community Awards are...
Blog - Free Flash Slideshow Widget
-
Dec 12, 2008, 04:32 #15
- Join Date
- Dec 2008
- Location
- Brussels
- Posts
- 377
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
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>
-
Dec 12, 2008, 04:42 #16
- Join Date
- Dec 2008
- Location
- Brussels
- Posts
- 377
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
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
-
Dec 12, 2008, 04:43 #17Code:
<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 Leader
The Votes Are In: The Winners of the 2013 Community Awards are...
Blog - Free Flash Slideshow Widget
-
Dec 12, 2008, 04:48 #18
- Join Date
- Dec 2008
- Location
- Brussels
- Posts
- 377
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
-
Dec 12, 2008, 04:53 #19
functions.php:
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 Leader
The Votes Are In: The Winners of the 2013 Community Awards are...
Blog - Free Flash Slideshow Widget
-
Dec 12, 2008, 05:44 #20
- Join Date
- Dec 2008
- Location
- Brussels
- Posts
- 377
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
To avoid I have to add cases when I have new functions, I do something like this:
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
-
Dec 12, 2008, 05:45 #21
Just switch function1() and nothing() in your if statement
Guido - Community Team Leader
The Votes Are In: The Winners of the 2013 Community Awards are...
Blog - Free Flash Slideshow Widget
-
Dec 12, 2008, 07:02 #22
- Join Date
- Dec 2008
- Location
- Brussels
- Posts
- 377
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
This works exactly how I want it to be.
With the call that is made by the red line:
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.
-
Dec 12, 2008, 07:16 #23Guido - Community Team Leader
The Votes Are In: The Winners of the 2013 Community Awards are...
Blog - Free Flash Slideshow Widget
-
Dec 12, 2008, 07:16 #24
-
Dec 12, 2008, 07:49 #25
- Join Date
- Oct 2006
- Location
- France, deep rural.
- Posts
- 6,869
- Mentioned
- 17 Post(s)
- Tagged
- 1 Thread(s)
Originally Posted by bulevardi
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