admin管理员组文章数量:1309971
"Appending an url with appendages that were already in the previous url?" That's the most confusing title -- I know. But I can't think of a better way to explain it.
Maybe the below example will help.
I have URL 1: /?value=xyz&stuff=abc
If someone clicks on a link within the page, can I pass along the appended values?
ie: /?value=xyz&stuff=abc
Thanks and sorry for being such a noob.
"Appending an url with appendages that were already in the previous url?" That's the most confusing title -- I know. But I can't think of a better way to explain it.
Maybe the below example will help.
I have URL 1: http://example./?value=xyz&stuff=abc
If someone clicks on a link within the page, can I pass along the appended values?
ie: http://www.example/?value=xyz&stuff=abc
Thanks and sorry for being such a noob.
Share Improve this question edited Jan 9, 2013 at 1:30 John Carter 55.4k28 gold badges115 silver badges144 bronze badges asked Sep 17, 2012 at 23:47 user1678869user1678869 111 silver badge2 bronze badges 3- How this link is built? With JavaScript or PHP? – raina77ow Commented Sep 17, 2012 at 23:48
- 1 Where's your code. Have you tried anything yet? – Luke Mills Commented Sep 17, 2012 at 23:50
-
What do you mean pass it along? Do you want it in the URL? In PHP, that information is in
$_SERVER['QUERY_STRING']
– ernie Commented Sep 17, 2012 at 23:52
3 Answers
Reset to default 7These appendages you ask about is the actual so called query part of an URI:
<scheme>://<authority><path>?<query>
foo://example.:8042/over/there?name=ferret#nose
\_/ \______________/\_________/ \_________/ \__/
| | | | |
scheme authority path query fragment
Taken from: 3. Syntax Components (RFC 3986) https://www.rfc-editor/rfc/rfc3986#page-16
You then need a helper function that is append the (optional) <query>
to an existing <scheme>://<authority><path>
part. I ignore the <fragment>
for this exmaple, as it would needed to be added at the end and I want to leave something as an exercise:
function href_append_query($href)
{
$query = isset($_SERVER['QUERY_STRING'])
? '?' . $_SERVER['QUERY_STRING']
: ''
;
$query = strtr(
$query, [
'"' => '"',
"'" => ''',
'&' => '&'
]
);
return $href . $query;
}
And it's usage:
<a href="<?=href_append_query('http://step2./')?>Some link</a>
This little function will ensure that the exisitng QUERY_STRING
which can be obtained via $_SERVER
Docs is encoded for HTML output.
If links are built with PHP, just append the original query string to them:
<a href="http://step2./?<?php echo $_SERVER['QUERY_STRING']; ?>">Some link</a>
In JavaScript, just append the value of window.location.search
to all the links you need to 'pass' the query string.
You could also use js/jquery to append the existing query string to each link on the page:
$(function() {
$('a').each(function() {
link = $(this).attr('href');
query = window.location.search;
if (link.indexOf('?') !== -1 && query !== '') {
query = query.replace('?','&');
}
$(this).attr('href',link+query);
});
});
本文标签: phpAppending query strings to URLStack Overflow
版权声明:本文标题:php - Appending query strings to URL - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741789564a2397580.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论