【问题标题】:Button give $_GET extra parameter按钮提供 $_GET 额外参数
【发布时间】:2014-05-06 18:21:52
【问题描述】:

我的表单中有两个按钮,一个用于回答问题,另一个用于复制问题。

<div id="question">
    <?php echo($question->content) ?>
</div>
<form action="script.php" method="GET" id="question">
    <input type="text" name="question">
    <button id="answer" onclick="document.getElementById('question').submit()">Answer the question</button>
    <button id="copy"   onclick="document.getElementById('question').submit()">Copy the question</button>
</form>

script.php 的 URL 现在看起来像:
script.php?question=sometext

现在我希望当您单击复制按钮时,URL 看起来像这样:
script.php?question=sometext&amp;copy

对于回答按钮:
script.php?question=sometext&amp;answer

编辑:

有很多答案说:“使用&lt;input type&gt;而不是&lt;button&gt;
问题是我不能将输入字段用作按钮,因为该按钮在我的表单之外。而且我不能把它放在我的表单中

【问题讨论】:

  • 为什么不使用&lt;input type="submit" name="copy" value="Copy.."&gt;&lt;input type="submit" name="answer" value="Answer..."&gt;
  • @AbcAeffchen,那么您在 URL 中什么也看不到
  • 刚刚测试了一下,我得到了script.php?question=sometext&amp;answer=Answer+the+question
  • 您可以使用两个不同名称的&lt;input type='submit'&gt;。它会解决你的问题。

标签: php html button get


【解决方案1】:

您可以做的是使用一个隐藏字段并根据按下的按钮更改其名称。类似于以下内容:

<div id="question">
<?php echo($question->content) ?>
</div>
    <form action="script.php" method="GET" id="question">
        <input type="text" name="question">
        <input id="action" type="hidden" name="" value="">
        <button id="answer" onclick="document.getElementById('action').setAttribute('name','answer'); document.getElementById('question').submit()">Answer the question</button>
        <button id="copy"   onclick="document.getElementById('action').setAttribute('name','copy'); document.getElementById('question').submit()">Copy the question</button>
    </form>

虽然这会在 url 上为您提供所需的结果,但将隐藏的字段名称作为“action”并通过 javascript 将其值更改为“copy”或“answer”会更合适。

【讨论】:

    【解决方案2】:

    尝试制作两个表单,其中包含一个带有值的隐藏输入字段。然后你在提交时在你的 url 中获得额外的参数

    【讨论】:

      【解决方案3】:

      将您的表单更改为以下内容

      <form action="script.php" method="GET" id="question">
          <input id="question" type="text" name="question">
          <input id="answer" type="submit" name="answer" value="true">
          <input id="copy" type="submit" name="copy" value="true">  
      </form>
      

      网址:

      script.php?question=hello&copy=true
      

      那你就可以查了

      if(isset($_GET['answer']) && $_GET['answer']=="true"){
      //answer action
      }
      
      if(isset($_GET['copy']) && $_GET['copy']=="true"){
      //copy action
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-30
        • 1970-01-01
        • 1970-01-01
        • 2011-08-08
        • 2017-01-25
        相关资源
        最近更新 更多