dr637349 2017-01-01 16:48
浏览 48
已采纳

PHP验证按钮

I would like to have on my HTML page a simple button allowing me if right clicked one, click activate every time my PHP script. I tried with this code, without any positive result for the moment:

//My html code
<!DOCTYPE html>
<head>
    <title><?php echo $pageTitle; ?></title>
    <meta charset="UTF-16" />
    <link rel="stylesheet" type="text/css" media="all" href="css.css">
<style></style>
</head>
<body>
    <main>
        <button onclick="doTheFunction();">Run the script</button>
            <script>
            function doTheFunction(){
            xmlhttp = new XMLHttpRequest();
            xmlhttp.open("POST","script.php",true);
            xmlhttp.send();
            xmlhttp.onreadystatechange = function() {
                if (this.readyState == 4 && this.status == 200) {
                  document.getElementById("demo").innerHTML = this.responseText;
                }
              };
            }
            }
            </script>

    </main>
    <footer>
        <?php echo $footer; ?>
    </footer>
</body>

A part of my php code :

echo "$result";
  • 写回答

1条回答 默认 最新

  • dsozqcx9668 2017-01-01 16:54
    关注

    Your code is doing nothing with the result of Ajax. I suggest starting here as a reference. The most important thing you're missing is:

    xmlhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
          document.getElementById("demo").innerHTML = this.responseText;
        }
      };
    

    Where "demo" is the Id of the element you want to load the Ajax result into. Otherwise nothing will happen with that result.

    EDIT

    Let's clean up your code so scripts and html are separate:

    function doTheFunction(){
         xmlhttp = new XMLHttpRequest();
         //Set this before sending!!
         xmlhttp.onreadystatechange = function() {
             if (this.readyState == 4 && this.status == 200) {
                 document.getElementById("demo").innerHTML = this.responseText;
             }
         };
         }
        //No reason to post
        xmlhttp.open("GET","theme/generator/gensent.php",true);
        xmlhttp.send();
    }
    <head>
        <title><?php echo $pageTitle; ?></title>
        <meta charset="UTF-16" />
        <link rel="stylesheet" type="text/css" media="all" href="css.css">
    <style></style>
    </head>
    <body>
        <main id="demo">
            <button onclick="doTheFunction();">Run the script</button>
        </main>
        <footer>
            <?php echo $footer; ?>
        </footer>
    </body>

    I would put the <script> tag at the end of your html, after closing the body, or include it in the header from another file. Note- I use GET since no sensitive info is transferred, the script should load after the HTML- hence put at the end, and finally you need a tag with the Id for this to actually work.

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 MATLAB四叉树处理长方形tif文件
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址
  • ¥15 elmos524.33 eeprom的读写问题
  • ¥15 使用Java milo连接Kepserver服务端报错?
  • ¥15 用ADS设计一款的射频功率放大器
  • ¥15 怎么求交点连线的理论解?
  • ¥20 软件开发方法学习来了
  • ¥15 微信小程序商城如何实现多商户收款 平台分润抽成
  • ¥15 HC32L176调试了一个通过TIMER5+DMA驱动WS2812B