www.webdeveloper.com
Results 1 to 10 of 10

Thread: Pass the current URL as a hidden field through HTML form.

  1. #1
    Join Date
    Dec 2011
    Posts
    5

    Pass the current URL as a hidden field through HTML form.

    I have an HTML contact form on many different pages of a web site. I need to know which page the user was on when they submit the form.

    I need to pass the URL of the current page as a hidden field through the form when it's submitted. Please be specific with the code, as I'm nowhere near an expert at this.

    Do I need to add javascript to the header to call a function pulling the URL and then a secondary function to place it in the hidden field's "value" attribute?

  2. #2
    Join Date
    Mar 2011
    Posts
    65
    It's hard to be specific since you haven't posted any code, but in general all you need is a hidden input like

    Code:
    <input type="hidden" id="txtUrl" name="txtUrl" value="" />
    and below the hidden input just one line of javascript

    Code:
    document.getElementById('txtUrl').value = window.location.href;

  3. #3
    Join Date
    Dec 2011
    Posts
    5
    Specifically, if my page were this simple, where would you insert javascript or html to serve this purpose:

    <html>
    <head><title></title></head>
    <body>

    <FORM ID="frmMain" ACTION="https://www.customsite.com" METHOD=post>
    First name: <INPUT TYPE=TEXT MAXLENGTH=21 ID=FIRST_NAME VALUE="">
    Last name: <INPUT TYPE=TEXT MAXLENGTH=26 ID=LAST_NAME VALUE="">

    <INPUT TYPE="submit" VALUE="Submit">

    <INPUT TYPE="HIDDEN" NAME="URL_FROM" VALUE="****NEED-CURRENT-URL-HERE***">
    </FORM>
    </BODY>
    </HTML>

  4. #4
    Join Date
    Mar 2011
    Posts
    65
    anywhere you like in the <form>

  5. #5
    Join Date
    Dec 2011
    Posts
    5
    Quote Originally Posted by webdev1958 View Post
    It's hard to be specific since you haven't posted any code, but in general all you need is a hidden input like

    Code:
    <input type="hidden" id="txtUrl" name="txtUrl" value="" />
    and below the hidden input just one line of javascript

    Code:
    document.getElementById('txtUrl').value = window.location.href;
    So, do I need to somehow enclose that javascript to keep it from showing up on the web page? I put that code right below the <input> hidden field, but it shows up right on the web output page.

    Does it need to be saved as php or something? Like I said, I'm new at this.

  6. #6
    Join Date
    Mar 2011
    Posts
    65
    you haven't posted your updated code so how the heck do you expect someone to tell you what you have done wrong?

    Did you put your javascript inside a <script> tag? Why not post your code so we can see what is going on?

    I'm assuming you want to learn how to do this and not just want someone to spoon feed you the code. I've already given you 99.9&#37; of it.

    If you want to learn how to add javascript to your page a quick google will give you a ship load of information - one example set of tutes

    If you want someone to just spoon feed you the code, then hopefully someone else will come along.

    In any case, you'd be better off getting the page's url from a server side script rather than using javascript because obviously the javascript solution won't work in javascript disabled browsers.
    Last edited by webdev1958; 12-16-2011 at 07:41 PM.

  7. #7
    Join Date
    Jan 2009
    Location
    Insanity
    Posts
    1,131
    you NAME your INPUTS not ID them.

    If you have a server side like PHP then write the value in to the hidden form element and make it read only.

    PHP Code:
    <input name="contactformname" type="hidden" value="<?php echo basename($_SERVER['SCRIPT_FILENAME'],".php");?>" readonly />
    which will put the name of the script that was used to post, so your input field will be whatever name the page is that posted the comment or form. you would need to use a different method of identifying if your pages are all called index, you would need to include the relative path on the domain, similar process to that just demonstrated.
    We all have baggage to carry in life, unfortunately for me I always get the trolley with the wonky wheel...
    Code:
    Youre = {
          STILL_not_getting_it:function(){
               alert("YOU, the original poster / thread starter NEED to POST the code and NOT a LINK.");
          },
          MissingThePoint:function(msg){
                alert("You're missing the point. " + msg);
          }
    }
    Youre.STILL_not_getting_it();

  8. #8
    Join Date
    Dec 2011
    Posts
    5

    Great, almost there.

    I appreciate the constructive criticism, should've posted more code. Thanks for the code so far, Javascript worked, as did the server-side, but as you said I just get the script filename. I added REQUEST_URI and got the full relative path now, but still no domain/full URL.

    I'd like the value to show up as a full hyperlink, so I manually added the http://domainname but that's not the best way to do it, I'm sure. Here's what's working now:
    Code:
     <input type="hidden" name="current_url" value="<?php echo 'http://domainname.com',$_SERVER['REQUEST_URI'];?>" readonly/>
    Any ideas on setting this up as a request instead of hardcoding the domain? Thanks.
    Last edited by sdebord; 12-17-2011 at 02:20 PM.

  9. #9
    Join Date
    Jan 2009
    Location
    Insanity
    Posts
    1,131
    $_SERVER['SERVER_NAME'] will return something like www.domain.com so that means that you could...
    PHP Code:
     <input type="hidden" name="current_url" value="http://<?php echo $_SERVER['SERVER_NAME']."/".$_SERVER['REQUEST_URI'];?>" readonly/>
    I don't know but you may need to lose the ."/". for a . to join the string. $_SERVER['REQUEST_URI'] may be prefixed with a / which means you wouldn't need the ."/".
    We all have baggage to carry in life, unfortunately for me I always get the trolley with the wonky wheel...
    Code:
    Youre = {
          STILL_not_getting_it:function(){
               alert("YOU, the original poster / thread starter NEED to POST the code and NOT a LINK.");
          },
          MissingThePoint:function(msg){
                alert("You're missing the point. " + msg);
          }
    }
    Youre.STILL_not_getting_it();

  10. #10
    Join Date
    Dec 2011
    Posts
    5

    resolved

    That's exactly it, thanks! Just removed the / as you said:
    Code:
    <input type="hidden" name="current_url" value="<?php echo 'http://',$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];?>"  readonly/>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles