Trigger PHP function by clicking HTML link

后端 未结 5 1331
耶瑟儿~
耶瑟儿~ 2020-12-21 03:04

Is it possible to trigger a PHP function by just clicking a link? Or should I stick with the form submit method? If clicking the link will work, where should the link refer

相关标签:
5条回答
  • 2020-12-21 03:20

    You can pass it as a query parameter of the link. http://example.com/?command=listMe&username=tom However that way everybody will be able to run the function by loading that URL

    <a href="http://example.com/?command=listMe&">List me</a>
    

    and in the PHP

    <?php
    if( isset($_GET['list_me']) && isset($_SESSION['Username'] ) ){
       listMe( $_SESSION['Username'] );
    }
    ?>
    
    0 讨论(0)
  • 2020-12-21 03:26

    You can do this by means of loading the entire page over again by the use of form submission, or by loading specific page contents directly into the page without needing to go from page to page. The second method is called "AJAX" (Asynchoronous Javascript and XML). Here are two examples, one of each specified.

    Form submission approach

    form.php

    <?php
    function get_users(){
    }
    if(isset($_GET['get_users']))
    {
        get_users();
    }
    ?>
    ...
    <form method="get">
        <input type="hidden" name="get_users">
        <input type="submit">
    </form>
    

    AJAX approach

    ajax_file.php

    <?php
    function call_me(){
        // your php code
    }
    call_me();
    ?>
    

    form.html

    <html>
    <head>
    <script type="text/javascript">
    function loadXMLDoc()
    {
        var xmlhttp;
        if (window.XMLHttpRequest)
        {
            xmlhttp = new XMLHttpRequest();
        }
        else
        {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function()
        {
            if(xmlhttp.readyState == 4 && xmlhttp.status == 200)
            {
                // do something if the page loaded successfully
            }
        }
        xmlhttp.open("GET","ajax_file.php",true);
        xmlhttp.send();
    }
    </script>
    </head>
    <body>
    <a href="#" onclick="loadXMLDoc()">click to call function</a>
    </body>
    </html>
    
    0 讨论(0)
  • 2020-12-21 03:32

    HTML

    <a href="?list_me">list me</a>
    

    PHP

    <?php
    if (isset($_GET['list_me'])) listMe();
    
    0 讨论(0)
  • 2020-12-21 03:35

    To trigger a function on link click with php the only way I know would be to append a param in the url of the link and then listen for that

    <a href="?function">Add my username to the list</a>
    

    Then check for link

    if (isset($_GET['function'])){
          runFunction();
    }
    

    This is because php is a server side technology if you want to fire something without refreshing the page you would need to look at something like javascript

    0 讨论(0)
  • 2020-12-21 03:44

    I found this code in a plugin, they have user a foreach look to trigger the action:

    $actions = unset($meta[$key]);
    foreach ( $actions as $action => $value ) {
        echo '<li><a href="' . esc_url( $value['url'] ) . '" class="my-action-' . $action . '">' . '<i class="fa fa-times"></i></a></li>';
    }
    
    0 讨论(0)
提交回复
热议问题