0

This is the url after I submit the form

from=Paris%2C+France&to=Rome%2C+Italy

In the new page, I want the from value to be placed into the

<input id="from" class="form-control" type="text" name="from" placeholder="From?"> 

and the to value to be placed in here

<input id="to" class="form-control" type="text" name="to" placeholder="To?"> 

using Javascript or jQuery. I prefer javascript.

How to do this? Thank you.

Huangism
  • 16,278
  • 7
  • 48
  • 74
EnexoOnoma
  • 8,454
  • 18
  • 94
  • 179
  • 2
    possible duplicate of [How to get the value from URL Parameter?](http://stackoverflow.com/questions/979975/how-to-get-the-value-from-url-parameter) – George Nov 26 '14 at 19:34
  • This would be easier if you are using PHP, as you could get each item in the URL using `$_GET` – ntzm Nov 26 '14 at 19:34

1 Answers1

0

By using this function:

function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.search);

    if(results == null) {
        return "";
    } else {
      return decodeURIComponent(results[1]);
    }
}

You'll be able to get any URL parameters by name, like so:

var to = getParameterByName("to");
var from = getParameterByName("from");

Then, once you have the value you can use jQuery or JavaScript to set it, like so:

jQuery

$(function() {
   $("#to").val(to);
   $("#from").val(from);
});

JavaScript

document.getElementById("to").value = to;
document.getElementById("from").value = from;
Michael Hommé
  • 1,696
  • 1
  • 14
  • 18