I'm not going to go into detail about the difference between server and client side programming, since it's outside the scope of this question.
The short answer is that you can't. PHP is executed on an interpreter. The client that you are serving the web page to may have a PHP interpreter.
The answer you're looking for is JavaScript, which is also executed on an interpreter, but unlike PHP it is built into all modern browsers.
The high level solution is to split your PHP and HTML into separate files. You serve the HTML, which contains Javascript, which then calls your PHP script.
In your HTML, here's how you include JQuery in your web page:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script></head>
Now you have access to all sorts of useful Javascript goodies.
Next step is to serve your PHP script at a publicly accessible URL. e.g.
http://example.com/myphpscript.php
Inside it would contain your code:
<?php
class myClass{
function myClass(){
echo "myClass called";
}
}
$myClassInstance = new myClass();
?>
Assuming you are running a version lower than PHP 5.3.3, myClass()
is treated as a constructor. That is, it is called automatically when MyClass
is instantiated, i.e. new MyClass
.
If you are running PHP 5.3.3 or higher, just change the name of the function myClass()
to __construct()
.
When you are done this step, it's time to write some Javascript. What you need to do is use Javascript to register an event listener that listens for when the button is clicked, and executes some Javascript code when that happens.
I don't want to duplicate an answer, so you can find it here: ajax post within jquery onclick