-1

I have a php class with a function and I want to call that function with an onclick event of a button. I wanted to know if there are any ways to do that. here's the code:

<?php 
class myClass{
  function myClass(){
    echo "myClass called";
  }
}
?>
<html>
<title>sample project</title>
<body>
<form action="">
<input type="button" value="click" onclick="myClass()">
</form>
</body>
</html>

I'll appreciate if you can help me with this.
thanks

m0j1
  • 4,067
  • 8
  • 31
  • 54
  • 2
    [Ajax](https://developer.mozilla.org/en/docs/AJAX) is your friend ;) – DontVoteMeDown Jul 01 '14 at 21:22
  • 7
    You need to understand the difference between server-side & client-side code... PHP is executed on your server before sending the results to the browser (client), which cannot interpret PHP. – blex Jul 01 '14 at 21:22
  • 6
    This question comes up so frequently that there is now a canonical answer for it. [Reference: What is the difference between client-side and server-side programming?](http://stackoverflow.com/questions/13840429/reference-what-is-the-difference-between-client-side-and-server-side-programmin) – laurent Jul 01 '14 at 21:24
  • It seems my question didn't have a specific answer and I should have either used Ajax or call the function from php tag itself. I only can mark one answer as correct but if I could I would mark all of them as correct. – m0j1 Jul 02 '14 at 22:42

3 Answers3

2

you could do this... (but this isnt to advise)

<?php 
class myClass{
  function myClass(){
    echo "myClass called";
  }
}

if($_POST['go']){
    $foo = new myClass();
}
?>
<html>
    <title>sample project</title>
    <body>
    <form action="" methode="post">
        <input type="hidden" name="go" value="1" />
        <input type="submit" value="click" />
    </form>
    </body>
</html>

but in your case you reall should use ajax, mixing php with html and/or javascript is not really nice coding.

Dwza
  • 6,494
  • 6
  • 41
  • 73
  • very sad to devote something and dont even point why ^^ – Dwza Jul 01 '14 at 21:29
  • Not the downvoter.. But the first part of your answer is pretty nasty. – rlemon Jul 01 '14 at 21:32
  • Not downvoter, however you are losing possible data on the page. Also you are making more network calls which is not efficient. – Hurricane Development Jul 01 '14 at 21:32
  • because i say dirty programming ? what is so bad about it ? you may read my last sentence – Dwza Jul 01 '14 at 21:33
  • A messed up answer for a messed up question. – blex Jul 01 '14 at 21:33
  • indeed. i dont say the question is good and i never would do it like this and also i know the diffrence between serverside and clientside stuff... but the answere is suitable to his question – Dwza Jul 01 '14 at 21:34
  • +1 for making me laugh with your edits :) – blex Jul 01 '14 at 21:38
  • actually i dont really get it why i got donvoted. there is someone who posts crap and even if its crap what hes doing. there is a crapy answere to it. now i get downvoted because a helped him dealing with his codinstyle. even if i would do it better... but this doesnt seems to interesst anybody ^^ – Dwza Jul 01 '14 at 21:40
  • 2
    They downvoted you to discourage you to answer questions that are fundamentally wrong, instead of pointing to questions that **explain** why it's wrong, like [the one mentionned in the comments](http://stackoverflow.com/questions/13840429/reference-what-is-the-difference-between-client-side-and-server-side-programmin). The point is not to encourage the poster to keep doing bad practice, but to let him learn the right way. – blex Jul 01 '14 at 21:51
  • ah ok. thank you very much for this information... i didnt even think about that. *rollEyes* – Dwza Jul 01 '14 at 21:54
2

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

Rob
  • 172
  • 6
1

So this is an issue with web development as html/css/javascript are client side, and PHP is server side. Properly you can still achieve your goal with ajax, but I use a simple jQuery trick that can call PHP code. Here it is:

Add this to HTML

<div class="holder"></div>

Then CSS

.holder {
    display: none;
}

And then javascript

function myClass() {
    $('.holder').load('path/to/php/file');
}

Any HTML returned by the php file (like echo or print_r will be put into the holder div, but won't be displayed.

Hurricane Development
  • 2,449
  • 1
  • 19
  • 40