【问题标题】:_GET adding variable to existing string_GET 将变量添加到现有字符串
【发布时间】:2012-11-07 08:55:23
【问题描述】:

我卡住了,我有一个网站,它索引了玩家的个人资料,现在保持着历史记录。 仅当您手动写出网址时,一切都已实现并正常工作。
跟随表单创建的链接不起作用,因为它的index.php?id= 然后我想将&date 添加到此但?date= 需要从index.php?id= 顶部的表单中添加&date=

普通用户输入 normalyresults 类似: index.php?id=123456789

返回用户配置文件没问题 在该页面上,我有“加载历史数据”下拉菜单,其中显示了此配置文件的加载日期,具体取决于 id 输入。

<form method='get' action='index.php?id=$data' >

跟随页面上的提交按钮返回:

index.php?id=123456789?date=112012

但是,我可以让它工作的唯一方法是手动将 ? 更改为 ID 和 DATE 之间的 & 对于我的生活,我无法弄清楚!

基本上我需要:
index.php?id=123456789 ? date=112012
成为
index.php?id=123456789 & date=112012

这样当页面重新加载时,我可以同时看到 ID 和 DATE 我想我可以通过帖子完成,但我需要它在 url 中理想地显示
我也试过了:

<form method='get' action='$_SERVER['PHP_SELF']'>

<form method='get' action='id_$data'> (with a mod rewrite)

【问题讨论】:

  • 将 id 与隐藏输入一起放置。 &lt;input type="hidden" value="&lt;?php echo $data?&gt;"&gt;
  • 表单的动作不会对页面重新加载有用。

标签: php html url-rewriting get


【解决方案1】:

您可以使用 str_replace() 替换所有 ?进入&

   $replacedstring = str_replace ( '?' , '&' , '$stringtobereplaced' )

str_replace()

但是最好在代码中查找并查看'?被添加到字符串中并在那里替换它。

最好的问候, 卡斯帕

【讨论】:

    【解决方案2】:

    获取

    您不能使用 get 方法发送到已经包含 get 参数的 url,因为您会从不同的浏览器获得混合结果。您需要将它们添加到这样的隐藏字段中,然后在发送表单时将其添加到 URL:

    <form method='get' action='index.php'>
    <input type='hidden' name='id' value='<?php print $data; ?>' />
    <input type='hidden' name='date' value='112012' />
    <input type='submit' value='Submit' />
    </form>
    

    发布

    如果您需要将表单方法设置为发布,同时在 url 中包含 get 参数,则需要这样做:

    <form method='get' action='index.php?id=<?php print $id; ?>&date=112012'>
    <input type='text' name='message' value='this is in POST, the other params are in GET' />
    <input type='submit' value='Submit' />
    </form>
    

    安全

    不要这样做:

    <form method='get' action='$_SERVER['PHP_SELF']'>
    

    Reason why not

    【讨论】:

    • 已排序,感谢 VBAssassin 对我有意义,并且正在工作。我可以做所有其他事情,我只是用表格很糟糕,因为我很喜欢它们......也感谢其他所有回答的人。现在我明白了它是如何工作的,我觉得很愚蠢!
    • 欢迎您,我只是在底部添加了安全位,以防您错过它;)
    【解决方案3】:

    我以前也遇到过这个问题……这是我的解决方案。用 & 号构建一串参数。

    $parameters = "&amp;id=9&amp;date=tomorrow&amp;x=y";

    然后我只是用问号替换了第一个&符号。

    $correct_string = "?" . ltrim($parameters, '&amp;');

    【讨论】:

      猜你喜欢
      • 2017-09-09
      • 1970-01-01
      • 2013-11-25
      • 2017-04-03
      • 1970-01-01
      • 2021-12-21
      • 2019-06-16
      • 2023-03-12
      • 2015-11-14
      相关资源
      最近更新 更多