【问题标题】:Query String in form does not work表单中的查询字符串不起作用
【发布时间】:2011-10-07 12:05:57
【问题描述】:

我的 form.php 文件中有以下表格:

<form action="operation.php?part=dictionary&operation=<?php echo (($_GET['action']=='addword')?'save':'edit&id='.$_GET['id'])?>" method="get">
.....
....
</form>

在我的 operation.php 文件中:

if($_GET['operation']=='save'){
        echo "This is true";
    }else{
        die(mysql_error());
    }

并显示无法识别操作参数的消息。 因此,如果有人知道如何区分保存和编辑之间的操作,将不胜感激。谢谢你

【问题讨论】:

  • 你做 GET 和 POST 有什么原因吗?
  • XSS 警报。 $_GET['id'] 应该是 htmlspecialchars($_GET['id'])。编辑:如果不强制转换为 int 和 urlencoded。
  • mysql_error() 有什么用??

标签: php forms query-string


【解决方案1】:

您可以尝试使用:

<form action="operation.php" method="get">
    <input type="hidden" name="part" value="dictionary">
    <input type="hidden" name="operation" value="<?php echo (($_GET['action']=='addword')?'save':'edit&id='.$_GET['id'])?>">
</form>

【讨论】:

    【解决方案2】:

    将表单的方法设置为“GET”会导致忽略添加到表单操作的所有 GET 参数。为了使这些参数起作用,您必须将它们添加为隐藏输入字段,否则您将表单的方法切换为“POST”。这会导致根据表单字段设置 POST 参数,并根据表单操作时添加的链接设置 GET 参数。

    【讨论】:

      【解决方案3】:

      您需要使用隐藏参数向表单提交值,如下所示:

      <form action="operation.php" method="GET">
          <input type="hidden" name="part" value="dictionary" />
          <input type="hidden" name="operation" value="<?php echo (($_GET['action']=='addword')?'save':'edit&id='.$_GET['id'])?>" />
      </form>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-06-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-05
        • 1970-01-01
        相关资源
        最近更新 更多