0

I have a line in a php code that reads something like this:

echo '<div class="sample-button"><a href="#">Do something</a></div>'

This shows a clickable text link on the page. Now I want that clicking on this link should call a php function myFunc() which I have defined in the same php file. How do I implement this?

3
  • 1
    PHP runs before the page is rendered in your browser so the only way you can interact directly with your php function would be to invoke an ajax request that triggers the php function. Commented Nov 16, 2015 at 13:43
  • 1
    PHP is serverside, the only way to do what you're saying is to send that link to a page which has the function or if you use Javscript/Ajax or a different client-side language Commented Nov 16, 2015 at 13:44
  • w3schools.com/js/default.asp Commented Nov 16, 2015 at 13:45

4 Answers 4

1

Whatever the <a href="#"> gos to is your answer. The path needs to be <a href="#?call=1">

Now that's set, you need to create an if statment..
if ($_GET['call'] === 1){ myFunc(); }

When you click the link, it should refresh the page with the url now set to: localhost/page.php?call=1. As the php page is refreshed it can call MyFunc().

Sign up to request clarification or add additional context in comments.

Comments

0

You can call a JS function on click, not a php one. I think you have to better check the documentation on what is the purpose of the php language.

Comments

0

If you really want to execute a php script by clicking on the link you can use jquery ajax.

In your case call the same php file where the function is located by listening to the button's click event and perform the ajax request:

$('.sample-button').click(function() {

  // Ajax Call
  $.ajax({
    url: "/path_to_your_script.php",
    contentType: "application/x-www-form-urlencoded",
    type: "POST",
    data: {callFunction:true},
    success: function(response){
        // Check your response
    },
    error: function(){
       // Error handling
    }
   });
});

On top of your php script you need to check:

<?php
if (!empty($_POST['callFunction'])) {

   your_function() {


       return $yourResponse;
   }
    exit();
}

Comments

0

A quick example ( untested ) to show how you might invoke your php function and use the response after clicking a standard link on a page.

<?php

    if( $_SERVER['REQUEST_METHOD']=='POST' && isset( $_POST['action'] ) && $_POST['action']=='call_my_func' ){

        /* ensure we discard the buffer if we need to send a response */
        ob_clean();

        function myFunc($var){
            echo 'PHP function...';
        }
        /* call your function */
        call_user_func( 'myFunc', 'some variable' );

        exit();
    }
?>



<script type='text/javascript'>
        function invoke_myfunc(){
            var http=new XMLHttpRequest();
            http.onreadystatechange=function(){
                if( http.readyState==4 && http.status==200 ) alert( http.response );
            };
            var headers={
                'Content-type': 'application/x-www-form-urlencoded'
            };
            http.open('POST', document.location.href, true );
            for( header in headers ) http.setRequestHeader( header, headers[ header ] );
            http.send( [ 'action=call_my_func' ].join('&') );
        }   
</script>

<?php
    /*
       in your page
       -------------
    */
    echo '<div class="sample-button"><a href="#" onclick="invoke_myfunc()">Do something</a></div>';
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.