Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder levani's Avatar
    Join Date
    Mar 2009
    Location
    Georgia
    Posts
    183
    Thanks
    29
    Thanked 1 Time in 1 Post

    Execute function only if I click link

    I have a standard php function which for example sends some data to database, but I don't want to execute this function every page load but only when I click a link on this page. How can I do that? How can I tell the function that the link is clicked?

    Can anyone please help?

  • #2
    Regular Coder funnymoney's Avatar
    Join Date
    Aug 2007
    Posts
    364
    Thanks
    17
    Thanked 24 Times in 24 Posts
    two most common methods of this is by using POST or GET PHP predefined variables.

    GET is usualy used to comunicate with PHP with links, while POST is used to communicate with forms. Although you can also use GET with forms but that is not recommended.

    Try this test.php to figure out how php works

    PHP Code:
    <?php
    if (isset($_GET['link'])) {
     
    getFunction($_GET['link']);
    }

    if(isset(
    $_POST['form'])) {
     
    postFunction($_POST['form']);
    }

    function 
    getFunction($variable) {
     print 
    $variable;
    }

    function 
    postFunction($variable) {
     print 
    $variable;
    }

    ?>

    <a href="?link=test_link">Test link for get</a>
    <br>
    <form action="" method="post">
    <input type="submit" name="form" value="Test link for post" />
    </form>


  •  

    Posting Permissions

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