Select Order Reduce Choices

[Total: 0    Average: 0/5]

This gives the user a select list to choose items.

Body Code:

<script>
var FINAL_URL="/"; /* this is the target submit page of this form */
function validate(_form)
{
  if(params.length == selections.length-1)
    _form.action=FINAL_URL;
  else
    _form.action=document.location.toString();
  return true;
}

/*
 * This holds the parameters which have been selected in their order.
 */
var ORDER=new Array();

/*
 * Construct Item
 *  -- 2 parameters: value and text/label
 */
function Item(_value,_text)
{
  this.VALUE=_value;
  this.TEXT=_text;
  this.ORDER=null;
  this.SetOrder=function(_order){this.ORDER=_order;ORDER[_order]=this;}
}

/*
 * This holds the objects "Item" of what is selectable
 */
var selections=new Array();

/*
 * Variable "x" is used to allow selection order below to be any order you want
 * and still have it work.
 */
var x=0;

/*
 * These are the menu items to choose from:
 *  2 parameters: value submitted for selection, and text to show for that option
 *  Values should not be the same on any 2 items!
 */
    selections[x++]=new Item("1","One");
	selections[x++]=new Item("2","Two");
	selections[x++]=new Item("3","Three");
	selections[x++]=new Item("4","Four");
	selections[x++]=new Item("5","Five");

/*
 * This will determine if anything has been selected, and what the order is
 */
var params = document.location.search; /* parameter list */
if(params.indexOf("?")!=-1) /* parameters exist? */
{
  params = params.split("?")[1]; /* get rid of the "?" */
  params = params.split("&"); /* make an array of variable/value pairs */
  for(var i=0;i<params.length;i++) /* go through each parameter */
  {
	for(var j=0;j<selections.length;j++) /* find the object to mark it in its order */
	{
	  if(selections[j].VALUE==params[i].split("=")[1])
	  {
	    selections[j].SetOrder(i);
	  }
	}
  }
}
//document.write(params);
</script>
<form name=f1 onsubmit="return validate(this)">
<script>
/*
 * This will print out the hidden form components for things already selected
 * (make the type be text to see what it is doing / hidden to hide it)
 */
for(var i=0;i<ORDER.length;i++) { document.write("<input type=hidden name=h"+i+" value='"+ORDER[i].VALUE+"'>"); }
</script>
Select Next Item:
<select name=s1>
<script>
/*
 * This will print out the form selection list.
 */
for(var i=0;i<selections.length;i++)
{
  document.write("S("+i+").ORDER="+selections[i].ORDER+"<BR>");
  if(selections[i].ORDER == null)
    document.write("<option value='"+selections[i].VALUE+"'>"+selections[i].TEXT+"</option>");
}
</script>
</select>
<input type=submit value="Go">
</form>

Times Viewed: 5

Leave a Reply

Your email address will not be published. Required fields are marked *