I have problem in my page that the dropdown box is not displaying data according to selection of first dropdown.Please help me.
Below is my code.
thanks..............
Expand|Select|Wrap|Line Numbers
- <?php
- session_start();
- if (!isset($_SESSION['uname'])) {
- header('Location: index.php?msg=acc');
- exit;
- }
- ?>
- <html>
- <head>
- <title>Add Main Category</title>
- <link rel="stylesheet" href="css/header.css">
- <link rel="stylesheet" href="css/footer.css">
- <link rel="stylesheet" href="css/misc.css">
- <link rel="stylesheet" href="css/nav.css">
- </head>
- <body>
- <table border="0" height="100%" width="100%" cellpadding="0" cellspacing="0">
- <tr>
- <td colspan="2" height="8%"; valign="top">
- <?php include 'templates/header.inc'?>
- </td>
- </tr>
- <tr>
- <td width="10%" valign="top"><br>
- <?php include 'templates/adminmenu.php';?>
- </td>
- <td width="90%" valign="top"><br>
- <?php
- include 'classes/ez_sql.php';
- $page = $_REQUEST['page'];
- if(empty($page))
- $page = 'home';
- switch($page)
- {
- case 'home':
- showCountry();
- break;
- case 'add':
- addCountry();
- break;
- case 'edit':
- showEdit();
- break;
- case 'update':
- updateCountry();
- break;
- case 'del':
- delCountry();
- break;
- default:
- showCountry();
- }
- function showCountry()
- {
- ?>
- <script language="javascript">
- function checkForm()
- {
- var ccategory,cmaincat,csubcat;
- with(window.document.frmAdd)
- {
- ccategory = category;
- cmaincat = maincat;
- csubcat = subcat;
- }
- if(trim(cmaincat.value) == '')
- {
- alert('Main Category is required!');
- cmaincat.focus();
- return false;
- }
- else if(trim(ccategory.value) == '')
- {
- alert('Category is required!');
- ccategory.focus();
- return false;
- }
- else if(trim(ccsubcat.value) == '')
- {
- alert('Sub Category is required!');
- csubcat.focus();
- return false;
- }
- else
- {
- ccategory.value = trim(ccategory.value);
- return true;
- }
- }
- function trim(str)
- {
- return str.replace(/^\s+|\s+$/g,'');
- }
- </script>
- <script language="javascript">
- function sureToDelete(id)
- {
- if (confirm("Are you sure to delete this Category?"))
- document.location = "<?php echo basename($_SERVER['PHP_SELF']);?>?page=del&subcat_id="+id ;
- }
- </script>
- <form action="<?php echo basename($_SERVER['PHP_SELF']);?>" method="post" name="frmAdd" enctype="multipart/form-data">
- <table align="center"><tr><td style="color:#CC0000">
- <? if (!empty($_REQUEST['msg']))
- {
- if ($_REQUEST['msg'] == "err")
- {
- echo "<h5>";
- echo "This Category is Already Existed!";
- echo "</h5>";
- }
- }
- ?>
- </td></tr></table>
- <table width="46%" height="14%" border="0" cellpadding="3" cellspacing="0" align="center" class="tbord">
- <tr align="center">
- <th colspan="2" valign="top">Add Sub Category</th>
- </tr>
- <tr>
- <td align="right" valign="top" class="tdlabel">Main Category Name :</td>
- <td align="left" valign="top">
- <?
- $q4=mysql_query("SELECT DISTINCT maincat_id,maincat_name FROM maincategory_master");
- $maincat=$_GET['maincat'];
- echo "<select name='maincat' onchange=\"reload3(this.form)\"><option value=''>Select one</option>";
- while($notici = mysql_fetch_array($q4))
- {
- if($notici['maincat_id']==@$maincat)
- {
- echo "<option selected value='$notici[maincat_id]'>$notici[maincat_name]</option>"."<BR>";
- }
- else
- {
- echo "<option value='$notici[maincat_id]'>$notici[maincat_name]</option>";
- }
- }
- echo "</select>";
- ?>
- </td>
- </tr>
- <tr>
- <td align="right" valign="top" class="tdlabel">Category Name :</td>
- <td align="left" valign="top">
- <?php
- if(isset($maincat) and strlen($maincat) > 0)
- {
- $quer=mysql_query("SELECT DISTINCT category_name,category_id,maincat_id FROM category_master where maincat_id='$maincat'");
- }
- echo "<select name='category' ><option value=''>Select one</option>";
- while($name= mysql_fetch_array($quer))
- {
- if($name['category_id']==@$cat3)
- {
- echo "<option selected value='$name[category_id]'>$name[category_name]</option>"."<BR>";
- }
- else
- {
- echo "<option value='$name[category_id]'>$name[category_name]</option>";
- }
- }
- echo "</select>";
- ?>
- </td>
- </tr>
- <tr>
- <td align="right" valign="top" class="tdlabel">Sub Category Name : </td>
- <td align="left" valign="top"><input type="text" name="subcat"></td>
- </tr>
- <tr align="center">
- <td colspan="2" valign="top"><input name="Insert" type="submit" class="button" value="Insert" onClick="return checkForm()">
- <input type="hidden" name="page" value="add"></td>
- </tr>
- </table>
- <script language="javascript">
- function reload3(form)
- {
- var val=form.maincat.options[form.maincat.options.selectedIndex].value;
- self.location='subcategory.php?maincat=' + val ;
- }
- </script>
- </form>
- <table width="57%" align="center">
- <?php
- $page_name="subcategory.php"; // If you use this code with a different page ( or file ) name then change this
- $start=$_GET['start']; // To take care global variable if OFF
- if(!($start > 0))
- { // This variable is set to zero for the first page
- $start = 0;
- }
- $eu = ($start -0);
- $limit = 15; // No of records to be shown per page.
- $this1 = $eu + $limit;
- $back = $eu - $limit;
- $next = $eu + $limit;
- $query2="SELECT subcat_id FROM subcat_master";
- $result2=mysql_query($query2);
- echo mysql_error();
- $nume=mysql_num_rows($result2);
- global $db;
- $sql = "SELECT * FROM subcat_master ORDER BY subcat_name limit $eu, $limit ";
- if($countreis = $db->get_results($sql))
- {
- ?>
- <tr>
- <th align="center">Main Category Name</th>
- <th align="center">Category Name</th>
- <th align="center">Sub Category Name</th>
- <th align="center" colspan="2">Operations</th>
- </tr>
- <?php
- $odev = 1;
- foreach($countreis as $country)
- {
- $q1="select maincat_name from maincategory_master where maincat_id='$country->maincat_id'";
- $main=$db->get_var($q1);
- $q2="select category_name from category_master where category_id='$country->category_id'";
- $cate=$db->get_var($q2);
- ?>
- <tr class="<?php if($odev % 2) echo 'TrOdd'; else echo 'TrRows';?>">
- <td><?php echo $main;?></td>
- <td><?php echo $cate;?></td>
- <td><?php echo $country->subcat_name;?></td>
- <td align="center">
- <a href="<?php echo basename($_SERVER['PHP_SELF']).'?page=edit&subcat_id='.$country->subcat_id;?>" class="link">Edit </a></td>
- <td align="center">
- <a href="javascript:sureToDelete(<?php echo $country->subcat_id;?>)" class="link">Delete</a></td>
- </tr>
- <?php
- $odev++;
- }
- } else{
- ?>
- <tr><td><?
- echo 'No record found!';
- ?></td></tr><? }
- ?>
- </table>
- <?php
- $p_limit=60; // This should be more than $limit and set to a value for whick links to be breaked
- $p_f=$_GET['p_f']; // To take care global variable if OFF
- if(!($p_f > 0))
- { // This variable is set to zero for the first page
- $p_f = 0;
- }
- $p_fwd=$p_f+$p_limit;
- $p_back=$p_f-$p_limit;
- //////////// End of variables for advance paging ///////////////
- /////////////// Start the buttom links with Prev and next link with page numbers /////////////////
- echo "<table align = 'center' width='50%'><tr><td align='left' width='20%'>";
- if($p_f<>0)
- {
- print "<a href='$page_name?start=$p_back&p_f=$p_back'><font face='Verdana' size='1' Color='SteelBlue'>PREV $p_limit</font></a>";
- }
- echo "</td><td align='left' width='10%'>";
- //// if our variable $back is equal to 0 or more then only we will display the link to move back ////////
- if($back >=0 and ($back >=$p_f))
- {
- print "<a href='$page_name?start=$back&p_f=$p_f'><font face='Verdana' size='1' Color='DarkBLue'>PREV</font></a>";
- }
- //////////////// Let us display the page links at center. We will not display the current page as a link ///////////
- echo "</td><td align=center width='30%'>";
- for($i=$p_f;$i < $nume and $i<($p_f+$p_limit);$i=$i+$limit)
- {
- if($i <> $eu)
- {
- $i2=$i+$p_f;
- echo " <a href='$page_name?start=$i&p_f=$p_f'><font face='Verdana' size='1' Color='SteelBlue'>$i</font></a> ";
- }
- else
- {
- echo "<font face='Verdana' size='2' Color='DarkBlue'>$i</font>";
- } /// Current page is not displayed as link and given font color red
- }
- echo "</td><td align='right' width='10%'>";
- ///////////// If we are not in the last page then Next link will be displayed. Here we check that /////
- if($this1 < $nume and $this1 <($p_f+$p_limit))
- {
- print "<a href='$page_name?start=$next&p_f=$p_f'><font face='Verdana' size='1' Color='DarkBlue'>NEXT</font></a>";}
- echo "</td><td align='right' width='20%'>";
- if($p_fwd < $nume)
- {
- print "<a href='$page_name?start=$p_fwd&p_f=$p_fwd'><font face='Verdana' size='1' Color='DarkBlue'>NEXT $p_limit</font></a>";
- }
- echo "</td></tr></table>";
- }
- function addcountry()
- {
- global $db;
- $maincat = $_POST['maincat'];
- $category=$_POST['category'];
- $subcat=ucwords($_POST['subcat']);
- $sql="select maincat_id,category_id,subcat_name from subcat_master where maincat_id='$maincat' and category_id='$category' and subcat_name='$subcat'";
- $cat=$db->get_row($sql);
- if($cat->maincat_id && $cat->category_id && $cat->subcat_name)
- {
- echo "<script language='javascript'>";
- echo "window.location='subcategory.php?msg=err'";
- echo "</script>"; }
- $q1="insert into subcat_master(maincat_id,category_id,subcat_name) values('$maincat','$category','$subcat')";
- $db->query($q1);
- echo '<script language="javascript">document.location="'.basename($_SERVER['PHP_SELF']).'";</script>'."\n";
- }
- function showEdit()
- {
- global $db;
- $id = $_REQUEST['subcat_id'];
- $sql = "SELECT maincat_id,category_id,subcat_name FROM subcat_master WHERE subcat_id = '$id'";
- $cname = $db->get_row($sql);
- $q1="select maincat_name from maincategory_master where maincat_id='$cname->maincat_id'";
- $main=$db->get_var($q1);
- ?>
- <form action="<?php echo basename($_SERVER['PHP_SELF']);?>" method="post" name="frmEdit" enctype="multipart/form-data">
- <script language="javascript">
- function checkForm()
- {
- var ccategory,csubcat;
- with(window.document.frmEdit)
- {
- ccategory = category;
- csubcat = subcat;
- }
- if(trim(ccategory.value) == '')
- {
- alert('Category is required!');
- ccategory.focus();
- return false;
- }
- else if(trim(ccsubcat.value) == '')
- {
- alert('Sub Category is required!');
- csubcat.focus();
- return false;
- }
- else
- {
- ccategory.value = trim(ccategory.value);
- return true;
- }
- }
- function trim(str)
- {
- return str.replace(/^\s+|\s+$/g,'');
- }
- </script>
- <table width="313" align="center" cellpadding="3" cellspacing="0" class="tbord">
- <tr align="center">
- <th colspan="2" class="tbord">Edit Sub Category </th>
- </tr>
- <tr>
- <td align="right" valign="top" class="tdlabel">Main Category Name :</td>
- <td align="left">
- <? echo $main;?>
- </td>
- </tr>
- <tr>
- <td align="right" valign="top" class="tdlabel">Category Name :</td>
- <td align="left">
- <?php
- $q2=mysql_query("SELECT DISTINCT category_name,category_id FROM category_master");
- echo "<select name='category'><option value=''>Select one</option>";
- while($noticia2 = mysql_fetch_array($q2))
- {
- if($noticia2['category_id']==$cname->category_id)
- {
- echo "<option selected value='$noticia2[category_id]'>$noticia2[category_name]</option>"."<BR>";
- }
- else
- {
- echo "<option value='$noticia2[category_id]'>$noticia2[category_name]</option>";
- }
- }
- echo "</select>";
- ?>
- </td>
- </tr>
- <tr>
- <td align="right" valign="top" class="tdlabel">Sub Category Name : </td>
- <td align="left"><input type="text" name="subcat" value="<? echo $cname->subcat_name;?>"></td>
- </tr>
- <tr align="center">
- <td colspan="2">
- <input type="hidden" name="page" value="update">
- <input type="hidden" name="subcat_id" value="<?php echo $id;?>">
- <input type="submit" value="Update" class="button" name="update" onClick="return checkForm()">
- <input type="button" value="Back" onClick="document.location='<?php echo basename($_SERVER['PHP_SELF']);?>'" class="button"> </td>
- </tr>
- </table>
- <script language="javascript">
- function reload2(form)
- {
- var val=form.main1.options[form.main1.options.selectedIndex].value;
- self.location='subcategory.php?page=edit&main1=' + val ;
- }
- </script>
- </form>
- <?php
- }
- function updatecountry()
- {
- global $db;
- $id=$_REQUEST['subcat_id'];
- $category=$_POST['category'];
- $subcat = ucwords($_POST['subcat']);
- $sql="select category_id,subcat_name from subcat_master where maincat_id='$maincat' and category_id='$category' and subcat_name='$subcat'";
- $cat=$db->get_row($sql);
- if($cat->category_id && $cat->subcat_name)
- {
- header('Location: '.basename($_SERVER['PHP_SELF']).'?msg=err');
- exit;
- }
- $q1="update subcat_master set category_id='$category',subcat_name='$subcat' where subcat_id='$id'";
- $db->query($q1);
- echo '<script language="javascript">document.location="'.basename($_SERVER['PHP_SELF']).'";</script>'."\n";
- }
- function delcountry()
- {
- global $db;
- $id=$_REQUEST['subcat_id'];
- $sql ="delete FROM subcat_master WHERE subcat_id= '$id'";
- $db->query($sql);
- echo '<script language="javascript">document.location="'.basename($_SERVER['PHP_SELF']).'";</script>'."\n";
- }
- ?>
- </td>
- </tr>
- <tr>
- <td colspan="2" height="2%" valign="top">
- <?php include 'templates/footer.inc'?>
- </td>
- </tr>
- </table>
- </body>
- </html>