在php中按下按钮触发一段代码

Basically what I'm trying to do is to execute a piece of code or a function once a button in clicked. I don't have any html form that sends the request and I don't want the page to reload.

Example: If I click the button named Hello I want to execute the next piece of code in php

echo 'Welcome '.$user.' !';

Later edit: I didn't said that i don't want to use AJAX, and YES, I know that PHP is serverside.

You'll have to use AJAX or reload the page/frame with the new info from user.
Take this example and try to modify to your needs:

<!DOCTYPE html>
<html>
<head>
<script>
function loadXMLDoc()
{
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)
    {
    document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","ajax_info.txt",true);
xmlhttp.send();
}
</script>
</head>
<body>

<div id="myDiv"><h2>Let AJAX change this text</h2></div>
<button type="button" onclick="loadXMLDoc()">Change Content</button>

</body>
</html>

There's a difference between both languages. PHP is run on the server, which means that when you retrieve a page, you send a request, the server will handle that request and give you the page back.

At that moment, the PHP part of this story has ended and the client-side part starts. When you click a button, and you want something to happen, you will probably use JavaScript. This language runs in your browser.

To make the two communicate with each other, you have to make a request somehow to the server from your JavaScript code, either by:

  • submitting a form,
  • reloading a page,
  • an AJAX request.

Since the first two options are not wanted by you, so I'll explain you how you can use the third one, the AJAX request.

The AJAX request is actually an asynchronous request to the server, you don't need to reload your page, it runs all behind the screens. To do this, you will need to connect your event handler to that AJAX request, for example by doing the same as in the next code example. This is written using the jQuery library because it simplifies event handling and sending AJAX requests. If you don't believe me or you don't want to use it, you can always look at Filipe's answer, which gives you a proper example of how to handle AJAX requests without jQuery.

$("#myButton").on("click", function() {
    $.get("theOtherPage.php", function(data) {
        // Do something with "data"
    });
});

In this case, an onClick event hanlder is added to a button #myButton and when it's clicked, a page called theOtherPage.php is requested. Here you put the content you want, for example:

echo 'Welcome '.$user.' !';

When the request is completed, the data will be available in the data argument of the callback. You can do something with it now (show it to the user for example).