Page 1 of 1

dropdown selected value lost after reloading a page in js

Posted: Fri Jan 02, 2015 12:50 am
by payal8007
dropdown selected value lost after reloading a page in javascript

Code: Select all

<?php

require 'config.php';  
?>

<!doctype html public "-//w3c//dtd html 3.2//en">

<html>

<head>
<SCRIPT language=JavaScript>
function reload(form)
{
var val=form.cat.options[form.cat.options.selectedIndex].value;
self.location='dd.php?cat=' + val ;

}


</script>
</head>

<body>
<?Php

@$cat=$_GET['cat']; 
if(strlen($cat) > 0 and !is_numeric($cat)){ 
echo "Data Error";
exit;
}




$quer2="SELECT maincat,id FROM category"; 



if(isset($cat) and strlen($cat) > 0){
$quer="SELECT subcat FROM category where pid=$cat"; 
}else{$quer="SELECT subcat FROM category"; } 


echo "<form method=post name=f1 action='#'>";

echo "<select name='cat' id='cat' onchange=\"reload(this.form)\" >";
echo "<option value=''>Select one</option>";

foreach ($dbo->query($quer2) as $noticia2) {
if($noticia2['cat_id']==@$cat){echo "<option selected value='$noticia2[id]'>$noticia2[maincat]</option>"."<BR>";}
else{echo  "<option value='$noticia2[id]'>$noticia2[maincat]</option>";}
}
echo "</select>";



echo "<select name='subcat'>";
foreach ($dbo->query($quer) as $noticia) {
echo  "<option value='$noticia[subcat]'>$noticia[subcat]</option>";
}
echo "</select>";


echo "<input type=submit value=Submit>";
echo "</form>";
?>
</body>

</html>

Re: dropdown selected value lost after reloading a page in j

Posted: Fri Jan 02, 2015 3:14 am
by requinix
Your query isn't returning a "cat_id" but you are trying to use it to determine whether the option should be selected.