PHP -调用一个带有post提交的函数


PHP - Calling a function with on post submit?

目前有:

<script>
  function copyToClipboard(info, text) {
    window.prompt(info, text);
  }
</script>
<?php
    function getLink($user) {
      return '<a class="clicky" onclick="copyToClipboard(
                ''Copy to clipboard: Ctrl+C, Enter'nUse this on any forum with [img] tags!'', 
                ''site/pcard.php?user='.$user.''');">
                <span class="label label-primary">Get Link</span>
              </a>';
    }
?>
<div class="well">
    <form method="post">
        <label>Get Signature Image</label>
        <input type="text" placeholder="Username..." name="signame" />
        <button type="submit" class="btn btn-primary">Look-up</button>
<?php
if (isset($_POST)) {
  getLink($_POST['signame']);
}
?>
    </form>

我将如何继续使这个调用与发布的信息脚本?还有,这里还有其他错误吗?

注意两点:

  1. $_POST始终存在。所以isset($_POST)总是true。您应该检查其中的参数是否存在(例如$_POST['signme'])或检查其是否为空(例如!empty($_POST))。

  2. getLink函数本身并不真正打印结果。它只是返回你刚刚忽略的HTML字符串。您应该打印getLink的返回值。

我想这就是你需要的:

    <script>
      function copyToClipboard(info, text) {
        window.prompt(info, text);
      }
    </script>
    <?php
        function getLink($user) {
          return '<a class="clicky" onclick="copyToClipboard(
                    ''Copy to clipboard: Ctrl+C, Enter'nUse this on any forum with [img] tags!'', 
                    ''site/pcard.php?user='.$user.''');">
                    <span class="label label-primary">Get Link</span>
                  </a>';
        }
    ?>
    <div class="well">
        <form method="post">
            <label>Get Signature Image</label>
            <input type="text" placeholder="Username..." name="signame" />
            <button type="submit" class="btn btn-primary">Look-up</button>
    <?php
    if (isset($_POST['signame'])) {
      print getLink($_POST['signame']);
    }
    ?>
        </form>
    </div>