douxi8759 2014-07-30 02:49
浏览 41
已采纳

PHP - 在帖子提交时调用函数?

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

  • 写回答

1条回答 默认 最新

  • dongyou26216708 2014-07-30 03:10
    关注

    Noticed 2 things:

    1. $_POST is always there. So isset($_POST) is always true. You should check the existence of parameter within it (e.g. $_POST['signme']) or check if its not empty (e.g. !empty($_POST)).

    2. The getLink function doesn't really print the result out itself. It just returning the HTML string that you just ignored. You should print the returns of 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
    Use 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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 求数学坐标画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站