Skip to content Skip to sidebar Skip to footer

Query String For Pre-filling Html Form Field

I manage a website for an organization that has separate chapter sites. There is a membership signup form that is on the main website that each chapter links to. On the form ther

Solution 1:

If you format your url like this:

www.myorg.com?chapter=1

You could add this script to your html head:

functiongetparam(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = newRegExp(regexS);
    var results = regex.exec(window.location.href);
    if (results == null)
        return"";
    elsereturn results[1];
}

functionloadform()
{

  var list = document.getElementById("mychapterdropdown");
  var chapter = getparam("chapter");
  if (chapter>=0 && chapter < list.options.length)
  {
    list.selectedIndex = chapter;
  }
}

The in your html body tag:

<bodyonload="loadform();" >

Could probably add more validation checks but that's the general idea.

Solution 2:

It sounds like what you are looking for are GET or POST requests. For instance, if your user selects "Chapter A" from your form and hits select, you can allow redirects from another site (for instance http://www.yoursite.com/form.html?chapter=A) to allow Chapter A to be preselected. In Javascript this is done by

var chapter="";
var queryString = location.search.substring(1);
if ( queryString.length > 0 ) {
  var getdata = queryString.split("&");
  var keyvalues;
  for(var i=0; i < getdata.length; i++){
    keyvalues = getdata.split("=");
  }
} else {
  chapter = "Not Found";
}

document.getElementById( "ChapterID").value = keyvalues['chapter'];

This is untested, so don't hold me to it :).

Solution 3:

maybe something using parse_url

$params = parse_url()
$query = $params['query'];

$query_pairs = explode('&',$query);
$key_val = array();
foreach($query_pairsas$key => $val){
    $key_val[$key] = $val;
}

http://www.php.net/manual/en/function.parse-url.php

Solution 4:

You would probably have to use an dynamic ajax content. Use the following javascript to read the querystring, then load the html file in that div using this javascript.

Post a Comment for "Query String For Pre-filling Html Form Field"