_GET将变量添加到现有字符串

时间:2012-11-19 12:22:45

标签: php html url-rewriting get

我卡住了,我有一个网站,其中包含索引的游戏资料,现在保持着历史记录。 只有当你手工编写网址时,才能实现和工作 赋予表单链接不起作用,因为它index.php?id=,然后我想要&date添加?date=,但&date=需要index.php?id=来自<form method='get' action='index.php?id=$data' > 的形式1}}

正常的用户输入通常会产生如下结果: 的的index.php?ID = 123456789

返回用户个人资料没问题 在那个页面上我有#34;加载历史数据&#34;下拉列表显示此配置文件的日期加载取决于id输入。

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

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

在页面上提交按钮返回:

的index.php?ID = 123456789?日期= 112012

然而,我能让这个工作的唯一方法是在ID和DATE之间将改为&amp; 对于我的生活,我无法理解!

基本上我需要:
index.php?id = 123456789 date = 112012
是的 index.php?id = 123456789 &amp; date = 112012

这样当页面重新加载时我可以看到ID和DATE 我想我可以通过帖子完成,但我需要它在网址中可见理想 我也尝试过:

{{1}}

3 个答案:

答案 0 :(得分:0)

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

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

str_replace()

然而,查看代码并查看'?'是多么的好看被添加到字符串中并替换它。

祝你好运, 卡斯帕

答案 1 :(得分:0)

获取

您无法使用get方法发送到已包含get参数的网址,因为您将获得来自不同浏览器的混合结果。您需要将它们添加到这样的隐藏字段,然后在发送表单时将其添加到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>

发表

如果你需要将表单方法设置为post,而在url中包含get params,则需要像这样完成:

<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

答案 2 :(得分:0)

我之前遇到过这个问题......这是我的解决方案。使用&符号构建一串参数。

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

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

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