shadowmonk Posted May 18, 2007 Share Posted May 18, 2007 Hi, I know there is another thread on here that is along these lines, but this one is different, trust me. I have a link that is already passing variables, my problem is trying to add onto the link. If i use the $_SERVER['PHP_SELF'] variable I only get up to the filename. If I use the # sign in the href tag I get the whole link, but I can't append a variable to it. Is this possible or do I need to create multiple links based on what variables are present? Quote Link to comment https://forums.phpfreaks.com/topic/52033-variables-in-links/ Share on other sites More sharing options...
Wildbug Posted May 18, 2007 Share Posted May 18, 2007 urlencode() Quote Link to comment https://forums.phpfreaks.com/topic/52033-variables-in-links/#findComment-256466 Share on other sites More sharing options...
shadowmonk Posted May 18, 2007 Author Share Posted May 18, 2007 I don't think urlencode() will work, based on how I'm understanding the documentation and examples. Let me try to clarify my problem with examples- say my link is already: http://mysite.com/script.php?var=rand What I'm trying to do is using a link within the page to append "&var2=foobar" If I use the php_self I just get: http://mysite.com/script.php&var2=foobar If I use # in the href it's: http://mysite.com/script.php?var=rand#&var2=foobar The # is close, but it is looking for a link titled "&var2=foobar" somewhere on the page instead of passing it to the script as a variable. I was thinking the javascript this.href might work, but I can't figure out how to get it to work. Quote Link to comment https://forums.phpfreaks.com/topic/52033-variables-in-links/#findComment-256490 Share on other sites More sharing options...
chigley Posted May 18, 2007 Share Posted May 18, 2007 $_SERVER["QUERY_STRING"] Quote Link to comment https://forums.phpfreaks.com/topic/52033-variables-in-links/#findComment-256497 Share on other sites More sharing options...
Wildbug Posted May 18, 2007 Share Posted May 18, 2007 Oh, well, check the phpinfo() function. There are other vars besides PHP_SELF. <?php $newvar = 'this=that'; $newlink = $_SERVER['PHP_SELF'] . '?' . $_SERVER['QUERY_STRING'] . ($_SERVER['QUERY_STRING'] ? '&' : '') . $newvar; ?> Quote Link to comment https://forums.phpfreaks.com/topic/52033-variables-in-links/#findComment-256498 Share on other sites More sharing options...
roopurt18 Posted May 18, 2007 Share Posted May 18, 2007 Use string concatenation. <?php // Long version $url = $_SERVER['PHP_SELF']; $url .= "?var=foo"; $url .= "&var2=bar"; // Short version $url = $_SERVER['PHP_SELF'] . "?var=foo&var2=bar"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/52033-variables-in-links/#findComment-256499 Share on other sites More sharing options...
shadowmonk Posted May 18, 2007 Author Share Posted May 18, 2007 the $_SEVER['QUERY_STRING'] works, I just needed to explode it into an array so I don't keep adding the same variable onto it every time it's clicked. I think I'm going to leave this open for a little bit, just in case I have any more questions related to this. Quote Link to comment https://forums.phpfreaks.com/topic/52033-variables-in-links/#findComment-256524 Share on other sites More sharing options...
kenrbnsn Posted May 18, 2007 Share Posted May 18, 2007 Instead of using the explode() function, you probably want to use the parse_str() function. Ken Quote Link to comment https://forums.phpfreaks.com/topic/52033-variables-in-links/#findComment-256533 Share on other sites More sharing options...
shadowmonk Posted May 18, 2007 Author Share Posted May 18, 2007 I could see the benefit of using parse_str() in other situations, but in this case using the explode() function allows me to keep the same link for multiple uses rather than just once. I might end up using it though, depending on how the project progresses. Quote Link to comment https://forums.phpfreaks.com/topic/52033-variables-in-links/#findComment-256544 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.