【问题标题】:HTML form to GET two variables from a PHP page从 PHP 页面获取两个变量的 HTML 表单
【发布时间】:2013-12-18 21:52:06
【问题描述】:

我已经工作了几个小时试图让它正常工作。我有一个表格的页面是/index.php?action=pagename。我有一个需要从以下/index.php?action=pagename&thing=something 获取变量的表单。我的 HTML 表单是这样的:

    <form role="form" action="pagename" method="get">
           <input type="text" name="thing">
    </form>

此表单位于 /index.php?action=pagename,我想从该 URL 获取 &amp;thing。 有什么想法吗?

我遇到的问题是,提交表单时,URL 重定向到 index.php?thing,而不是停留在 index.php?action=pagename

【问题讨论】:

  • "此表单位于 /index.php?action=pagename,我想从该 URL 获取 &thing。" - thing/index.php?action=pagename 中不存在
  • 将传递给$thing=thisvalue 的值可用作表单中的输入值。
  • 当您将&amp;thing 添加到index.php?action=pagename 时,会根据&amp;thing 加载新内容。此处的目的是使用表单搜索&amp;thing,以便用户可以通过手动添加&amp;thing 来加载内容。
  • 我已经用我遇到的确切问题更新了这个问题。我不知道为什么我被如此严厉地否决了。

标签: php html forms get


【解决方案1】:

看起来您可能在使用 &lt;forms&gt; 时遇到了一些问题,而不仅仅是 PHP,所以这里是一个概述:

<!-- the action is where you want to send the form data -->
<!-- assuming THIS code is the index.php file then we want to send the data to ourselves -->
<!-- the method is GET so it will be directly accessible from the URL later -->

<form action="index.php" method="GET">

    <!-- add a hidden value for pagename -->
    <input type="hidden" name="action" value="pagename">

    <!-- the name called "thing" will be appended to the URL and it's value as well -->
    <input type="text" name="thing" value="<?php echo (isset($_GET['thing']) ? $_GET['thing'] : ''); ?>">
    <br>

    <!-- click this button to submit the form to itself -->
    <!-- once this has been submitted then you can retrieve the URL value with $_GET as you can see above -->
    <input type="submit" value="submit" />
</form>

【讨论】:

  • 我用了这个,结果是URL重定向回index.php?thing='result'
  • 抱歉,我一般不使用method="GET",但我认为更新后的代码现在应该可以使用了
  • 感谢您跳出框框思考,你真棒!
  • 不客气,不要让投反对票让你灰心,因为我绝对记得我也曾与&lt;form&gt;s 斗争过,所以希望你坚持下去,因为 Web 开发总体上有一个光明的未来.
【解决方案2】:

您可以简单地使用 PHP 变量 $_GET['thing'] 来获取值。

【讨论】:

    【解决方案3】:

    表单元素的action属性是提交时调用的目标脚本。如果为空,它将是显示表单的当前脚本。你也只能给出以?开头的url参数。

    <form role="form" action="?action=pagename" method="get">
           <input type="text" name="thing">
    </form>
    

    摘自php.net:http://www.php.net/manual/de/tutorial.forms.php

    PHP 最强大的功能之一是它处理 HTML 的方式 形式。重要的基本概念是任何 表单元素将自动可用于您的 PHP 脚本。 请阅读关于外部来源变量的手册部分 有关在 PHP 中使用表单的更多信息和示例。这是一个 示例 HTML 表单:

    示例 #1 一个简单的 HTML 表单

    <form action="action.php" method="post">
     <p>Your name: <input type="text" name="name" /></p>
     <p>Your age: <input type="text" name="age" /></p>
     <p><input type="submit" /></p>
    </form>
    

    这种形式没有什么特别之处。它是一个直接的 HTML 表单 没有任何类型的特殊标签。当用户填写此表格并 点击提交按钮,action.php 页面被调用。在这个文件中 你会这样写:

    示例 #2 从我们的表单中打印数据

    Hi <?php echo htmlspecialchars($_POST['name']); ?>.
    You are <?php echo (int)$_POST['age']; ?> years old.
    

    此脚本的示例输出可能是:

    Hi Joe. You are 22 years old.
    

    【讨论】:

      【解决方案4】:

      我制作的用于从 URL 获取变量的基本脚本:

      <?php
      if (isset($_GET["action"]) && isset($_GET["thing"])) {
          $action = $_GET["action"];
          $thing = $_GET["thing"];
          echo $action .PHP_EOL . $thing;
      }
      ?>
      

      这将为 URL /index.php?action=pagename&amp;thing=something 输出以下内容

      pagename
      something
      

      虽然您可能应该先学习如何正确使用表单。

      【讨论】:

        猜你喜欢
        • 2023-03-13
        • 1970-01-01
        • 2018-10-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多