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.
It's hard to be specific since you haven't posted any code, but in general all you need is a hidden input like
<input type="hidden" id="txtUrl" name="txtUrl" value="" />
document.getElementById('txtUrl').value = window.location.href;
<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***">
anywhere you like in the <form>
Originally Posted by webdev1958
Does it need to be saved as php or something? Like I said, I'm new at this.
you haven't posted your updated code so how the heck do you expect someone to tell you what you have done wrong?
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% of it.
If you want someone to just spoon feed you the code, then hopefully someone else will come along.
Last edited by webdev1958; 12-16-2011 at 08:41 PM.
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.
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.
<input name="contactformname" type="hidden" value="<?php echo basename($_SERVER['SCRIPT_FILENAME'],".php");?>" readonly />
Great, almost there.
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:
Any ideas on setting this up as a request instead of hardcoding the domain? Thanks.
<input type="hidden" name="current_url" value="<?php echo 'http://domainname.com',$_SERVER['REQUEST_URI'];?>" readonly/>
Last edited by sdebord; 12-17-2011 at 03:20 PM.
$_SERVER['SERVER_NAME'] will return something like www.domain.com so that means that you could...
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 ."/".
<input type="hidden" name="current_url" value="http://<?php echo $_SERVER['SERVER_NAME']."/".$_SERVER['REQUEST_URI'];?>" readonly/>
That's exactly it, thanks! Just removed the / as you said:
<input type="hidden" name="current_url" value="<?php echo 'http://',$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];?>" readonly/>
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)