简体   繁体   English

PHP-在提交后调用函数?

[英]PHP - Calling a function with on post submit?

Currently have this: 目前有这个:

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

How would I go on to make this call that script with the posted info? 我将如何继续使用已发布的信息对此脚本进行调用? Also, are there any other mistakes here? 另外,这里还有其他错误吗?

Noticed 2 things: 注意两件事:

  1. $_POST is always there. $_POST总是在那里。 So isset($_POST) is always true . 因此isset($_POST)始终为true You should check the existence of parameter within it (eg $_POST['signme'] ) or check if its not empty (eg !empty($_POST) ). 您应该检查其中是否存在参数(例如$_POST['signme'] )或检查其是否不为空(例如!empty($_POST) )。

  2. The getLink function doesn't really print the result out itself. getLink函数本身并不会真正打印出结果。 It just returning the HTML string that you just ignored. 它只是返回您刚刚忽略的HTML字符串。 You should print the returns of getLink . 您应该打印getLink的返回。

I think this is what you needed: 我认为这是您需要的:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM