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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来