SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Zealot
    Join Date
    Jul 2005
    Location
    Osoyoos BC Canada
    Posts
    178
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Passing a URL to the page it calls.

    I'm stuck trying to pass the contents of a js variable, location.href (which is the current page) to the page that that page calls.

    What I want to do is: On a site, when a viewer hits the 'contact us' button, a new page opens with a contact form which will in turn be submitted to a php script on the server.

    I want to pass the url of the page that called the form to the php script in a hidden field, so that when the php form handler emails me the form result it tells me what page the viewer was on when they hit the contact me button, and then after that I want to send the viewer back to that page,

    In a page I can get the page url using js to get location.href and place it in a variable. But after calling the form page I don't see how to put that variable which I think is global(?) into a hidden form field to post when the form is submitted.

    Once it is at the server I have no trouble with the php to do what I want, I just can't see how to get the javascript variable to persist across site pages.

    Thanks for any help!

  2. #2
    SitePoint Addict richtestani's Avatar
    Join Date
    Nov 2003
    Location
    Bridgeport
    Posts
    292
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think you want the document.referrer property. This returns the previous page's URL.
    RichTestani
    -------------------------------
    http://www.junkdepot.com
    http://www.rareoopdvds.com | The Movie Poster Site

  3. #3
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    You can also do that with PHP. The reserved variable $_SERVER['HTTP_REFERER'] handles that. This has the advantage of not relying on Javascript, which allegedly some people have turned off.

    Bear in mind that there's no referer if the user types the URL into the browser directly, so don't make your script depend on it in any way.

  4. #4
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,862
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    It is also possible to change the browser or firewall options to not pass that field. If you want to make sure it is always passed then pass it in the querystring on the end of the URL on your button. Like this (in HTML):

    <a href="nextpage.htm?ref=thispage.htm">

    or like this in Javascript:

    location.href = 'nextpage.htm?ref=' + encodeURIComponent(location.href);
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  5. #5
    SitePoint Zealot
    Join Date
    Jul 2005
    Location
    Osoyoos BC Canada
    Posts
    178
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks!

    Surely in PHP, $_SERVER['HTTP_REFERER'] holds current page that called the script, not the 'page that called that page'?

    I tried the javascript document.referrer property, thus:

    Code:
    <script language="Javascript" type="text/javascript">
    <!--
    
    if (document.referrer&&document.referrer!="")
      document.write('The page you were on when you clicked on CONTACT ME was: '+document.referrer);
      var callingPage=document.referrer;
      
    
    //-->
    </script>
    This works and puts the url of the calling page into the variable callingPage.

    Now I need to insert that url into a hidden field to pass with the form to the server.

    I realize that I (probably) need to insert a hidden form field in my form, using this syntax:

    Code:
    <input type = "hidden" name = "callingPage" value ="" />
    but can't see how to place the contents of the javascript variable callingPage into the value before submitting the form.

  6. #6
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    No, in PHP $_SERVER['HTTP_REFERER'] is the page that referred the user to the current page. See here:

    http://au.php.net/reserved.variables

    With your JS method, you could do document.getElementById('whatever').value=callingPage

    You have to give the input the id "whatever" and decide what event will trigger that.

  7. #7
    SitePoint Member
    Join Date
    Sep 2006
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Another idea is to have the form generated by a PHP script. Use the php environment variable to get the calling page and insert it into the form as a hidden field. Then when the form is sent back to the server, that field will carry the calling page's identity.

  8. #8
    SitePoint Zealot
    Join Date
    Jul 2005
    Location
    Osoyoos BC Canada
    Posts
    178
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    From http://http://au.php.net/reserved.variables


    $_SERVER['HTTP_REFERER'] The address of the page (if any) which referred the user agent to the current page. This is set by the user agent. Not all user agents will set this, and some provide the ability to modify HTTP_REFERER as a feature. In short, it cannot really be trusted.

    Surely this is saying that it contains the address of the page that contains the form that submitted to the current page which is the php script. I want the url of the page the user was on when they called the form. Ie one step further back...

    After loading the form I have the right value in callingPage so I tried placing it in the form using this:
    Code:
    <input type = "hidden" name = "callingPage" 
    
    value ="<script language="Javascript" 
    
    type="text/javascript">callingPage</script>"
    But the contents of callingPage weren't inserted into the form field...

  9. #9
    SitePoint Zealot
    Join Date
    Jul 2005
    Location
    Osoyoos BC Canada
    Posts
    178
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Got there in the end!

    I couldn't place the script tags within the form input tag, but could put the input tag in the script. Thus:

    Code:
    <script language="JavaScript" type="text/javascript">
    
    <!--
    
    document.write('<input type="hidden" name="callingPage" value="' + 
    
    callingPage + '" />');
    
    //-->
    
    </script>
    This works, the value in callingpage, obtained from document.referrer is placed in the hidden field and passed to the php script. After processing the form and emailing the page, the browser/client is sent back to the page they were on when the hit Contact me.

    If javascript is turned off at the client's browser then the hidden field is not created, but no matter, document.referrer wouldn't have executed either, so there would be no url to pass.

    Thanks everyone!

  10. #10
    SitePoint Member
    Join Date
    Sep 2006
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could just write the form field using php and then it wont matter if javascript is turned off.

    Code:
    <?php
    
    echo "<input type='hidden' name='callingPage' value='" . $_SERVER['HTTP_REFERER'] . "' />";
    
    ?>


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •