Using hyperlink to call php function
Page 1 of 2 12 LastLast
Results 1 to 15 of 19

Thread: Using hyperlink to call php function

  1. #1
    Looking for new tools JLHeidecker's Avatar
    Join Date
    May 2006
    Posts
    47

    Using hyperlink to call php function

    Example code:

    <?php
    function myFunction()
    {
    echo 'The function ran successfully.';
    }

    echo '<a href="xxxxx">Link to myFunction</a>';

    ?>

    What do I put in place of xxxxx? I want the act of clicking the hyperlink to run myFunction().

    Thanks in advance!

  2. #2
    Member
    Join Date
    Aug 2004
    Posts
    3,021
    I suppose you could do something like this:
    PHP Code:
    if ($_GET['run_func'] == 'yes') {
        
    myFunction();
    } else {
        echo 
    '<a href="xxxxx?run_func=yes">Link to myFunction</a>';
    }

    function 
    myFunction() 
    {
        echo 
    'The function ran successfully.';


  3. #3
    phpMaster
    Join Date
    Jun 2005
    Location
    Europe
    Posts
    2,983
    PHP Code:
    <?php

    ///// functions /////

    function myFirst(){
        echo 
    'The First ran successfully.';
    }

    function 
    mySecond(){
        echo 
    'The Second ran successfully.';
    }

    ///// START /////

    ?>
    <html><body>

    <?php

    if (isset($_GET['run'])) $linkchoice=$_GET['run'];
    else 
    $linkchoice='';

    switch(
    $linkchoice){

    case 
    'first' :
        
    myFirst();
        break;

    case 
    'second' :
        
    mySecond();
        break;

    default :
        echo 
    'no run';

    }

    ?>
    <hr>
    <a href="?run=first">Link to First</a>
    <br>
    <a href="?run=second">Link to Second</a>
    <br>
    <a href="?run=0">Refresh No run</a>

    </body></html>
    A devlopement based on Installer example in post above.
    Added so now there is 3 options in this page.
    I dont think there is much to explain.
    switch test if is 'first' or 'second'
    default is executed if is any other value

    I tested code at my php server and it works.
    Apache/2.4.7 (Win64) PHP/5.5.9 MySQL/5.6.16 phpMyAdmin/4.1.7

  4. #4
    Lost in Time (1970's) Houdini's Avatar
    Join Date
    Jul 2004
    Location
    Nashville TN USA
    Posts
    1,930
    One question is why would you want to link to a function? Normally you would 'call' the funtion and provide a paramater(s) if any. What is the point? Why not have all your function in a file called functions and as an example this would have just one.
    PHP Code:
    <?php
    include("functions.php");//comment this out to test
    //here is the actual function that is unseen from the include
    function myFunction()//
    {
    echo 
    'The function ran successfully.';
    }
    //and below is the script of the page
    echo "<h2>I am going to call my function to see what happens with the include!</h2>";
    echo 
    "<span style='color:green'><h1>";
    myFunction();
    echo 
    "</h1></span>The above function call should be green and means it worked.";
    echo 
    "<br />Wasn't that neato?";
    ?>

  5. #5
    Looking for new tools JLHeidecker's Avatar
    Join Date
    May 2006
    Posts
    47
    Quote Originally Posted by Houdini
    One question is why would you want to link to a function?
    The "point" is that I want functions to run when users click hyperlinks. Instead of loading another separate php script, or passing URL parameters, I'd rather just call the function directly. This way, for small applications, I can do everything from within one php script, without having to pass a URL parameter like "index.php?fnc=do_something."

  6. #6
    phpMaster
    Join Date
    Jun 2005
    Location
    Europe
    Posts
    2,983
    Quote Originally Posted by JLHeidecker
    The "point" is that I want functions to run when users click hyperlinks.
    as you already have seen by running my script
    this is exactly what my script does:
    - run different functions when a user click a hyperlinks

    if you have any questions about this, just ask
    but think is very straight forwardcand easy code to understand
    PHP Code:
    <?php

    ///// functions /////

    function myFirst(){
        echo 
    'The First ran successfully.';
    }

    function 
    mySecond(){
        echo 
    'The Second ran successfully.';
    }

    ///// START /////

    ?>
    <html><body>

    <?php

    if (isset($_GET['run'])) $linkchoice=$_GET['run'];
    else 
    $linkchoice='';

    switch(
    $linkchoice){

    case 
    'first' :
        
    myFirst();
        break;

    case 
    'second' :
        
    mySecond();
        break;

    default :
        echo 
    'no run';

    }

    ?>
    <hr>
    <a href="?run=first">Link to First</a>
    <br>
    <a href="?run=second">Link to Second</a>
    <br>
    <a href="?run=0">Refresh No run</a>

    </body></html>
    Apache/2.4.7 (Win64) PHP/5.5.9 MySQL/5.6.16 phpMyAdmin/4.1.7

  7. #7
    Looking for new tools JLHeidecker's Avatar
    Join Date
    May 2006
    Posts
    47

    Smile

    Quote Originally Posted by halojoy
    as you already have seen by running my script
    this is exactly what my script does:
    - run different functions when a user click a hyperlinks

    if you have any questions about this, just ask
    but think is very straight forwardcand easy code to understand

    Hi, thanks Halojoy. My previous post was responding to Houdini, who was asking why I would want to call a function from a hyperlink.

    Yes, of course your code works, there are many versions of that solution. It's what just about any developer would typically do.

    I was originally hoping there was a way to call a function directly from the hyperlink within the same php script, thus eliminating the need to reload the script, and have a test for the URL parameter.

    But after thinking, I realize that is not what I really want. I think I'm going to go back to passing URL parameters!

  8. #8
    Junior Member
    Join Date
    May 2007
    Posts
    11

    Arrow Link to function in PHP

    For those who may actually need to do this, it is really quite simple:

    Code:
    echo('<a href="' . YourFunction() . '">Run your function</a>');

  9. #9
    Pna lbh ernq guvf¿
    Join Date
    Jul 2004
    Location
    Kansas City area
    Posts
    19,353
    Quote Originally Posted by RationalRabbit View Post
    For those who may actually need to do this, it is really quite simple:

    Code:
    echo('<a href="' . YourFunction() . '">Run your function</a>');
    Either you've misunderstood the topic, or you really misunderstand how PHP works. In your code, YourFunction() will be executed and it's output will be placed in the 'href' attribute of the HTML code.

    It will not simply wait for the user to click the hyperlink and then execute the PHP function 'YourFunction'.

  10. #10
    Junior Member
    Join Date
    May 2007
    Posts
    11

    Red face Duh

    You are, of course, right. Not much thinking going on on my part this morning. (he said embarrassingly)

  11. #11
    Junior Member
    Join Date
    Nov 2011
    Posts
    4
    Hey,
    this thread has really Helped me out!

    1 Question:

    I'm using halojoy's code (works great!)
    Would it be possible to pass a variable along with the call?
    Without getting into the details, this would give me some more functionality.

    Example:
    Instead of:

    <a href="?run=second">Link to Second</a>

    Do something like:

    <a href="?run=second($example_variable)">Link to Second</a>

    ....I'm relatively new to PHP, so learning it as I go...
    Thanks

  12. #12
    Senior Member Derokorian's Avatar
    Join Date
    Apr 2011
    Location
    Denver
    Posts
    1,740
    PHP Code:
    <a href="?run=second&arg=stuff_here">Link to Second</a>

    ...

    case 
    'second' :
        
    mySecond($_GET['args']);
        break; 

  13. #13
    Junior Member
    Join Date
    Nov 2011
    Posts
    4
    Thanks for the quick response Derokorian!
    ...now, can you see why this isn't working?


    PHP Code:
    <?php

    function myFirst(){
        echo 
    'The First ran successfully.';
    }
    function 
    mySecond(){
        echo 
    'The Second ran successfully.';
    }
    function 
    myThird($samplePassVariable){
        echo 
    $samplePassVariable;
    }
    ?>


    <?php
    if (isset($_GET['run'])) $linkchoice=$_GET['run'];
    else 
    $linkchoice='';

    switch(
    $linkchoice){
        case 
    'first' :
            
    myFirst();
            break;
        
        case 
    'second' :
            
    mySecond();
            break;

        case 
    'third' :
            
    myThird($_GET['args']);
            break;

        default :
            echo 
    'no run';
    }
    ?>

    <hr>
    <a href="?run=first">Link to First</a>
    <br>
    <a href="?run=second">Link to Second</a>
    <br>
    <a href="?run=third&arg="Sample string to send">Link to Third</a>
    <br>
    <a href="?run=0">Refresh No run</a>

  14. #14
    Pedantic Curmudgeon Weedpacket's Avatar
    Join Date
    Aug 2002
    Location
    General Systems Vehicle "Thrilled To Be Here"
    Posts
    21,773
    Because "arg" is not the same thing as "args".
    THERE IS AS YET INSUFFICIENT DATA FOR A MEANINGFUL ANSWER
    FAQs! FAQs! FAQs! Most forums have them!
    Search - Debugging 101 - Collected Solutions - General Guidelines - Getting help at all

  15. #15
    Senior Member Derokorian's Avatar
    Join Date
    Apr 2011
    Location
    Denver
    Posts
    1,740
    Also look at the href attribute... href="?run=third&arg="Sample string to send" which says href="?run=third&arg=" because it will stop at the quote! A solution would be to A) not use quotes in the query string and/or (most likely and) B) use url_encode() on the string you want to put there. Example:
    PHP Code:
    <hr> 
    <a href="?run=first">Link to First</a> 
    <br> 
    <a href="?run=second">Link to Second</a> 
    <br> 
    <a href="?run=third&arg=<?php echo url_encode("Sample string to send"); ?>">Link to Third</a> 
    <br> 
    <a href="?run=0">Refresh No run</a>

Thread Information

Users Browsing this Thread

There are currently 3 users browsing this thread. (0 members and 3 guests)

Posting Permissions

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