[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Onchange with javascript

Posted on 2011-10-18
4
Medium Priority
?
317 Views
Last Modified: 2012-05-12
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

0
Comment
Question by:benner6807
  • 2
4 Comments
 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 36990991
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
 
LVL 3

Accepted Solution

by:
jaffog earned 2000 total points
ID: 36991398
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
 
LVL 3

Expert Comment

by:jaffog
ID: 36991411
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
 
LVL 1

Author Closing Comment

by:benner6807
ID: 36991851
Thank jaffog that worked perfectly.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Suggested Courses
Course of the Month18 days, 22 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question