-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.

Manish Kumar
  • 79
  • 1
  • 12
  • 1
    Possible duplicate of [What is the difference between client-side and server-side programming?](http://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) – user3942918 Mar 29 '17 at 03:26
  • Try using Ajax To do this – Rotimi Mar 29 '17 at 03:26
  • My answer here might help you http://stackoverflow.com/questions/43035050/php-code-runs-automatically-in-javascript-when-not-needed/43035202?noredirect=1#comment73157099_43035202 – AZee Mar 29 '17 at 03:28

2 Answers2

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();

?>
Community
  • 1
  • 1
Neil
  • 14,063
  • 3
  • 30
  • 51
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 
}
Community
  • 1
  • 1
Aaron N. Brock
  • 4,276
  • 2
  • 25
  • 43