Hi
I'm now trying to have a dependent lists in a form
my form is a query based form
i.e. I fill my MySQL query from this form
I have around 30 fields/columns
each have number of values... sometimes 2 , sometimes 20.. maybe more !
I've searched the web to find a javascript code that helps me to do the dependent lists
I found few funtions.. but their work depends on INDEX !
they're very powerful functions.. i liked them.. but didn't know how to modify them until i get what i'm looking for
This is what I've found ( in two diff websites) - it's working if you copied it and pasted then tried it !
[code=html]
<html>
<head>
<script type="text/javascript">
function DepSelectOne(ma sterSelect){
var frm = masterSelect.fo rm;
this.selectedBl ock = 0;
this.selBoxes = new Array(masterSel ect);
if((frm)&&(type of Option != 'undefined')){
for(var c = 1;c < arguments.lengt h;c++){
var selObj = frm[arguments[c]];
if((selObj)&&(s elObj.type.inde xOf('select') == 0)&&(selObj.typ e.indexOf('mult ') < 0)&&(selObj.opt ions.length > 2)){
this.selBoxes[this.selBoxes.l ength] = frm[arguments[c]];
}
}
if((this.selBox es.length >1)&&(this.selB oxes.length == arguments.lengt h)){
this.selectElem ent = this.selBoxes[0];
DepSelectOne.in st[this.index = this.selectElem ent.name] = this; //keep global record of this object instance.
this.selBoxes[0] = this;
this.optionBloc k = new OptionBlock(0, this.selectElem ent, this.selectElem ent.options[0].value, this, 0);
this.next = new DepSelectOneNex t(this.selBoxes , 1);
this.next.setSe lection(this.op tionBlock.getSe lection());
this.selectElem ent.onchange = new Function('DepSe lectOne.inst[\''+this.index+ '\'].handleChange(0 )');
}else{
//alert('DepSelec tOne constructor could not find\nsufficien t properly configured SELECT elements\nto initialise.');
}
}
}
DepSelectOne.in st = {}; //Globally accessible object to hold instances of the DepSelectOne Class.
//call chain terminating functions.
DepSelectOne.pr ototype.validat eNext = DepSelectOne.pr ototype.reset = DepSelectOne.pr ototype.setSele ction = DepSelectOne.pr ototype.setSele ctedFromChild = function(){retu rn true;}
DepSelectOne.pr ototype.validat e = function(){
return ((this.optionBl ock.lastSelecti on < 0)?false:this.n ext.validateNex t());
}
DepSelectOne.pr ototype.init = function(select edBlock){
if(selectedBloc k >= 0){
this.optionBloc k.lastSelection = selectedBlock;
}
if(this.optionB lock.lastSelect ion >= 0){
this.optionBloc k.doElement();
if(window.setTi meout)setTimeou t('DepSelectOne .inst[\''+this.index+ '\'].refresh();', 5); //fix for Opera 7 timing problem. Harmless otherwise.
}
}
DepSelectOne.pr ototype.handleC hange = function(ind){
this.selBoxes[ind].changed();
if(window.setTi meout)setTimeou t('DepSelectOne .inst[\''+this.index+ '\'].refresh();', 5); //fix for Opera 7 timing problem. Harmless otherwise.
}
DepSelectOne.pr ototype.refresh = function(){
this.selectElem ent.selectedInd ex = this.optionBloc k.lastSelection ;
this.next.reset ();
}
DepSelectOne.pr ototype.changed = function(){
this.optionBloc k.readState();
this.next.setSe lection(this.op tionBlock.getSe lection());
}
function DepSelectOneNex t(selBoxes, selBoxIndex){
this.selectedBl ock = -1;
this.classObj = selBoxes[0];
this.previous = selBoxes[(selBoxIndex-1)]
this.selectElem ent = selBoxes[selBoxIndex];
selBoxes[selBoxIndex] = this;
this.selectElem ent.onchange = new Function('DepSe lectOne.inst[\''+this.classO bj.index+'\'].handleChange(' +selBoxIndex+') ');
this.seperatorO ption = new DepOption(this. selectElement.o ptions[0]);
this.optionBloc ks = [];
var startIndex = 1;
while(startInde x < this.selectElem ent.options.len gth){
var nextIndex = this.optionBloc ks.length;
this.optionBloc ks[nextIndex] = new OptionBlock(sta rtIndex, this.selectElem ent, this.selectElem ent.options[0].value, this, nextIndex);
startIndex += this.optionBloc ks[nextIndex].getOptionsTota l();
}
for(var cnt = 0,c = 0;c < this.optionBloc ks.length;c++){
this.optionBloc ks[c].blockStart = cnt;
cnt += this.optionBloc ks[c].getItems();
}
if(++selBoxInde x < selBoxes.length ){
this.next = new DepSelectOneNex t(selBoxes, selBoxIndex);
}else{
this.next = this.classObj;
this.previous.i nit(this.select edBlock);
}
}
DepSelectOneNex t.prototype.res et = function(){
if((this.select edBlock >= 0)&&(this.optio nBlocks[this.selectedBl ock].lastSelection >= 0)){
this.selectElem ent.selectedInd ex = this.optionBloc ks[this.selectedBl ock].lastSelection;
this.next.reset ();
}
}
DepSelectOneNex t.prototype.set Selection = function(selBlo ck){
this.selectedBl ock = selBlock;
if(this.selecte dBlock < 0){
var opt = this.selectElem ent.options;
opt.length = 0; //clear options
opt[0] = this.seperatorO ption.getOption ();
this.selectElem ent.selectedInd ex = 0;
this.next.setSe lection(-1);
}else{
var blk = this.optionBloc ks[this.selectedBl ock];
blk.doElement() ;
this.next.setSe lection(blk.get Selection());
}
}
DepSelectOneNex t.prototype.cha nged = function(){
if(this.selecte dBlock >= 0){
var blk = this.optionBloc ks[this.selectedBl ock];
blk.readState() ;
this.next.setSe lection(blk.get Selection());
}
}
DepSelectOneNex t.prototype.set SelectedFromChi ld = function(blockI ndex){
if(this.selecte dBlock < 0){ //only set from OptonBlock on the first attempt.
this.selectedBl ock = blockIndex;
}
}
DepSelectOneNex t.prototype.ini t = function(selBlo ck){
if(selBlock >= 0){
var blockIndex = 0;
while(!this.opt ionBlocks[blockIndex++].nextBlockTest( selBlock));
}
this.previous.i nit(this.select edBlock);
}
DepSelectOneNex t.prototype.val idateNext = function(){
return (((this.selecte dBlock < 0)||(this.optio nBlocks[this.selectedBl ock].lastSelection < 0))?false:this. next.validateNe xt());
}
function DepOption(optEl ){
this.value = optEl.value;
this.text = optEl.text;
}
DepOption.proto type.getOption = function(){
return new Option(this.tex t, this.value);
}
function OptionBlock(sta rtIndex, selectElement, seperatorValue, owner, blockIndex){
this.blockStart = 0;
this.owner = owner;
this.index = blockIndex;
this.selectElem ent = selectElement;
this.seperators = [];
this.optionBloc k = [];
this.lastSelect ion = -1;
var opts = this.selectElem ent.options;
while((startInd ex < opts.length)&&( opts[startIndex].value == seperatorValue) ){
this.seperators[this.seperators .length] = new DepOption(opts[startIndex++]);
}
while((startInd ex < opts.length)&&( opts[startIndex].value != seperatorValue) ){
if((this.lastSe lection < 0)&&(opts[startIndex].selected == true)){
this.lastSelect ion = this.optionBloc k.length; //record first item selected, if any.
}
this.optionBloc k[this.optionBloc k.length] = new DepOption(opts[startIndex++]);
}
if(this.lastSel ection >= 0)this.owner.se tSelectedFromCh ild(this.index)
}
OptionBlock.pro totype.getOptio nsTotal = function(){
return (this.seperator s.length + this.optionBloc k.length);
}
OptionBlock.pro totype.getItems = function(){
return this.optionBloc k.length;
}
OptionBlock.pro totype.nextBloc kTest = function(selBlo ck){
if((selBlock >= this.blockStart )&&(selBlock < this.blockStart +this.optionBlo ck.length)){
this.lastSelect ion = selBlock - this.blockStart ;
this.owner.sele ctedBlock = this.index;
return true;
}
return false;
}
OptionBlock.pro totype.getSelec tion = function(){
return ((this.lastSele ction < 0)?-1:(this.blockSt art+this.lastSe lection));
}
OptionBlock.pro totype.doElemen t = function(){
var opt = this.selectElem ent.options;
opt.length = 0; //clear options
if((this.lastSe lection < 0)&&(this.seper ators.length > 0)){
opt[opt.length] = this.seperators[(this.seperator s.length-1)].getOption();
}
for(var c = 0;c < this.optionBloc k.length;c++){
opt[opt.length] = this.optionBloc k[c].getOption();
}
this.selectElem ent.selectedInd ex = ((this.lastSele ction < 0)?0:this.lastS election);
}
OptionBlock.pro totype.readStat e = function(){
if(this.lastSel ection < 0){
this.lastSelect ion = (this.selectEle ment.selectedIn dex - 1);
this.doElement( );
}else{
this.lastSelect ion = this.selectElem ent.selectedInd ex;
}
}
</script>
</head>
<body onload="new DepSelectOne(do cument.forms['StateCityForm'].elements['State'], 'City');">
<form name="StateCity Form" ACTION="" METHOD="GET" TARGET="_blank" >
First Choose State:
<select name="State">
<option value="choose"> choose one</option>
<option value="State_1" >State 1</option>
<option value="State_2" >State 2</option>
<option value="State_3" >State 3</option>
</select><br><br>
Now Select City:
<select name="City">
<option value="choose"> <select previous first</option>
<option value="choose"> select City for State_1</option>
<option value="City_A__ State_1" >City A of State 1</option>
<option value="City_B__ State_1" >City B of State 1</option>
<option value="City_C__ State_1" >City C of State 1</option>
<option value="City_D__ State_1" >City D of State 1</option>
<option value="City_E__ State_1" >City E of State 1</option>
<option value="City_F__ State_1" >City F of State 1</option>
<option value="choose"> select City for State_2</option>
<option value="City_A__ State_2" >City A of State 2</option>
<option value="City_B__ State_2" >City B of State 2</option>
<option value="choose"> select City for State_3</option>
<option value="City_A__ State_3" >City A of State 3</option>
<option value="City_B__ State_3" >City B of State 3</option>
<option value="City_C__ State_3" >City C of State 3</option>
<option value="City_D__ State_3" >City D of State 3</option>
</select>
</form>
</body>
</html>
[/code]
this above code - is done with index.. i can't list all of mysql table values !!
let me tell you what i would like to do:
in the options of first list their should be this code:
this code will show fields available in the mysql table
[php]
<?
$sql = "SHOW FIELDS FROM myTable";
$result = mysql_query($sq l);
while($row = mysql_fetch_row ($result))
{?>
<option value="<?echo $row[0]?>"><?echo $row[0]?></option>
<?}
?>
[/php]
NOW .. to show values of ONE field (second list)
i use this code:
[php]
<?
$sql = "Select filed2 FROM myTable GROUP BY filed2";
$result = mysql_query($sq l);
while($row = mysql_fetch_row ($result))
{?>
<option value="<?echo $row[0]?>"><?echo $row[0]?></option>
<?}
?>
[/php]
In the above code, what i want is to have field2 be different depending on what user has selected in the first list
is there something like !! .. if user select item in the first list - take this item and assign it to field ?? ( so I'll use in my above code $field instead of field2 ) !!!
please help me.. I feel i'm almost there finding the right solution for this problem.. but need some help from you guys :)
P.S.1. I don't want to submit twice in the form i.e. i don't wonna sumbit first list so second list should be assigned.. i've tried that and made some other problems in my form
P.S.2. I don't want to use AJAX - .. what i need is php/html/javascript
Thanks in advance :)
I'm now trying to have a dependent lists in a form
my form is a query based form
i.e. I fill my MySQL query from this form
I have around 30 fields/columns
each have number of values... sometimes 2 , sometimes 20.. maybe more !
I've searched the web to find a javascript code that helps me to do the dependent lists
I found few funtions.. but their work depends on INDEX !
they're very powerful functions.. i liked them.. but didn't know how to modify them until i get what i'm looking for
This is what I've found ( in two diff websites) - it's working if you copied it and pasted then tried it !
[code=html]
<html>
<head>
<script type="text/javascript">
function DepSelectOne(ma sterSelect){
var frm = masterSelect.fo rm;
this.selectedBl ock = 0;
this.selBoxes = new Array(masterSel ect);
if((frm)&&(type of Option != 'undefined')){
for(var c = 1;c < arguments.lengt h;c++){
var selObj = frm[arguments[c]];
if((selObj)&&(s elObj.type.inde xOf('select') == 0)&&(selObj.typ e.indexOf('mult ') < 0)&&(selObj.opt ions.length > 2)){
this.selBoxes[this.selBoxes.l ength] = frm[arguments[c]];
}
}
if((this.selBox es.length >1)&&(this.selB oxes.length == arguments.lengt h)){
this.selectElem ent = this.selBoxes[0];
DepSelectOne.in st[this.index = this.selectElem ent.name] = this; //keep global record of this object instance.
this.selBoxes[0] = this;
this.optionBloc k = new OptionBlock(0, this.selectElem ent, this.selectElem ent.options[0].value, this, 0);
this.next = new DepSelectOneNex t(this.selBoxes , 1);
this.next.setSe lection(this.op tionBlock.getSe lection());
this.selectElem ent.onchange = new Function('DepSe lectOne.inst[\''+this.index+ '\'].handleChange(0 )');
}else{
//alert('DepSelec tOne constructor could not find\nsufficien t properly configured SELECT elements\nto initialise.');
}
}
}
DepSelectOne.in st = {}; //Globally accessible object to hold instances of the DepSelectOne Class.
//call chain terminating functions.
DepSelectOne.pr ototype.validat eNext = DepSelectOne.pr ototype.reset = DepSelectOne.pr ototype.setSele ction = DepSelectOne.pr ototype.setSele ctedFromChild = function(){retu rn true;}
DepSelectOne.pr ototype.validat e = function(){
return ((this.optionBl ock.lastSelecti on < 0)?false:this.n ext.validateNex t());
}
DepSelectOne.pr ototype.init = function(select edBlock){
if(selectedBloc k >= 0){
this.optionBloc k.lastSelection = selectedBlock;
}
if(this.optionB lock.lastSelect ion >= 0){
this.optionBloc k.doElement();
if(window.setTi meout)setTimeou t('DepSelectOne .inst[\''+this.index+ '\'].refresh();', 5); //fix for Opera 7 timing problem. Harmless otherwise.
}
}
DepSelectOne.pr ototype.handleC hange = function(ind){
this.selBoxes[ind].changed();
if(window.setTi meout)setTimeou t('DepSelectOne .inst[\''+this.index+ '\'].refresh();', 5); //fix for Opera 7 timing problem. Harmless otherwise.
}
DepSelectOne.pr ototype.refresh = function(){
this.selectElem ent.selectedInd ex = this.optionBloc k.lastSelection ;
this.next.reset ();
}
DepSelectOne.pr ototype.changed = function(){
this.optionBloc k.readState();
this.next.setSe lection(this.op tionBlock.getSe lection());
}
function DepSelectOneNex t(selBoxes, selBoxIndex){
this.selectedBl ock = -1;
this.classObj = selBoxes[0];
this.previous = selBoxes[(selBoxIndex-1)]
this.selectElem ent = selBoxes[selBoxIndex];
selBoxes[selBoxIndex] = this;
this.selectElem ent.onchange = new Function('DepSe lectOne.inst[\''+this.classO bj.index+'\'].handleChange(' +selBoxIndex+') ');
this.seperatorO ption = new DepOption(this. selectElement.o ptions[0]);
this.optionBloc ks = [];
var startIndex = 1;
while(startInde x < this.selectElem ent.options.len gth){
var nextIndex = this.optionBloc ks.length;
this.optionBloc ks[nextIndex] = new OptionBlock(sta rtIndex, this.selectElem ent, this.selectElem ent.options[0].value, this, nextIndex);
startIndex += this.optionBloc ks[nextIndex].getOptionsTota l();
}
for(var cnt = 0,c = 0;c < this.optionBloc ks.length;c++){
this.optionBloc ks[c].blockStart = cnt;
cnt += this.optionBloc ks[c].getItems();
}
if(++selBoxInde x < selBoxes.length ){
this.next = new DepSelectOneNex t(selBoxes, selBoxIndex);
}else{
this.next = this.classObj;
this.previous.i nit(this.select edBlock);
}
}
DepSelectOneNex t.prototype.res et = function(){
if((this.select edBlock >= 0)&&(this.optio nBlocks[this.selectedBl ock].lastSelection >= 0)){
this.selectElem ent.selectedInd ex = this.optionBloc ks[this.selectedBl ock].lastSelection;
this.next.reset ();
}
}
DepSelectOneNex t.prototype.set Selection = function(selBlo ck){
this.selectedBl ock = selBlock;
if(this.selecte dBlock < 0){
var opt = this.selectElem ent.options;
opt.length = 0; //clear options
opt[0] = this.seperatorO ption.getOption ();
this.selectElem ent.selectedInd ex = 0;
this.next.setSe lection(-1);
}else{
var blk = this.optionBloc ks[this.selectedBl ock];
blk.doElement() ;
this.next.setSe lection(blk.get Selection());
}
}
DepSelectOneNex t.prototype.cha nged = function(){
if(this.selecte dBlock >= 0){
var blk = this.optionBloc ks[this.selectedBl ock];
blk.readState() ;
this.next.setSe lection(blk.get Selection());
}
}
DepSelectOneNex t.prototype.set SelectedFromChi ld = function(blockI ndex){
if(this.selecte dBlock < 0){ //only set from OptonBlock on the first attempt.
this.selectedBl ock = blockIndex;
}
}
DepSelectOneNex t.prototype.ini t = function(selBlo ck){
if(selBlock >= 0){
var blockIndex = 0;
while(!this.opt ionBlocks[blockIndex++].nextBlockTest( selBlock));
}
this.previous.i nit(this.select edBlock);
}
DepSelectOneNex t.prototype.val idateNext = function(){
return (((this.selecte dBlock < 0)||(this.optio nBlocks[this.selectedBl ock].lastSelection < 0))?false:this. next.validateNe xt());
}
function DepOption(optEl ){
this.value = optEl.value;
this.text = optEl.text;
}
DepOption.proto type.getOption = function(){
return new Option(this.tex t, this.value);
}
function OptionBlock(sta rtIndex, selectElement, seperatorValue, owner, blockIndex){
this.blockStart = 0;
this.owner = owner;
this.index = blockIndex;
this.selectElem ent = selectElement;
this.seperators = [];
this.optionBloc k = [];
this.lastSelect ion = -1;
var opts = this.selectElem ent.options;
while((startInd ex < opts.length)&&( opts[startIndex].value == seperatorValue) ){
this.seperators[this.seperators .length] = new DepOption(opts[startIndex++]);
}
while((startInd ex < opts.length)&&( opts[startIndex].value != seperatorValue) ){
if((this.lastSe lection < 0)&&(opts[startIndex].selected == true)){
this.lastSelect ion = this.optionBloc k.length; //record first item selected, if any.
}
this.optionBloc k[this.optionBloc k.length] = new DepOption(opts[startIndex++]);
}
if(this.lastSel ection >= 0)this.owner.se tSelectedFromCh ild(this.index)
}
OptionBlock.pro totype.getOptio nsTotal = function(){
return (this.seperator s.length + this.optionBloc k.length);
}
OptionBlock.pro totype.getItems = function(){
return this.optionBloc k.length;
}
OptionBlock.pro totype.nextBloc kTest = function(selBlo ck){
if((selBlock >= this.blockStart )&&(selBlock < this.blockStart +this.optionBlo ck.length)){
this.lastSelect ion = selBlock - this.blockStart ;
this.owner.sele ctedBlock = this.index;
return true;
}
return false;
}
OptionBlock.pro totype.getSelec tion = function(){
return ((this.lastSele ction < 0)?-1:(this.blockSt art+this.lastSe lection));
}
OptionBlock.pro totype.doElemen t = function(){
var opt = this.selectElem ent.options;
opt.length = 0; //clear options
if((this.lastSe lection < 0)&&(this.seper ators.length > 0)){
opt[opt.length] = this.seperators[(this.seperator s.length-1)].getOption();
}
for(var c = 0;c < this.optionBloc k.length;c++){
opt[opt.length] = this.optionBloc k[c].getOption();
}
this.selectElem ent.selectedInd ex = ((this.lastSele ction < 0)?0:this.lastS election);
}
OptionBlock.pro totype.readStat e = function(){
if(this.lastSel ection < 0){
this.lastSelect ion = (this.selectEle ment.selectedIn dex - 1);
this.doElement( );
}else{
this.lastSelect ion = this.selectElem ent.selectedInd ex;
}
}
</script>
</head>
<body onload="new DepSelectOne(do cument.forms['StateCityForm'].elements['State'], 'City');">
<form name="StateCity Form" ACTION="" METHOD="GET" TARGET="_blank" >
First Choose State:
<select name="State">
<option value="choose"> choose one</option>
<option value="State_1" >State 1</option>
<option value="State_2" >State 2</option>
<option value="State_3" >State 3</option>
</select><br><br>
Now Select City:
<select name="City">
<option value="choose"> <select previous first</option>
<option value="choose"> select City for State_1</option>
<option value="City_A__ State_1" >City A of State 1</option>
<option value="City_B__ State_1" >City B of State 1</option>
<option value="City_C__ State_1" >City C of State 1</option>
<option value="City_D__ State_1" >City D of State 1</option>
<option value="City_E__ State_1" >City E of State 1</option>
<option value="City_F__ State_1" >City F of State 1</option>
<option value="choose"> select City for State_2</option>
<option value="City_A__ State_2" >City A of State 2</option>
<option value="City_B__ State_2" >City B of State 2</option>
<option value="choose"> select City for State_3</option>
<option value="City_A__ State_3" >City A of State 3</option>
<option value="City_B__ State_3" >City B of State 3</option>
<option value="City_C__ State_3" >City C of State 3</option>
<option value="City_D__ State_3" >City D of State 3</option>
</select>
</form>
</body>
</html>
[/code]
this above code - is done with index.. i can't list all of mysql table values !!
let me tell you what i would like to do:
in the options of first list their should be this code:
this code will show fields available in the mysql table
[php]
<?
$sql = "SHOW FIELDS FROM myTable";
$result = mysql_query($sq l);
while($row = mysql_fetch_row ($result))
{?>
<option value="<?echo $row[0]?>"><?echo $row[0]?></option>
<?}
?>
[/php]
NOW .. to show values of ONE field (second list)
i use this code:
[php]
<?
$sql = "Select filed2 FROM myTable GROUP BY filed2";
$result = mysql_query($sq l);
while($row = mysql_fetch_row ($result))
{?>
<option value="<?echo $row[0]?>"><?echo $row[0]?></option>
<?}
?>
[/php]
In the above code, what i want is to have field2 be different depending on what user has selected in the first list
is there something like !! .. if user select item in the first list - take this item and assign it to field ?? ( so I'll use in my above code $field instead of field2 ) !!!
please help me.. I feel i'm almost there finding the right solution for this problem.. but need some help from you guys :)
P.S.1. I don't want to submit twice in the form i.e. i don't wonna sumbit first list so second list should be assigned.. i've tried that and made some other problems in my form
P.S.2. I don't want to use AJAX - .. what i need is php/html/javascript
Thanks in advance :)
Comment