1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Solved: How do I pass a variable in the URL of a page into a href link on that page?

Discussion in 'Web Design & Development' started by watchintv, Jan 22, 2011.

Thread Status:
Not open for further replies.
Advertisement
  1. watchintv

    watchintv Thread Starter

    Joined:
    May 27, 2005
    Messages:
    136
    so after users fill out a form on my website, they are taken to a confirmation page.

    as this is happening, i am passing thru their email to the confirmation page using a variable generated by the form on the previous page and placed into the URL of the confirmation page.

    so on the confirm page the url in the status bar looks like: http://website.com/confirm.php&[email protected]"

    what i want to do is then place that email variable into a link on the confirmation page, something like:
    <a href="http://website.com/index.php&em=EMAIL ADDRESS" >
    or
    <a href="http://website.com/index.php&[email protected]" >
    or
    <a href="http://website.com/index.php&em=[em]" >

    just to give you an idea of what im trying to accomplish.

    So when users click that link on the confirmation page, their email address is again passed thru to the next page.

    essentially, i want to pass the same variable through two pages

    the form is awebers, so with the following code within the <head></head> tags:
    Code:
    <script type="text/javascript">
    
    var formData = function() {
    
        var query_string = (location.search) ? ((location.search.indexOf('#') != -1) ? location.search.substring(1, location.search.indexOf('#')) : location.search.substring(1)) : '';
        var elements = [];
    
        if(query_string) {
           var pairs = query_string.split("&");
           for(i in pairs) {
              if (typeof pairs[i] == 'string') {
                  var tmp = pairs[i].split("=");
                  elements[unescape(tmp[0])] = unescape(tmp[1]);
              }
           }
        }
    
        return {
            display: function(key) {
                if(elements[key]) {
                  document.write(elements[key]);
                } else {
                  document.write("<!--If desired, replace everything between these quotes with a default in case there is no data in the query string.-->");
                }
            }   
        }
    
    }();
    
    </script>   
    i can then print out the email that is being pass thru in the URL onto the confirmation page with the code below :

    Code:
    <script type="text/javascript">formData.display("email")</script>
    Of course, what I really want to do is somehow place what that code above prints out (the email) into a new href link on the confirmation page, not just display it. im just showing this to give more context...

    not too bright with javascript or php so simple answers are nice :)

    any help would be greatly appreciated, thanks!
     
  2. andythepandy

    andythepandy

    Joined:
    Jul 2, 2006
    Messages:
    379
    You can get the variable from the url with a very simple piece of PHP:

    Code:
    <a href="index.php?em=<?php echo $_GET['em']; ?>">Click here</a>
    
    
     
  3. watchintv

    watchintv Thread Starter

    Joined:
    May 27, 2005
    Messages:
    136
    Thank you Andy, that was exactly what I needed.
     
  4. andythepandy

    andythepandy

    Joined:
    Jul 2, 2006
    Messages:
    379
    Glad it helped :)
     
  5. ehymel

    ehymel

    Joined:
    Aug 12, 2007
    Messages:
    696
    The answer from andy is exactly correct, but the original poster should understand the security risks involved in this sort of code. For example, I could easily just type the url with my own (or any arbitrary) email address, like

    Code:
    http://website.com/confirm.php&[email protected]
    If you aren't handling the $_GET variables appropriately, then bad or unintended things can happen. One rule of thing is the use $_GET only to pass info between pages, but never to alter or access anything on the server (like in a database). To more securely pass information, you could use POST or session variables instead. These are much harder to fake.

    I'm not saying it's wrong to use $_GET variables, I use them all the time. I'm just saying be aware of the risks and don't carry this example to another application that might be more security-centric.
     
  6. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/976162

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice