javascript - PHP - 在提交后调用函数?

标签 javascript php

目前有这个:

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

关于javascript - PHP - 在提交后调用函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25028004/

相关文章:

javascript - 从下拉列表中选择选项时显示图像

javascript - 动态添加 jQuery 的 slider

javascript - JSON 对象被 Javascript 重新排序

php - SQL 错误 : 1191 - Can't find FULLTEXT index matching the column list

javascript - 如何在 ExtJS 中添加 Facebook 分享按钮

javascript - 为什么AJAX函数不立即从数据库获取数据?

javascript - 如何在 jasmine 中模拟按键进行单元测试

php - 复选框将 1 或 0 插入 SQL 数据库

java - 如何从Mysql中的所有表中删除一条唯一记录

php - 在 Codeigniter 中调用未定义的函数 ibase_connect()