0

This is the field that I want to get prefilled by the current page's url

<input type="url" class="form-control" name="inv" id="inv" placeholder="Referrer's Link" size="100"  />

Anyone with any idea please show me how

I am aware of the javascript code

document.getElementById('inv').value = window.location.href;

The problem is that I dont understand how to implement it

miken32
  • 42,008
  • 16
  • 111
  • 154
  • 1
    `document.getElementById('inv').value = window.location.href;` – Ryan Wilson May 14 '19 at 19:15
  • Possible duplicate of [Get the current URL with JavaScript?](https://stackoverflow.com/questions/1034621/get-the-current-url-with-javascript) – Ryan Wilson May 14 '19 at 19:15
  • @RyanWilson that doesnt answer my question! –  May 14 '19 at 19:18
  • Now how do I implement that to prefil the url field?? –  May 14 '19 at 19:19
  • You need to add the value to the input after the dom has loaded, there is a `DOMContentLoaded` event you can subscribe to in your javascript file then use the line above. (https://developer.mozilla.org/en-US/docs/Web/API/Window/DOMContentLoaded_event) – Ryan Wilson May 14 '19 at 19:22
  • but why not do it with php? https://stackoverflow.com/questions/6768793/get-the-full-url-in-php – Elena Roman May 14 '19 at 19:28

1 Answers1

0

First dont use type="url", its down supported by IE, via php get all url data from $_SERVER and you can concat what ever you want.

<?php
$actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://".$_SERVER[HTTP_HOST].$_SERVER[REQUEST_URI];

?>
<input value="<?php echo $actual_link?>" type="text" class="form-control" name="inv" id="inv" placeholder="Referrer's Link"   />

Eli Meiler
  • 59
  • 4
  • Don't use something because a decade-old browser with minimal usage doesn't support it? https://caniuse.com/#feat=input-email-tel-url – miken32 May 14 '19 at 19:55
  • Perfect! You are amazing.. I've really been looking for this. Thank you –  May 14 '19 at 20:08