Current location: Hot Scripts Forums » General Web Coding » JavaScript » need help with onChange events


need help with onChange events

Reply
  #1 (permalink)  
Old 09-04-06, 07:08 AM
nassau nassau is offline
Wannabe Coder
 
Join Date: May 2004
Posts: 211
Thanks: 0
Thanked 0 Times in 0 Posts
need help with onChange events

the following script allows the user to select fields in a database. the selection follows the database tree, ie database/table/field.

it all works as expected except for one issue. after selecting table, the fields are displayed but the table selection jumps back to the first option. this makes it impossible to select tables.

i'm a javascript novice so this might be real simple. i hope it is...

Code:
<?php

print_r($_POST);

?>


<html xmlns="http://www.w3.org/1999/xhtml">

<head></head>

<body>

<script type="text/javascript">

function setOptions(chosenDb, chosenTbl){

var selbox1 = document.formName.table;
selbox1.options.length = 0;

var selbox2 = document.formName.searchfield;
selbox2.options.length = 0;

var selbox3 = document.formName.searchorder;
selbox3.options.length = 0;

if (chosenDb == " ") {
	selbox1.options[selbox1.options.length] = new Option('No database selected',' ');
	
	selbox2.options[selbox2.options.length] = new Option('No database selected',' ');
	
	selbox3.options[selbox3.options.length] = new Option('No database selected',' ');
	}
if (chosenDb == "1") {
	selbox1.options[selbox1.options.length] = new Option('table one, choise one','1');
	selbox1.options[selbox1.options.length] = new Option('table one, choise two','2');
	selbox1.options[selbox1.options.length] = new Option('table one, choise three','3');
	selbox1.options[selbox1.options.length] = new Option('table one, choise four','4');
	selbox1.options[selbox1.options.length] = new Option('table one, choise five','5');
	
	selbox2.options[selbox2.options.length] = new Option('table one, choise one','oneone');
	selbox2.options[selbox2.options.length] = new Option('table one, choise two','onetwo');
	
	selbox3.options[selbox3.options.length] = new Option('table one, choise one','onetwo');
	selbox3.options[selbox3.options.length] = new Option('table one, choise two','onetwo3131e');
	
	
	if (chosenTbl == " ") {
		document.getElementById("checkbox1").innerHTML= "text not set";
		document.getElementById("checkbox2").innerHTML= "text not set";
		}
	if (chosenTbl == "1") {
		document.getElementById("checkbox1").innerHTML= "text 1";
		document.getElementById("checkbox2").innerHTML= "text 1";
		}
	if (chosenTbl == "2") {
		document.getElementById('checkbox1').innerHTML= '<input type="checkbox" name="chkbx1[field1]" value="korv1" />';
		document.getElementById("checkbox2").innerHTML= '<input type="checkbox" name="chkbx1[field2]" value="korv2" />';
		}
	if (chosenTbl == "3") {
		document.getElementById("checkbox1").innerHTML= "text 3";
		document.getElementById("checkbox2").innerHTML= "text 3";
		}
	if (chosenTbl == "4") {
		document.getElementById("checkbox1").innerHTML= "text 4";
		document.getElementById("checkbox2").innerHTML= "text 4";
		}
	if (chosenTbl == "5") {
		document.getElementById("checkbox1").innerHTML= "text 5";
		document.getElementById("checkbox2").innerHTML= "text 5";
		}
	}
if (chosenDb == "2") {
	selbox1.options[selbox1.options.length] = new Option('table two, choise one','1');
	selbox1.options[selbox1.options.length] = new Option('table two, choise two','2');
	selbox1.options[selbox1.options.length] = new Option('table two, choise three','3');
	selbox1.options[selbox1.options.length] = new Option('table two, choise four','4');
	
	selbox2.options[selbox2.options.length] = new Option('table two, choise one','twoone');
	selbox2.options[selbox2.options.length] = new Option('table two, choise two','twotwo');
	selbox2.options[selbox2.options.length] = new Option('table two, choise three','twothree');
	selbox2.options[selbox2.options.length] = new Option('table two, choise four','twofour');
	
	selbox3.options[selbox3.options.length] = new Option('table two, choise four','twofour');
	
	
	if (chosenTbl == " ") {
		document.getElementById("checkbox1").innerHTML= "text not set";
		document.getElementById("checkbox2").innerHTML= "text not set";
		}
	if (chosenTbl == "1") {
		document.getElementById("checkbox1").innerHTML= "text 1";
		document.getElementById("checkbox2").innerHTML= "text 1";
		}
	if (chosenTbl == "2") {
		document.getElementById('checkbox1').innerHTML= '<input type="checkbox" name="chkbx1[field1]" value="korv1" />';
		document.getElementById("checkbox2").innerHTML= '<input type="checkbox" name="chkbx1[field2]" value="korv2" />';
		}
	if (chosenTbl == "3") {
		document.getElementById("checkbox1").innerHTML= "text 3";
		document.getElementById("checkbox2").innerHTML= "text 3";
		}
	if (chosenTbl == "4") {
		document.getElementById("checkbox1").innerHTML= "text 4";
		document.getElementById("checkbox2").innerHTML= "text 4";
		}
	if (chosenTbl == "5") {
		document.getElementById("checkbox1").innerHTML= "text 5";
		document.getElementById("checkbox2").innerHTML= "text 5";
		}
	}
if (chosenDb == "3") {
	selbox1.options[selbox1.options.length] = new Option('third choice - option one','threeone');
	selbox1.options[selbox1.options.length] = new Option('third choice - option two','threetwo');
	
	selbox2.options[selbox2.options.length] = new Option('third choice - option one','threeone');
	selbox2.options[selbox2.options.length] = new Option('third choice - option two','threetwo');
	
	selbox3.options[selbox3.options.length] = new Option('third choice - option one','threeone');
	}


}


</script>


<form name="formName" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">

<select name="database" size="1" onchange="setOptions(document.formName.database.options[document.formName.database.selectedIndex].value, null);">
	<option value=" " selected="selected"></option>
	<option value="1">Database 1</option>
	<option value="2">Database 2</option>
	<option value="3">Database 3</option>
</select>
<br /><br />
<select name="table" size="1" onchange="setOptions(document.formName.database.options[document.formName.database.selectedIndex].value, document.formName.table.options[document.formName.table.selectedIndex].value);">
	<option value=" " >No database selected</option>
</select>
<br /><br />
<select name="searchfield" size="1">
	<option value=" " selected="selected">No database selected</option>
</select>
<br /><br />
<select name="searchorder" size="1">
	<option value=" " selected="selected">No database selected</option>
</select>
<br /><br />
<input type="submit" />
<div id="checkbox1">Select from the list to change this box</div>
<div id="checkbox2">Select from the list to change this box</div>

</form>


</body>

</html>
Reply With Quote
  #2 (permalink)  
Old 09-04-06, 10:13 AM
nassau nassau is offline
Wannabe Coder
 
Join Date: May 2004
Posts: 211
Thanks: 0
Thanked 0 Times in 0 Posts
solved..

solved...

Code:
<?php

print_r($_POST);

?>


<html xmlns="http://www.w3.org/1999/xhtml">

<head></head>

<body>

<script type="text/javascript">

function setOptions(chosen){

var selbox1 = document.formName.table;
selbox1.options.length = 0;

var selbox2 = document.formName.searchfield;
selbox2.options.length = 0;

var selbox3 = document.formName.searchorder;
selbox3.options.length = 0;

if (chosen == " ") {
	selbox1.options[selbox1.options.length] = new Option('No database selected',' ');
	
	selbox2.options[selbox2.options.length] = new Option('No database selected',' ');
	
	selbox3.options[selbox3.options.length] = new Option('No database selected',' ');
	}
if (chosen == "1") {
	selbox1.options[selbox1.options.length] = new Option(' ',' ');
	selbox1.options[selbox1.options.length] = new Option('first choice - option one','1');
	selbox1.options[selbox1.options.length] = new Option('first choice - option two','2');
	selbox1.options[selbox1.options.length] = new Option('first choice - option three','3');
	selbox1.options[selbox1.options.length] = new Option('first choice - option four','4');
	selbox1.options[selbox1.options.length] = new Option('first choice - option five','5');
	
	selbox2.options[selbox2.options.length] = new Option('first choice - option one','oneone');
	selbox2.options[selbox2.options.length] = new Option('first choice - option two','onetwo');
	
	selbox3.options[selbox3.options.length] = new Option('first choice - option two','onetwo');
	selbox3.options[selbox3.options.length] = new Option('first choice - option two1e12321','onetwo3131e');
	}
if (chosen == "2") {
	selbox1.options[selbox1.options.length] = new Option('second choice - option one','1');
	selbox1.options[selbox1.options.length] = new Option('second choice - option two','2');
	selbox1.options[selbox1.options.length] = new Option('second choice - option three','3');
	selbox1.options[selbox1.options.length] = new Option('second choice - option four','4');
	
	selbox2.options[selbox2.options.length] = new Option('second choice - option one','twoone');
	selbox2.options[selbox2.options.length] = new Option('second choice - option two','twotwo');
	selbox2.options[selbox2.options.length] = new Option('second choice - option three','twothree');
	selbox2.options[selbox2.options.length] = new Option('second choice - option four','twofour');
	
	selbox3.options[selbox3.options.length] = new Option('second choice - option four','twofour');
	}
if (chosen == "3") {
	selbox1.options[selbox1.options.length] = new Option('third choice - option one','threeone');
	selbox1.options[selbox1.options.length] = new Option('third choice - option two','threetwo');
	
	selbox2.options[selbox2.options.length] = new Option('third choice - option one','threeone');
	selbox2.options[selbox2.options.length] = new Option('third choice - option two','threetwo');
	
	selbox3.options[selbox3.options.length] = new Option('third choice - option one','threeone');
	}
}

function setCheckboxes(chosenDb, chosenTbl){

if (chosenDb == "1"){
	
	if (chosenTbl == " ") {
		document.getElementById("checkbox1").innerHTML= "1text not set";
		document.getElementById("checkbox2").innerHTML= "1text not set";
		}
	if (chosenTbl == "1") {
		document.getElementById('checkbox1').innerHTML= '1<input type="checkbox" name="chkbx1[field1]" value="korv1" />';
		document.getElementById("checkbox2").innerHTML= '1<input type="checkbox" name="chkbx1[field2]" value="korv2" />';
		}
	if (chosenTbl == "2") {
		document.getElementById("checkbox1").innerHTML= "1text 2";
		document.getElementById("checkbox2").innerHTML= "1text 2";
		}
	if (chosenTbl == "3") {
		document.getElementById("checkbox1").innerHTML= "1text 3";
		document.getElementById("checkbox2").innerHTML= "1text 3";
		}
	if (chosenTbl == "4") {
		document.getElementById("checkbox1").innerHTML= "1text 4";
		document.getElementById("checkbox2").innerHTML= "1text 4";
		}
	if (chosenTbl == "5") {
		document.getElementById("checkbox1").innerHTML= "1text 5";
		document.getElementById("checkbox2").innerHTML= "1text 5";
		}
	}

if (chosenDb == "2"){
	if (chosenTbl == "1") {
		document.getElementById('checkbox1').innerHTML= '2<input type="checkbox" name="chkbx1[field1]" value="korv1" />';
		document.getElementById("checkbox2").innerHTML= '2<input type="checkbox" name="chkbx1[field2]" value="korv2" />';
		}
	if (chosenTbl == "2") {
		document.getElementById("checkbox1").innerHTML= "2text 2";
		document.getElementById("checkbox2").innerHTML= "2text 2";
		}
	if (chosenTbl == "3") {
		document.getElementById("checkbox1").innerHTML= "2text 3";
		document.getElementById("checkbox2").innerHTML= "2text 3";
		}
	if (chosenTbl == "4") {
		document.getElementById("checkbox1").innerHTML= "2text 4";
		document.getElementById("checkbox2").innerHTML= "2text 4";
		}
	}
}

</script>


<form name="formName" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">

<select name="database" size="1" onchange="setOptions(document.formName.database.options[document.formName.database.selectedIndex].value);">
	<option value=" " selected="selected"></option>
	<option value="1">Database 1</option>
	<option value="2">Database 2</option>
	<option value="3">Database 3</option>
</select>
<br /><br />
<select name="table" size="1" onchange="setCheckboxes(document.formName.database.options[document.formName.database.selectedIndex].value, document.formName.table.options[document.formName.table.selectedIndex].value);">
	<option value=" " >No database selected</option>
</select>
<br /><br />
<select name="searchfield" size="1">
	<option value=" " selected="selected">No database selected</option>
</select>
<br /><br />
<select name="searchorder" size="1">
	<option value=" " selected="selected">No database selected</option>
</select>
<br /><br />
<input type="submit" />
<div id="checkbox1">Select from the list to change this box</div>
<div id="checkbox2">Select from the list to change this box</div>

</form>


</body>

</html>
Reply With Quote
  #3 (permalink)  
Old 09-04-06, 02:39 PM
TwoD TwoD is offline
Community VIP
 
Join Date: Sep 2003
Location: 404
Posts: 1,813
Thanks: 0
Thanked 0 Times in 0 Posts
You could make that code a whole lot shorter and easier to maintain by putting the different values in arrays, or even arrays within arrays to skip the chosen==smtng if-statements. Then use a loop to create the options.

Another tip, create a variable to hold the references to checkbox1 and checkbox2, like you do with the selectboxes, makes it a lot easier to change the names etc.
__________________
[W3Schools - learn all about the standards.] [QuirksMode - Browser Quirks] [MS's Online Reference Docs] [DOM in Gecko.]
Please pay attention to stickys, announcements and forum rules, thank you.
Please also remember Code Wrappers and [SOLVED] Marking, this helps everyone.
Reply With Quote
  #4 (permalink)  
Old 09-04-06, 03:53 PM
nassau nassau is offline
Wannabe Coder
 
Join Date: May 2004
Posts: 211
Thanks: 0
Thanked 0 Times in 0 Posts
yea, i wanted to do that but i don't really know the syntax. i'm a novice at javascript so i don't have a clue what i'm doing...

feel free to show me if you want!
Reply With Quote
  #5 (permalink)  
Old 09-07-06, 02:45 AM
nassau nassau is offline
Wannabe Coder
 
Join Date: May 2004
Posts: 211
Thanks: 0
Thanked 0 Times in 0 Posts
anyone want to show me how to save my data in variables? so i don't have to re-print it over and over.


thanks
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
error with onchange and GET method ebizbuys JavaScript 1 05-03-06 11:53 PM
Javascript onchange() ? darknite87 JavaScript 1 08-28-05 03:09 PM
php calendar events cigcode Job Offers & Assistance 0 07-01-04 01:57 PM
Simple script for events blasco Script Requests 2 12-21-03 10:31 AM
ONKEYUP and ONCHANGE events 770 JavaScript 0 10-27-03 10:47 AM


All times are GMT -5. The time now is 11:16 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.