2014-02-15 20:22
浏览 208


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.

图片转代码服务由CSDN问答提供 功能建议

基本上我要做的是在点击一个按钮后执行一段代码或一个函数。 我没有任何发送请求的html表单,我不希望页面重新加载。

示例: 如果我单击名为Hello的按钮我想执行下一个 php中的一段代码

echo'Welcome'。$ user。' !';

稍后编辑: 我没有说我不想使用AJAX,是的,我知道PHP是服务器端。< / p>

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongzhi2332 2014-02-15 20:33

    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>
    function loadXMLDoc()
    var xmlhttp;
    if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
      {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
    <div id="myDiv"><h2>Let AJAX change this text</h2></div>
    <button type="button" onclick="loadXMLDoc()">Change Content</button>
    解决 无用
    打赏 举报
  • duanfuxing2212 2014-02-15 20:32

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

    解决 无用
    打赏 举报