Home > Software design >  How to get the form page url?
How to get the form page url?

Time:09-01

I want to get the live URL from which an HTML form has been submitted. Like a hidden input tag using a javascript function, which will send me the URL with the form submission. How can I do that?

CodePudding user response:

If your backend language is PHP, look for $_SERVER['HTTP_REFERER']. Other backend languages also have similar Referrer global variables.

CodePudding user response:

You can add submit event to your form, it will be executed shortly before submit action. And set value of hidden field.

let myForm = document.getElementById('form');
myForm.addEventListener('submit', function(){
  let myForm = document.getElementById('url').value = location.href;
})
<html>
<body>
<form id="form" action="" method="post">
  <input type="hidden" name="html" id="url"> 
  <input type="submit"> 
</form>
</body>
<html>

  • Related