23

I'm trying to create a URL builder form with JavaScript or jQuery.

Basically, it will take the value of the two form fields, add them to a preset URL and show it on a third field on submit.

The resulting URL might be http://example.com/index.php?variable1=12&variable2=56

Now, this isn't the "action" of the form and the application can't read a URL (to grab the variables), so it has to be done on the page.

The resulting URL will be shown in the field named "url".

Here's a sample of the form:

<form id="form1" name="form1" method="post" action="">
  <p>
    <label>Variable 1
      <input type="text" name="variable1" id="variable1" />
    </label>
  </p>
  <p>
    <label>Variable 2
      <input type="text" name="variable2" id="variable2" />
    </label>
  </p>
  <p>
    <label>URL
      <input type="text" name="url" id="url" />
    </label>
  </p>
  <p>
    <input type="submit" name="button" id="button" value="Submit" />
  </p>
</form>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Troy
  • 961
  • 6
  • 13
  • 24

3 Answers3

27

jQuery has serialize which builds the query string values.

So if you want to do the entire form:

alert($("#form1").serialize());

If you want to do only a few fields, then just make the selector select those fields.

alert($("#variable1, #variable2").serialize());
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
epascarello
  • 204,599
  • 20
  • 195
  • 236
8

Use something like...

var inputs = $('#form1').find('input[type=text]').not('#url');
var str = "http://www.base.url/path/file.ext?"
inputs.each(function (i, item) {
    str += encodeURIComponent(item.name) + "=" + encodeURIComponent(item.value) + "&";
});
$('#url').val(str);

This will select all <input>s on in form1 with type='text', and concatenate them into a query string. See encodeURIComponent().


Orrrr.....you could just use .serialize(). Thank you, prodigitalson.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
clarkf
  • 3,032
  • 20
  • 14
  • Since youre already using jQ, why do all that when you can just use `$(inputSelector).serialize();`? – prodigitalson Oct 24 '10 at 03:27
  • You shoud include this: if(i<$(this).length){str +="&";} in the last line of each loop, because last time you have a trimming "&". – netadictos Oct 24 '10 at 10:52
0

Something like the following should work:

var partFields = '#variable1,#variable2';

$(partFields).change(function(){
  var url = 'static/URL/to/file.php?';
  $('#url').val(url + $(partFields).serialize());
});

However, unless you want people to be able to override the URL, you might want to use a hidden field and a regular element for display and submission of the URL value in which case you'd have something like the following:

var partFields = '#variable1,#variable2';

$(partFields).change(function(){
  var url = 'static/URL/to/file.php?';
  var urlValue = url + $(partFields).serialize();
  $('#url-display').text(urlValue); // Set the displaying element
  $('#url').val(urlValue); // Set the hidden input value
});
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
prodigitalson
  • 60,050
  • 10
  • 100
  • 114
  • I would use @clarkf line: var partFields = $('#form1').find('input[type=text]').not('#url'); Here you have the working demo: http://www.jsfiddle.net/dactivo/4Nmcx/ – netadictos Oct 24 '10 at 11:00