views:

115

answers:

4

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 there is a dropdown box that allows a person to choose the chapter they want to join. What I would like to do is have each chapter website use a specific link to the form that will preselect their chapter from the dropdown box.

After searching the web, I found that I will probably need to use a Javascript function to utilize a Query String. With all my searching, I still can't figure out the exact code to use. The page is a basic HTML page...no php and it is hosted on a linux server.

Any help would be greatly appreciated.

A: 

If you format your url like this:

www.myorg.com?chapter=1

You could add this script to your html head:

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

function loadform()
{

  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:

<body onload="loadform();" >

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

brendan
A: 

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 :).

Andrew Sledge
The user would be clicking on a link from the chapters individual website that directs them to the national (main) website. Since the chapters manage their own sites, they all have different hosts and I do not manage them. So all they would want is a special link (form.html?chapter=A) that will choose their chapter on our form. You code looks similar to others I have seen, but I have a few questions.1. "var chapter" - is the 'chapter' supposed to be the name of the fields ID?2. "chapter = "Not Found"" - is this supposed to be replace with the default option's name?
Stephen
3. "document.getElementById( "ChapterID").value = keyvalues['chapter'];" - Am I supposed to replace ChapterID with the actual ID name in my form? Also, the keyvalues ['chapter'] what am I supposed to change 'chapter' to if anything?
Stephen
A: 

maybe something using parse_url

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

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

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

easement
A: 

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.

Shawn Mclean