dropdown selected value lost after reloading a page in js

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
payal8007
Forum Newbie
Posts: 1
Joined: Thu Jan 01, 2015 11:19 pm

dropdown selected value lost after reloading a page in js

Post 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>
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

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

Post 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.
Post Reply