cancel
Showing results for 
Search instead for 
Did you mean: 

On form submit get current URL

SOLVED

On form submit get current URL

Hi Guys, a little help here: 

 

O used to have a input type="text" in a form of mine with 

value="<?php echo $_SERVER['HTTP_REFERER']; ?>

 and it stopped working.

I went back to code and now is showing 

value="&lt;?php echo $_SERVER['HTTP_REFERER']; ?&gt;

so I believe that it stopped working due to this caracthere conversion. 

 

Anyone has another code that would record the URL as value via js using input id or name? 

Or anyother way that you might think that would work better? 

 

Tks

1 ACCEPTED SOLUTION

Accepted Solutions

Re: On form submit get current URL

@anna carolina_caro 

for that, you can update with the following code : 

$("input[name=urlencodeenable]").val(document.URL);

or

$("input[name=urlencodeenable]").val(window.location.href );

 

kindly Accept as a Solution if this works for you and give Kudos Smiley Happy 

View solution in original post

9 REPLIES 9

Re: On form submit get current URL

@anna carolina_caro ,

seems like you are using this in Admin CMS Area 

then you need to use JS to fill referrer 

for that, you can update the form with following 

<input type="hidden" id="referrer" value="" >

and a JS 

<script>
require(['jquery'], function($){
    $(document).ready(function() {
        $("input[id=referrer]").attr('value', document.referrer);
    });
});
</script>

 this will fill the input with the referrer. 

 

Re: On form submit get current URL

@amitsamsukha yes, it´s a cms block actually. 

I´d tried your code however is not retrieving URL, it´s still blank. I´d cleaned magento cache and browser cache after changes as you suggested. 

 

Here is my input:

 

<input id="referrer" hidden="" maxlength="255" name="cf_contacts_urlcontato" type="text" value="" data-label="" />

 

Here is script:

 

<script type="text/javascript" xml="space">// <![CDATA[
require(['jquery'], function($){
    $(document).ready(function() {
        $("input[id=referrer]").attr('value', document.referrer);
    });
});
// ]]></script>

 

Re: On form submit get current URL

@anna carolina_caro ,

can you give me URL so that I can check and help 

Re: On form submit get current URL

sure @amitsamsukha  I will send URL via private message. 

Just want to explain that the form is inputed via block on CMS pages, so I´d live to know in which page the form as sent from. 

Re: On form submit get current URL

@anna carolina_caro 

you have placed the wrong ID in the script 

kindly try updating the script with the same id that is used for input

 

<script type="text/javascript" xml="space">// <![CDATA[
require(['jquery'], function($){
    $(document).ready(function() {
        $("input[name=urlencodeenable]").val(document.referrer);
    });
});
// ]]></script>

Re: On form submit get current URL

@amitsamsukha  I followed your instruction and now is showing www.google.com, I believe this is where the user was initially forwarded from, right? Actually it´s kind of usefull, I think I will keep that as well. Smiley Happy 

However my initial need that is to get URL where fom was filled out is still pending. 

Re: On form submit get current URL

@anna carolina_caro 

for that, you can update with the following code : 

$("input[name=urlencodeenable]").val(document.URL);

or

$("input[name=urlencodeenable]").val(window.location.href );

 

kindly Accept as a Solution if this works for you and give Kudos Smiley Happy 

Re: On form submit get current URL

@amitsamsukha thank you very much! Working again now! Smiley Happy 

Re: On form submit get current URL

In pure jQuery style:

$(location).attr('href');

The location object also has other properties, like host, hash, protocol, and pathname.

var x = $(location).attr('<property>');

Also, you can get this done by using JavaScript's built-in window.location object .

var pathname = window.location.pathname; // Returns path only
var url = window.location.href; // Returns full URL
var origin = window.location.origin; // Returns base URL