-2

Click me

  <?php 
        function myfun(){
             echo "Hello World";
        }
        ?>

I really don't know how does it work i just want to learn php. Please help me by solving my problem. when i'm clicking the button it's showing nothing.

2
  • Try using Ajax To do this Commented Mar 29, 2017 at 3:26
  • My answer here might help you stackoverflow.com/questions/43035050/… Commented Mar 29, 2017 at 3:28

2 Answers 2

0

You can't invoke a server side function (PHP) in a client side language (JavaScript).

However, you can emulate this by using AJAX and requesting an external webpage:

index.html

$("button").click(function () {
  $(this).attr("disabled",true);
  $.get("http://neil.computer/stack/test.php",function (data) {
    $(document.body).html(data);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<button>Load HTML!</button>

test.php

<?php

function myfun(){
     echo "Hello World";
}

myfun();

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

Comments

0

PHP is a server-side application so there, so you cannot directly call it onClick() because that event happens on the client side.

However, you can call it via JavaScript! More specifically aJax.

Here's a good place to start

Here's a quick example from What is Sleep`s answer

AJax:

function callPHP() {
    $.ajax ({
        url: "yourPageName.php",
        data: { action : assign }, //optional
        success: function( result ) {
            //do something after you receive the result
        }
    }

PHP:

if ($_POST["action"] == "assign")
{
    assign(your parameters); //You need to put the parameters you want to pass in
                             //the data field of the ajax call, and use $_POST[]
                             //to get them 
}

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.