Page 1 of 1

Combo box loosing the sript effect

Posted: Tue Sep 11, 2007 9:59 pm
by wasir
I am using linked combo boxes connected to DB, which shows the result in a new page. It works all good.

But when user clicks on BACK button of the browser, the combo boxes loose the javascript control. Box 1 maintains the selection BUT box 2 goes back to default (on load) settings. To get this back in control, either the page needs to be refreshed or box 1 selection needs to be changed. I prefer not to open the result in new window.

Is there a way to get this javascript working...????

I am using...

Code: Select all

<?php
include ('functions.php5');
echo getHeader();

if (!isset($passed_value)){ $passed_value=1;}
if (!isset($passed_value2)){ $passed_value2=1;}

?>

<body onload="redirect(<?php echo $passed_value.','.$passed_value2?>,2)">

<div id="content">
	<div id="banner">
	<h1>strata corporate services</h1>
	</div>
	<div id="nav">
	<p>directory</p>
	</div>
	<div id="flash">
	<img src="images/img_crown.jpg" width="204" height="58" /><img src="images/temp.jpg" width="530" height="58" class="temp1" />
	</div>
		<div id="result1">
		<div id="bar1">
		<p>easy find</p>
		</div>
		<div id="easyfind">
			<?php 
				include('con.php');
				$sql="select * from categories";
				$sql2="select * from subcategories WHERE categ=".$passed_value;
				$result=mysql_query($sql,$id_link) or die (mysql_error());
				$result2=mysql_query($sql2,$id_link) or die (mysql_error());
			?>
			<form name="doublecombo" action="listing.php5" method="post">
			<div id="division1"> 
			1.
				<div id="division11">
					<label for="example">Select the CATEGORY here:</label>
					<select id="example" name="example" size="1"  onChange="redirect(this.options.selectedIndex,<?php echo $passed_value.','.$passed_value2?>)">
					<option value="maincateg">select all --></option>
					<?php
						while ($newarray=mysql_fetch_array($result)) {
					    	echo '<option value="'.$newarray['sno'].'" ';
						 	if ($newarray['sno']==$passed_value) {echo "SELECTED";};
						 	echo '>'.$newarray['categories'].'</option>'.'\n';
					  	} 
					?> 
					</select>
				</div>
			</div>
			<div id="division1">
			2.
				<div id="division11">
					<label for="stage2">Select the SUB-CATEGORY here:</label>
					<select id="stage2" name="stage2" size="1">
					<?php 
						while ($newarray=mysql_fetch_array($result2)) {	
					    	echo '<option value="'.$newarray['sno'].'" ';
						 	if ($newarray['sno']==$passed_value2) {echo "SELECTED";};
						 	echo '>'.$newarray['scategories'].'</option>';
					     	$x=$x+1;
						} 
					?>
					</select>
				</div>
			</div>
			<div id="division3">
			3.
				<div id="division13">
					<input type="submit" name="getit" value="Submit" />
				</div>
			</div>
						
			<script>
			<!--
			var groups=document.doublecombo.example.options.length
			var group=new Array(groups)
			for (i=0; i<groups; i++){
			group[i]=new Array
			
			}
			<?php 
			//Here comes the data from the two linked tables in a format suited for JScripting
			// into the script mentioned here above
			
			$sql="select * from categories";
			
			$result=mysql_query($sql,$id_link) or die (mysql_error());
			$x=0;$y=0;$m=0;$z=1;
			
			 while ($newarray=mysql_fetch_array($result)) { 
				echo "group[0][$m]=new Option(\"".$newarray['categories'].'","'.$newarray['sno'].'")'."\n";
				$sql2="select * from subcategories WHERE categ=".$newarray['sno']." order by scategories asc";
				$result2=mysql_query($sql2,$id_link) or die (mysql_error());
				
				while ($newarray2=mysql_fetch_array($result2)) {
					echo "group[$z][$y]=new Option(\"".$newarray2['scategories'].'","'.$newarray2['sno'].'")'."\n";
					$y=$y+1;
					
					}
				$z=$z+1;
				$y=0;
				$m=$m+1;
				}
			?>
			
			
			var temp=document.doublecombo.stage2
			function redirect(x,d,f){
			
			for (m=temp.options.length-1;m>0;m--)
			
			temp.options[m]=null
			
			for (i=0;i<group[x].length;i++){
			temp.options[i]=new Option(group[eval(x)][i].text,group[eval(x)][i].value)
			}
			temp.options[d].selected=true
			
			}
			
			//-->
			</script>

			</form>
		</div>
	</div>
<?php	
echo getFooter();
?>

Posted: Wed Sep 12, 2007 2:07 am
by Rovas
From your post I understand that you want to clear both boxes so check first that the passed values are set, unset them them then continue with the PHP script or put:

Code: Select all

var temp=null;

Posted: Wed Sep 12, 2007 9:17 pm
by wasir
I've added onbeforeunload and it's working fine now.

Code: Select all

<body onbeforeunload="" onload="redirect(<?php echo $passed_value.','.$passed_value2?>,2)">