【问题标题】:PHP another HTML code after click a Button [closed]单击按钮后的PHP另一个HTML代码[关闭]
【发布时间】:2013-08-28 13:58:02
【问题描述】:

我有以下代码:

  <form action="" method="post">
    <input type="submit" name="ok" value="ÓK">
  </form>
  <?php
    if (isset($_POST['ok'])) 
    { 
      printf "OK";
    }  
  ?>

我希望在按下按钮后会出现另一个 html 代码

我不知道该怎么做

【问题讨论】:

标签: php html button


【解决方案1】:

这样做:

<form action="" method="post">
    <input type="submit" name="ok" value="ÓK">
</form>
<?php
    if (isset($_POST['ok'])) 
    { 
?>

<div>
<p>Your HTML-code here</p>
</div>

<?php
    }  
?>

【讨论】:

    【解决方案2】:

    尝试使用 header() 方法进行重定向。如果您想回显简单的 html,请执行以下操作:

    echo("<div></div>");
    

    【讨论】:

      【解决方案3】:

      你现在有 printf "ok"; 放

      ?> <?php
      

      在它之间你可以放置你的新 HTML

      【讨论】:

        【解决方案4】:

        使用 printecho ,而不是 printf

        【讨论】:

          【解决方案5】:

          如果您希望在单击按钮时出现另一段 HTML 代码,那么您将需要一个 JavaScript 函数,当按钮上发生 onclick 事件时调用该函数。

          我不擅长 JS,所以我把它留给 OP 练习

          【讨论】:

            【解决方案6】:

            您可以使用Ajax 提交表单。在纯 javascript 中,您可以使用 XMLHttpRequest 对象。

            这里是简单的ajax.php 文件示例:

            <?php
            if (isset($_POST['ok'])) {
                // send some response here
                var_export($_POST);
                exit;
            }
            ?>
            <!DOCTYPE html>
            <html><head>
            <script type="text/javascript">
            function ajax() {
                var form = document.forms['form1'];
                var queryString = 'test=' + form['test'].value + '&ok=' + form['ok'].value;
                var xmlHttpReq = false;
                var self = this;
                if (window.XMLHttpRequest) { // Mozilla/Chrome/Opera/Safari/IE9+
                    self.xmlHttpReq = new XMLHttpRequest();
                } else if (window.ActiveXObject) { // IE6-8
                    self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
                }
                self.xmlHttpReq.open('POST', 'ajax.php', true);
                self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
                self.xmlHttpReq.onreadystatechange = function () {
                    if (self.xmlHttpReq.readyState == 4) {
                        document.getElementById("result").innerHTML = self.xmlHttpReq.responseText;
                    }
                }
                self.xmlHttpReq.send(queryString); // send http request
                return false; // disable submitting the form below and thus reloading the page
            }
            </script>
            </head>
            <body>
            <form id="form1" action="" method="post" onsubmit="return ajax();">
                <input type="text" name="test" value="test data" />
                <input type="submit" name="ok" value="OK" />
            </form>
            <div id="result" style="background:#faa;"></div>
            </body>
            </html>
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2018-01-02
              • 2018-04-22
              • 2014-06-01
              • 1970-01-01
              • 2013-07-13
              • 1970-01-01
              • 2022-12-18
              • 2015-07-04
              相关资源
              最近更新 更多