Jump to content

Dependable combos in PHP and MYSQL


seeee

Recommended Posts

Hi

 

i am a newbie in php and mysql and am stuck with combos. I have two dependable combos say categories combo and subcategories combo. When a user selects a

 

category then corresponding subcategories get loaded after that and the second combo gets populated. I have other fields too so there will be one final

 

submit at the end.

 

Please send me a working example if anyone has it or has a better approach to this problem as i am completely new to PHP.

 

This is a part of it that i got online and was trying it. Please help me with it.

 

THE CODE STARTS HERE

<?php

 

global $onpage;

$onpage = $_SERVER['PHP_SELF'];

 

  // Connect to the test datbase on localhost

  // That's where we created the countries table above

  mysql_connect('localhost','finaluser','sp');

  mysql_select_db('dbtest');

 

  // Query the countries table and load all of the records

  // into an array.

  $sql = 'SELECT * FROM category';

  $res = mysql_query($sql) or die(mysql_error());

  while ($rec = mysql_fetch_assoc($res)) $categories[] = $rec;

 

  ////pasted form code here

  echo "<form  action=\"<?=$onpage?>\" method=\"post\" name=\"cat\">";

  echo '<SELECT name="cat">';

  echo "<OPTION value=\"\" SELECTED>-----  Choose your category first  ----</OPTION>\n";

  foreach ($categories as $c)

  {

    if ($c['id'] == $_GET['id'])

      echo "<OPTION value=\"{$c['catid']}\" >{$c['catname']}</OPTION>\n";

      //echo "<OPTION value=\"{$c['catid']}\" SELECTED>{$c['catname']}, {$c['catname']}</OPTION>\n";

  }

  echo '</SELECT>';

 

  echo $c['catid'];

 

  echo "/////////////////////////////";

  //second combo box2

  $sql2 = 'SELECT * FROM subcategory';

  $res2 = mysql_query($sql2) or die(mysql_error());

  while ($rec = mysql_fetch_assoc($res2)) $subcategories[] = $rec;

  echo '<SELECT name="subcat">';

  echo "<OPTION value=\"\" SELECTED>-----  Choose your sub category    ----</OPTION>\n";

  foreach ($subcategories as $c2)

  {

    if ($c2['id'] == $_GET['id'])

      echo "<OPTION value=\"{$c2['subcatid']}\" >{$c2['subcatname']}</OPTION>\n";

      //echo "<OPTION value=\"{$c['catid']}\" SELECTED>{$c['catname']}, {$c['catname']}</OPTION>\n";

  }

  echo '</SELECT>';

  echo '</form>';

 

  //echo $forms["subcat"];

?>

 

Please help me with this as i am stuck with it and cannot go further.

thanks in advance.

seema

Link to comment
https://forums.phpfreaks.com/topic/54954-dependable-combos-in-php-and-mysql/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.