SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 38
  1. #1
    SitePoint Enthusiast that_tim_fella's Avatar
    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
    Or however?

    Thanks

  2. #2
    Sidewalking anode's Avatar
    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

  3. #3
    SitePoint Member Charles's Avatar
    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?

  4. #4
    SitePoint Addict mr tinkles's Avatar
    Join Date
    Jan 2003
    Posts
    262
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    "<a href="dowhatever.php">Click me</a>"

    what, dowhatever.php, does, is up to you.

  5. #5
    SitePoint Member Charles's Avatar
    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?

  6. #6
    SitePoint Member
    Join Date
    Aug 2005
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    on javascript:

    <a href='javascript:function_name()'>Click Me</a>

  7. #7
    Who turned the lights out !! Mandes's Avatar
    Join Date
    May 2005
    Location
    S.W. France
    Posts
    2,496
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Charles
    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?
    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.
    A Little Knowledge Is A Very Dangerous Thing.......
    That Makes Me A Lethal Weapon !!!!!!!!

    Contract PHP Programming

  8. #8
    SitePoint Addict
    Join Date
    Dec 2008
    Location
    Brussels
    Posts
    377
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by mr tinkles View Post
    "<a href="dowhatever.php">Click me</a>"

    what, dowhatever.php, does, is up to you.

    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";
    ?>

  9. #9
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,509
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    This thread is almost 6 years old
    How did you find it?

  10. #10
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    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";

    Requires a bit of setting up though, and a bit of a change of mind-set and reasonable JS and PHP skills.

    Otherwise bulevardi's comments are sound.

  11. #11
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by guido2004 View Post
    This thread is almost 6 years old
    How did you find it?
    Doh... I was wondering why Mandes was back on the boards.

  12. #12
    SitePoint Addict
    Join Date
    Dec 2008
    Location
    Brussels
    Posts
    377
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by guido2004 View Post
    This thread is almost 6 years old
    How did you find it?
    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.

  13. #13
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    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.
    But what is your question then?

  14. #14
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,509
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by bulevardi View Post
    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.
    I didn't even notice you were asking something, I thought you were giving an answer
    Quote Originally Posted by bulevardi View Post
    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>"
    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>"
    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.
    or something like this:

    <?php
    echo "click <a href=\"" . function1() . "\">me</a> for the function";
    ?>
    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.

  15. #15
    SitePoint Addict
    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>

  16. #16
    SitePoint Addict
    Join Date
    Dec 2008
    Location
    Brussels
    Posts
    377
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by guido2004 View Post
    I didn't even notice you were asking something, I thought you were giving an answer
    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

  17. #17
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,509
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Code:
    <form action="function1();" method="post">
    <input type="submit" name="send" value="click to call function1" />
    </form>
    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'.

    Inside that page (which can also be the page with the form itself), you'll have to accept the form values ($_POST).

  18. #18
    SitePoint Addict
    Join Date
    Dec 2008
    Location
    Brussels
    Posts
    377
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by guido2004 View Post
    1// No, not necessarily. As you say immediately after, you can put all functions in one script:

    2// 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.
    1// But how do you call them ?
    2// And how do you target them?

    Because my examples don't actually work with me...

  19. #19
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,509
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    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;
        }
      }
    page with links:
    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>

  20. #20
    SitePoint Addict
    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>
    I get the wrong messages... When the page is just loaded, I get the text:
    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

  21. #21
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,509
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Just switch function1() and nothing() in your if statement

  22. #22
    SitePoint Addict
    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'm wondering why I couldn't find something like this elsewhere on the net.
    I can't see why other people would recommend it with Javascript, or Ajax and stuff like that.

  23. #23
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,509
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by bulevardi View Post
    I'm wondering why I couldn't find something like this elsewhere on the net.
    Sitepoint rocks!
    I can't see why other people would recommend it with Javascript, or Ajax and stuff like that.
    It all depends on what you want to do.

  24. #24
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I would be extremely careful with that, that code will enable anyone to run any function they like on your server. Validate the function against a whitelist of allowed functions before calling it.

  25. #25
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by bulevardi
    I can't see why other people would recommend it with Javascript, or Ajax and stuff like that.
    If you are referring to my comments, then as is clear, I was mistakenly responding to the Original Posters (OP) question:

    Quote Originally Posted by OP
    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:
    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.

    "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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •