0

Is there a way to detect a click on a HTML button in PHP, or do I really need to put a form tag around the element and make it an input submit button.

<button name="btnBottom" type="button"></button>

OR

<form method="post" action="#">
<input type="submit" name="btnBottom" />
</form>

Thanks in advance.

user2381011
  • 351
  • 6
  • 21

5 Answers5

8

PHP is a server-side language. If you'd like to tell a server-side script about clicks on HTML elements, you have to either submit the form or use AJAX.

ciruvan
  • 5,143
  • 1
  • 26
  • 32
3

You can't detect any event in PHP, on a generated HTML page.
Use some Javascript with AJAX to passe those events to a PHP page which will treat data.

See jQuery and jQuery AJAX

netvision73
  • 4,831
  • 1
  • 24
  • 30
2

It can be that you are confusing terms. PHP is server side code, it generates html, it's the code that makes html code. And there you cannot detect user actions because when the page is online, then PHP has already done it's job.

If you want to register user action you will need client-side code like Javascript. To communicate with PHP you need to make a AJAX call. Read/Google for these terms and you will understand better.

Sergio
  • 28,539
  • 11
  • 85
  • 132
0

if x.php is the handling php file then do this:

<button onclick="doThis()">Click here</button>

here's javascript:

function doThis()
{
var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    //do whatever you want upon getting response from php file
    }
  }
xmlhttp.open("GET","x.php",true); //call PHP file on click
xmlhttp.send();
}
Anshu Dwibhashi
  • 4,617
  • 3
  • 28
  • 59
0

You could try something like SimpleHTMLDOM that will allow you to perform jQuery like actions but within your PHP code on the server rather than than client-side using javascript

user2689136
  • 140
  • 5