[英]Appending GET parameters on HREF tag with PHP
I'm working on a code like this: 我正在像这样的代码:
<?php
$id=$_POST['id'];
$url_tag = $_POST['url_tag'];
$url_back = 'https://www.page.example.com/page.php?';
$query='id='.$id.'&url_tag='.$url_tag;
$url = $url_back.$query;
echo 'Look how this url shows up: '.$url;
echo '<a href='.$url.'>Click here</a>';
?>
This is, the page receives two POST parameters. 即,页面接收两个POST参数。 Then prepare a link to
https://www.page.example.com/page.php?
然后准备指向
https://www.page.example.com/page.php?
的链接https://www.page.example.com/page.php?
and I append those two parameters as GET parameters with the ids id
and url_tag
respectively. 然后将这两个参数分别作为id为
id
和url_tag
GET参数附加。
Then I display how the whole link looks like. 然后,我显示整个链接的外观。 It shows up correctly, in this case
https://www.page.example.com/page.php?id=ID&url_tag=URL_TAG
, where ID and URL_TAG are the actual values received as POST parameters. 它可以正确显示,在这种情况下为
https://www.page.example.com/page.php?id=ID&url_tag=URL_TAG
,其中ID和URL_TAG是作为POST参数接收的实际值。
However, when I click on the ' Click here ' link, it redirects me to https://www.page.example.com/page.php?
但是,当我单击“ 单击此处 ”链接时,它会将我重定向到
https://www.page.example.com/page.php?
, which is the url without any GET parameter. ,即没有任何GET参数的网址。
Why is that happening and how would I solve it? 为什么会发生这种情况,我将如何解决? I've tried to feed HREF with urlencode($url) instead, but it redirects me to an address flooded with undesired characters...
我试图用urlencode($ url)来给HREF喂,但是它会将我重定向到一个充满了不想要的字符的地址...
Any idea? 任何想法? Thank you!
谢谢!
Try to replace the last line of your code by this: 尝试用以下代码替换代码的最后一行:
echo '<a href="'.$url.'">Click here</a>';
It should work. 它应该工作。
Try using http_build_query()
, it takes care of any URL character compatibility issues for you... 尝试使用
http_build_query()
,它可以为您解决所有URL字符兼容性问题...
// assuming you've already checked and validated your $_POST parameters
$query = http_build_query(array(
'id' => $_POST['id'],
'url_tag' => $_POST['url_tag']
));
$url = 'https://www.page.example.com/page.php?' . $query;
?>
<a href="<?= $url ?>">Click here</a>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.