Onchange with javascript

Ok I have this php file that I use to call objects from my database when they select an option from the select list. My problem that I am having is when I go to select the item from the list it resets the entire list and does not remember my selection. Is there something that I am missing to make this work? I have attached the code to help.
<html>
	<head>
		<style type="text/css">
			body { color:black; background:white; font-size:18px; }
			table { margin:2px; background:#dddddd; color:black; border-color:gray;}
			caption { font-size:20px; background:gray;}
			th { font-size:14px; background:silver; border:2px solid #0000aa;}
			td { font-size:12px; padding:1px; border-color:#0000aa;}
			hr { border: 1px dashed gray; }
			a { color:yellow; }
			input {height:18px; font-size:11px;}
		</style> 
		<title>Test </title>
		<?php
			error_reporting(0);
			$user = $HTTP_GET_VARS['user'];
			$pass = $HTTP_GET_VARS['pass'];
			$uuser = str_replace(' ','%20',$user);
			$ppass = str_replace(' ','%20',$pass);
			$terrain = $_POST['terrain'];

			$connection = mysql_connect("$dbhost", "$dbuser", "$dbpass") or die("Could not connect... ");
			mysql_select_db("$database");
		?>
	</head>
	<body text=white>
		<center>
			<font size=4><b>TEst</b></font>
			<font color=red></font>
			<font color=green></font>
		</center>
		<form action=stuff.php method=post><input type=hidden name=start value=0>
			<table border=0 cellspacing=1 cellpadding=2 align=center>
				<?php	
					$terrain = $_POST['terrain'];
					$t = str_replace(' ','%20',$terrain);
					$list = "<tr>
						<th colspan=2><a href=stuff.php> Refresh Page </a></th>
						<th colspan=5><b> Select Terrain </b>
						<select name='terrain' onchange=javascript:document.forms[0].submit();>
							<option selected>Terrain</option>
							<option value=a > Forest (a) </option>
							<option value=b > Deep Forest (b) </option>
							<option value=c > Plains (c) </option>
							<option value=d > Dunes (d) </option>
							<option value=e > Marshes (e) </option>
							<option value=f > Cliffs (f) </option>
							<option value=g > Coastline (g) </option>
							<option value=h > Ocean (h) </option>
							<option value=i > Mountains (i) </option>
							<option value=j > Summit (j) </option>
							<option value=k > Volcano (k) </option>
							<option value=l > Magma (l) </option>
							<option value=m > Courtyard (m) </option>
							<option value=n > Cemetary (n) </option>
							<option value=o > Jail (o) </option>
							<option value=p > Cave (p) </option>
							<option value=q > Savanna (q) </option>
							<option value=r > Subarctic Forest (r) </option>
							<option value=s > Deep Ocean (s) </option>
						</select>
						</th>
						</tr>
						<tr>";
					echo $list;
					$terrain = $_POST['terrain'];
					$placenames = array(
      			'a' => 'Forest', 'b' => 'Deep Forest', 'c' => 'Plains', 'd' => 'Dunes',
      			'e' => 'Marshes', 'f' => 'Cliffs', 'g' => 'Coastline', 'h' => 'Ocean',
      			'i' => 'Mountains', 'j' => 'Summit', 'k' => 'Volcano', 'l' => 'Magma',
     				'm' => 'Courtyard', 'n' => 'Cemetary', 'o' => 'Jail', 'p' => 'Cave',
     				'q' => 'Savanna', 'r' => 'Subarctic Forest', 's' => 'Deep Ocean');
    			$place = $placenames[$terrain];
    			$rm = mysql_query("SELECT * FROM m WHERE place = BINARY '$terrain'");
    			$numof = mysql_num_rows($rm);
					$table = "<th colspan=10>Select a M from <font color=blue>$place</font> Start=0, showing $numof of $numof </th></tr>";
					$table .= "<tr><td colspan=10 align=center><table border=1  style=border-collapse:collapse;background-color:bbddff>";
					$table .= "<tr><td>id</td><td><b>N</b></td><td><b>L</b></td><td align=right><b>E</b></td><td align=right><b>M</b></td>";
					$table .= "<td><b>M</b></td><td align=right><b>H</b></td><td align=right><b>M</b></td><td align=right><b> D</b></td><td align=right><b> D</b></td><td><b>MA</b></td>";
					$table .= "<td align=right><b>A</b></td><td align=right><b>V</b></td><td align=right><b>P</b></td><td align=right><b>S</b></td></tr>";
					while($m = mysql_fetch_array($rm)) {
    				$table .= "<tr onclick=selectM('$m[0]'); name=rows style=text-align:right>";
    				$table .= "<td>$m[0]</td>"; //ID
    				$table .= "<td align=left><nobr>$m[1]</nobr></td>";
    				$table .= "<td>$m[3]";
    				$table .= "<td>$m[2]"; 
    				$table .= "<td>$m[4]"; 
    				$table .= "<td>$m[27]"; 
    				$table .= "<td>$m[5]"; 
    				$table .= "<td>$m[6]"; 
    				$table .= "<td>$m[7]"; 
    				$table .= "<td>$m[8]"; 
    				$table .= "<td>$m[9]"; 
    				$table .= "<td>$m[10]";
    				$table .= "<td>$m[11]"; 
    				$table .= "<td>$m[12]"; 
    				$table .= "<td>$m[13]</td>"; 
    				$table .= "<td style='display:none'>$m[30]</td>";
    				$table .= "<td style='display:none'>$m[14]</td>";
    				$table .= "<td style='display:none'>$m[16]</td>";
    				$table .= "<td style='display:none'>$m[17]</td>";
    				$table .= "<td style='display:none'>$m[18]</td>";
    				$table .= "<td style='display:none'>$m[19]</td>";
    				$table .= "<td style='display:none'>$m[20]</td>";
    				$table .= "<td style='display:none'>$m[21]</td>";
    				$table .= "<td style='display:none'>$m[22]</td>";
    				$table .= "<td style='display:none'>$m[15]</td>";
    				$table .= "<td style='display:none'>$m[23]</td>";
    				$table .= "<td style='display:none'>$m[24]</td>";
    				$table .= "<td style='display:none'>$m[33]</td>";
    				$table .= "<td style='display:none'>$m[25]</td>";
    				$table .= "<td style='display:none'>$m[26]</td>";
    				$table .= "<td style='display:none'>$m[28]</td>";
    				$table .= "<td style='display:none'>$m[29]</td>"; 
    				$table .= "<td style='display:none'>$m[32]</td>"; 
			  		$table .= "</tr>";
					}
						$table .= "</td></tr></table></from>";
		echo $table;
		mysql_close($connection);
		?>
		<script>
			function clearForm() {
				var st = document.getElementsByTagName('input');
				for (var n=1;n < st.length-4; n++)	// hidden id field = 1
				st[n].value = '';
			}
			function modify(i) {
			}
			function selectM(id){
				document.getElementById('id_out').innerHTML = id;
				var st = document.getElementsByTagName('input');
				var rows = document.getElementsByName('rows')
				var l = rows.length;
				var oldv,newv;
				// alert(l);
				for (var z = 0; z < l; z++) {
					var Anzahl = rows[z].childNodes.length;
					var Erster = rows[z].childNodes[0].innerHTML;
					if (id == Erster){
						rows[z].style.backgroundColor='yellow';
						for (d = 0; d < Anzahl; d++){
							oldv = rows[z].childNodes[d].innerHTML;
							newv = oldv.replace(/<nobr>/g, '');
							newv = newv.replace(/<\/nobr>/g, '');
							st[d+1].value = newv;
						}
					}
					else rows[z].style.backgroundColor='bbddff';
				}
			}
		</script>
	</body>
</html>

Open in new window

LVL 1
benner6807Asked:
Who is Participating?
 
jaffogConnect With a Mentor Commented:
Something like this?
<html>
	<head>
		<style type="text/css">
			body { color:black; background:white; font-size:18px; }
			table { margin:2px; background:#dddddd; color:black; border-color:gray;}
			caption { font-size:20px; background:gray;}
			th { font-size:14px; background:silver; border:2px solid #0000aa;}
			td { font-size:12px; padding:1px; border-color:#0000aa;}
			hr { border: 1px dashed gray; }
			a { color:yellow; }
			input {height:18px; font-size:11px;}
		</style> 
		<title>Test </title>
		<?php
			error_reporting(0);
			$user = $HTTP_GET_VARS['user'];
			$pass = $HTTP_GET_VARS['pass'];
			$uuser = str_replace(' ','%20',$user);
			$ppass = str_replace(' ','%20',$pass);
			$terrain = $_POST['terrain'];

			$connection = mysql_connect("$dbhost", "$dbuser", "$dbpass") or die("Could not connect... ");
			mysql_select_db("$database");
		?>
	</head>
	<body text=white>
		<center>
			<font size=4><b>TEst</b></font>
			<font color=red></font>
			<font color=green></font>
		</center>
		<form action=stuff.php method=post><input type=hidden name=start value=0>
			<table border=0 cellspacing=1 cellpadding=2 align=center>
				<?php	
					$terrain = $_POST['terrain'];
					$t = str_replace(' ','%20',$terrain);
					$list = "<tr>
						<th colspan=2><a href=stuff.php> Refresh Page </a></th>
						<th colspan=5><b> Select Terrain </b>
						<select name='terrain' onchange='javascript:document.forms[0].submit();'>
							<option selected>Terrain</option>
							<option value='a'".(($terrain=='a')?' selected="selected"':'')."> Forest (a) </option>
							<option value='b'".(($terrain=='b')?' selected="selected"':'')."> Deep Forest (b) </option>
							<option value='c'".(($terrain=='c')?' selected="selected"':'')."> Plains (c) </option>
							<option value='d'".(($terrain=='d')?' selected="selected"':'')."> Dunes (d) </option>
							<option value='e'".(($terrain=='e')?' selected="selected"':'')."> Marshes (e) </option>
							<option value='f'".(($terrain=='f')?' selected="selected"':'')."> Cliffs (f) </option>
							<option value='g'".(($terrain=='g')?' selected="selected"':'')."> Coastline (g) </option>
							<option value='h'".(($terrain=='h')?' selected="selected"':'')."> Ocean (h) </option>
							<option value='i'".(($terrain=='i')?' selected="selected"':'')."> Mountains (i) </option>
							<option value='j'".(($terrain=='j')?' selected="selected"':'')."> Summit (j) </option>
							<option value='k'".(($terrain=='k')?' selected="selected"':'')."> Volcano (k) </option>
							<option value='l'".(($terrain=='l')?' selected="selected"':'')."> Magma (l) </option>
							<option value='m'".(($terrain=='m')?' selected="selected"':'')."> Courtyard (m) </option>
							<option value='n'".(($terrain=='n')?' selected="selected"':'')."> Cemetary (n) </option>
							<option value='o'".(($terrain=='o')?' selected="selected"':'')."> Jail (o) </option>
							<option value='p'".(($terrain=='p')?' selected="selected"':'')."> Cave (p) </option>
							<option value='q'".(($terrain=='q')?' selected="selected"':'')."> Savanna (q) </option>
							<option value='r'".(($terrain=='r')?' selected="selected"':'')."> Subarctic Forest (r) </option>
							<option value='s'".(($terrain=='s')?' selected="selected"':'')."> Deep Ocean (s) </option>
						</select>
						</th>
						</tr>
						<tr>";
					echo $list;
					$terrain = $_POST['terrain'];
					$placenames = array(
      			'a' => 'Forest', 'b' => 'Deep Forest', 'c' => 'Plains', 'd' => 'Dunes',
      			'e' => 'Marshes', 'f' => 'Cliffs', 'g' => 'Coastline', 'h' => 'Ocean',
      			'i' => 'Mountains', 'j' => 'Summit', 'k' => 'Volcano', 'l' => 'Magma',
     				'm' => 'Courtyard', 'n' => 'Cemetary', 'o' => 'Jail', 'p' => 'Cave',
     				'q' => 'Savanna', 'r' => 'Subarctic Forest', 's' => 'Deep Ocean');
    			$place = $placenames[$terrain];
    			$rm = mysql_query("SELECT * FROM m WHERE place = BINARY '$terrain'");
    			$numof = mysql_num_rows($rm);
					$table = "<th colspan=10>Select a M from <font color=blue>$place</font> Start=0, showing $numof of $numof </th></tr>";
					$table .= "<tr><td colspan=10 align=center><table border=1  style=border-collapse:collapse;background-color:bbddff>";
					$table .= "<tr><td>id</td><td><b>N</b></td><td><b>L</b></td><td align=right><b>E</b></td><td align=right><b>M</b></td>";
					$table .= "<td><b>M</b></td><td align=right><b>H</b></td><td align=right><b>M</b></td><td align=right><b> D</b></td><td align=right><b> D</b></td><td><b>MA</b></td>";
					$table .= "<td align=right><b>A</b></td><td align=right><b>V</b></td><td align=right><b>P</b></td><td align=right><b>S</b></td></tr>";
					while($m = mysql_fetch_array($rm)) {
    				$table .= "<tr onclick=selectM('$m[0]'); name=rows style=text-align:right>";
    				$table .= "<td>$m[0]</td>"; //ID
    				$table .= "<td align=left><nobr>$m[1]</nobr></td>";
    				$table .= "<td>$m[3]";
    				$table .= "<td>$m[2]"; 
    				$table .= "<td>$m[4]"; 
    				$table .= "<td>$m[27]"; 
    				$table .= "<td>$m[5]"; 
    				$table .= "<td>$m[6]"; 
    				$table .= "<td>$m[7]"; 
    				$table .= "<td>$m[8]"; 
    				$table .= "<td>$m[9]"; 
    				$table .= "<td>$m[10]";
    				$table .= "<td>$m[11]"; 
    				$table .= "<td>$m[12]"; 
    				$table .= "<td>$m[13]</td>"; 
    				$table .= "<td style='display:none'>$m[30]</td>";
    				$table .= "<td style='display:none'>$m[14]</td>";
    				$table .= "<td style='display:none'>$m[16]</td>";
    				$table .= "<td style='display:none'>$m[17]</td>";
    				$table .= "<td style='display:none'>$m[18]</td>";
    				$table .= "<td style='display:none'>$m[19]</td>";
    				$table .= "<td style='display:none'>$m[20]</td>";
    				$table .= "<td style='display:none'>$m[21]</td>";
    				$table .= "<td style='display:none'>$m[22]</td>";
    				$table .= "<td style='display:none'>$m[15]</td>";
    				$table .= "<td style='display:none'>$m[23]</td>";
    				$table .= "<td style='display:none'>$m[24]</td>";
    				$table .= "<td style='display:none'>$m[33]</td>";
    				$table .= "<td style='display:none'>$m[25]</td>";
    				$table .= "<td style='display:none'>$m[26]</td>";
    				$table .= "<td style='display:none'>$m[28]</td>";
    				$table .= "<td style='display:none'>$m[29]</td>"; 
    				$table .= "<td style='display:none'>$m[32]</td>"; 
			  		$table .= "</tr>";
					}
						$table .= "</td></tr></table></from>";
		echo $table;
		mysql_close($connection);
		?>
		<script>
			function clearForm() {
				var st = document.getElementsByTagName('input');
				for (var n=1;n < st.length-4; n++)	// hidden id field = 1
				st[n].value = '';
			}
			function modify(i) {
			}
			function selectM(id){
				document.getElementById('id_out').innerHTML = id;
				var st = document.getElementsByTagName('input');
				var rows = document.getElementsByName('rows')
				var l = rows.length;
				var oldv,newv;
				// alert(l);
				for (var z = 0; z < l; z++) {
					var Anzahl = rows[z].childNodes.length;
					var Erster = rows[z].childNodes[0].innerHTML;
					if (id == Erster){
						rows[z].style.backgroundColor='yellow';
						for (d = 0; d < Anzahl; d++){
							oldv = rows[z].childNodes[d].innerHTML;
							newv = oldv.replace(/<nobr>/g, '');
							newv = newv.replace(/<\/nobr>/g, '');
							st[d+1].value = newv;
						}
					}
					else rows[z].style.backgroundColor='bbddff';
				}
			}
		</script>
	</body>
</html>

Open in new window

0
 
COBOLdinosaurCommented:
The select has an onChange that submits the form.  That version of the page is gone.  when the server responds to the client it is a new page and it has no memory of the previous values unless they are sent back from the server. so if you want to preseve information between the selects that cause the submits your have to maintain the list on the server side and send it back as part of the page.
0
 
jaffogCommented:
What I forgot to add is that you have to "tell" the SELECT element what option should be selected, based on the value you got from that page. Since the SELECT element is hard coded, it may appear a bit messy. Like COBOLdinosaur mentioned, if you maintain that list server side, then it will be more easy to maintain the output and generation of the appropriate form element as well.
0
 
benner6807Author Commented:
Thank jaffog that worked perfectly.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.