Hi frnds..

i want to daisplay values in listbox from database table regarding selected item in first listbox?

here i copied the code...in this i got values of first listbox frm db table..after i want 2nd listbox values from db table of 1st selected item..

plz help ma asap...
i am eagarly waiting 4 reply..

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<table width="100%" height="208" border="0">
  <tr>
    <th width="87%" height="21" scope="col">
      <div align="right">
        <table width="60%" border="0">
          <tr>
            <th width="34%" scope="col"><div align="right"><a href="addcourses.php">Add Courses</a></div></th>
            <th width="40%" scope="col"><div align="right"><a href="addsubcourses.php">Add Sub Courses</a></div></th>
            <th width="26%" scope="col"><div align="right">Add Content</div></th>
          </tr>
            </table>
    </div></th>
    <th width="13%" scope="col"><a href="logout.php">Logout</a></th>
  </tr>
  <tr>
    <td>
    <form name="form" method="post" action="content.php">
    <table width="100%" height="118" border="0">
      <tr>
        <td><div align="right"><strong>Course</strong></div></td>
        <td><div align="center"><strong>:</strong></div></td>
        <td><select name="cname" id="select" >
		<?php 
include("config.php");
$ress=mysql_query("select cname from addcourse ") or die(mysql_error());


while ($row = mysql_fetch_array($ress))	 { 
        
	?>
        <option value="<?php echo $row['cname'];  ?> " > <?php echo $row['cname']; ?></option>
        <?php
		}
		?>
        
        </select></td>
      </tr>
      <tr>
        <th width="57%" scope="col"><div align="right">Sub Course </div></th>
        <th width="8%" scope="col">:</th>
        <th width="35%" scope="col" align="left">
		[B]<select name="subcname" id="">
        
		<?php 
include("config.php");
$result=mysql_query("select subcname from subcourse  ") or die(mysql_error());


while ($row = mysql_fetch_array($result))	 { 
        
	?>
        <option value="<?php echo $row['subcname'];  ?> " > <?php echo $row['subcname']; ?></option>
        <?php
		}
		?>[/B]
        
        </select></th>
      </tr>
      
      <tr>
        <td><div align="right"><strong>Content</strong></div></td>
        <td><div align="center"><strong>:</strong></div></td>
        <td><textarea name="content" id="textfield2" rows="5" cols="30"></textarea></td>
      </tr>
      <tr>
        <td>
          <div align="right">
            <input type="submit" name="button" id="button" value="Add Content" />
            </div></td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
      
    </table>
    </form>
    
    </td>
    <td>&nbsp;</td>
  </tr>
</table>
</body>
</html>

Thanks in advance...

Member Avatar for langsor

Okay, I did NOT test this, so who knows if it will actually work or what bugs, spelling, and syntax errors I have in it ... but this is the idea I would go with and you can check the errors to figure out what I did wrong. :-)

Hope it helps ...

content.php

<?php

include_once( "config.php" );

$doc = new DOMDocuemt('1.0');
$doc->loadHTMLFile('form.html');
$nodes = $doc->getElementsByTagName('*');
foreach ( $nodes as $node ) {
  switch ( $node->getAttribute('id') ) {
    case 'select': $select = $node; break;
    case 'select2': $select2 = $node; break;
  }
}

$result = mysql_query("SELECT `cname` FEOM `addcourse`") 
  or die(mysql_error());
if ( mysql_num_rows( $result ) ) {
  while ( $obj = mysql_fetch_object( $result ) ) {
    $option = $doc->createElement('option');
    $option->setAttribute('value',"obj->cname");
    $option->nodeValue = $obj->cname;
    $select->appendChild( $option );
  }
  mysql_free_result( $result );
} else {
  die( "No results for first select box query" );
}

if ( $cname = $_REQUEST['cname'] ) {
  $result = mysql_query("SELECT `subcname` FEOM `subcourse` WHERE `cname`='$cname'") 
  or die(mysql_error());
  // WHERE `cname`='$cname' ... here `cname` must match field searched specific to your `subcourse` database table
  if ( mysql_num_rows( $result ) ) {
    while ( $obj = mysql_fetch_object( $result ) ) { 
      $option = $doc->createElement('option');
      $option->setAttribute('value',"obj->subcname");
      $option->nodeValue = $obj->subcname;
      $select2->appendChild( $option );
    }
  } else {
    die( "No results for second select box query" );
  }
}

print $doc->saveHTML();
?>

form.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>Untitled Document</title>
</head>
<body>
<table width="100%" height="208" border="0">
  <tr>
    <th width="87%" height="21" scope="col"> <div align="right">
        <table width="60%" border="0">
          <tr>
            <th width="34%" scope="col"><div align="right"><a href="addcourses.php">Add Courses</a></div></th>
            <th width="40%" scope="col"><div align="right"><a href="addsubcourses.php">Add Sub Courses</a></div></th>
            <th width="26%" scope="col"><div align="right">Add Content</div></th>
          </tr>
        </table>
      </div></th>
    <th width="13%" scope="col"><a href="logout.php">Logout</a></th>
  </tr>
  <tr>
    <td><form name="form" method="post" action="content.php">
        <table width="100%" height="118" border="0">
          <tr>
            <td><div align="right"><strong>Course</strong></div></td>
            <td><div align="center"><strong>:</strong></div></td>
            <td><select name="cname" id="select"></select></td>
          </tr>
          <tr>
            <th width="57%" scope="col"><div align="right">Sub Course </div></th>
            <th width="8%" scope="col">:</th>
            <th width="35%" scope="col" align="left"> <select name="subcname" id="select2"></select></th>
          </tr>
          <tr>
            <td><div align="right"><strong>Content</strong></div></td>
            <td><div align="center"><strong>:</strong></div></td>
            <td><textarea name="content" id="textfield2" rows="5" cols="30"></textarea></td>
          </tr>
          <tr>
            <td><div align="right"><input type="submit" name="button" id="button" value="Add Content" /></div></td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
          </tr>
        </table>
      </form></td>
    <td>&nbsp;</td>
  </tr>
</table>
</body>
</html>

hi,create a new field parentid in ur table and insert values like this

<?  $qry2="INSERT INTO fm_forums(title,description,parentid) VALUES ('".$_POST['title']."','".$_POST['description']."','".$_POST['selcategory']."')";
			 $res2=mysql_query($qry2) or die(mysql_error());?>
<table width="85%"  border="0" align="center" cellpadding="6" cellspacing="1" bgcolor="#CCCCCC">
			
                <tr bgcolor="#E1E1E1">
                  <td width="25%" bgcolor="#FFFFFF"><strong>Sub-Topic Title : </strong></td>
                  <td width="75%" bgcolor="#FFFFFF"><input name="title" type="text" class="Inputform" id="title2" size="60" <? if (isset($_GET['aid'])){?>value="<?=$subcategory?>"><? }?></td>
                </tr><?
				    $ers="SELECT * FROM fm_forums WHERE parentid=0";
					$sed=mysql_query($ers) or die(mysql_error());
					$sed1=mysql_fetch_array($sed);
					$qry4="SELECT * FROM fm_forums WHERE parentid=".$sed1['forumid'];
					$res4=mysql_query($qry4) or die(mysql_error());
					$num4=mysql_num_rows($res4);
					if($num4>0)
					{
					
					?>
                <tr bgcolor="#E1E1E1">
                  <td bgcolor="#FFFFFF"><strong>Under Topic : </strong></td>
                  <td bgcolor="#FFFFFF">
				  
                    <select name="selcategory" class="box2" id="selcategory">
                      <option value="">------Selectsubtopic-------</option>
                    <?  while($row4=mysql_fetch_array($res4))
					{ ?>
                      <option value="<?=$row4['forumid']?>" <? if ((isset($_GET['forumid']))&&($row4['forumid']==$category)){?>selected<? }?>>
                      <?=$row4['title']?><? }
				}?>
                      </option>
               
                    </select></td>
                </tr>
                <tr bgcolor="#E1E1E1">
                  <td bgcolor="#FFFFFF"><strong>Summary :</strong></td>
                  <td bgcolor="#FFFFFF"><textarea name="description" cols="60" rows="8" class="Inputform" id="description"><? if (isset($_GET['aid'])){ echo "$subcategory1";}?></textarea></td>
                </tr>
            </table>

in front end u will display based on id like this

<tr>
        <th width="57%" scope="col"><div align="right">Sub Course </div></th>
        <th width="8%" scope="col">:</th>
        <th width="35%" scope="col" align="left">
		<select name="subcname" id="">
        
		<?php 
include("config.php");
$result=mysql_query("select subcname from subcourse where parentid=".$row[id]) or die(mysql_error());


while ($row = mysql_fetch_array($result))	 { 
        
	?>
        <option value="<?php echo $row['subcname'];  ?> " > <?php echo $row['subcname']; ?></option>
        <?php
		}
		?>
        
        </select></th>
      </tr>

i hope u got what iam trying to say and dont worry about aid here i called it for editing
if any doubts post here

Thank u praveen,

It looks simple...but i didn,t get totally..i.e where i want to used that code.can u explain me plz...

In which page i will use that query 2?

thanks once again..

okay, create parentid field in same dbtable in which u have list values and create a new php page to insert dbvalues in second list box for that use my first code. in the page u posted use my second code to display ur second list box values from db.

Hi langsor..
Thank u verymuch...

here i got an error..
"Fatal error: Class 'DOMDocuemt' not found in C:\wamp\www\training\content.php on line 5 "


can u explain me please....

Thanks in advance

i dont knew but may be this attachment may work

i dont knew but may be this attachment may work

Thanks praveen..
can u see this attachment..plz do that.. here also using php4..

Thank u once again.....

hi saritha,
here is the attachment it worked perfectly for me, hope it helps to u

hi saritha, i have made little modifications to insert names in add content column

Member Avatar for langsor

Hi langsor..
Thank u verymuch...

here i got an error..
"Fatal error: Class 'DOMDocuemt' not found in C:\wamp\www\training\content.php on line 5 "


can u explain me please....

Thanks in advance

It's a type-o, should be DOMDocument

Also, the SELECT `cname` FEOM sql statements should be SELECT `cname` FROM

There may be others ... ;-)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.