Jump to content

variables in links


shadowmonk

Recommended Posts

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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;

?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.