Skip navigation
Wookie
Currently Being Moderated

Dynamic dropdown using URLs from database (PHP / MySql)

Sep 20, 2011 8:24 AM

Hi,

 

I have two list boxes. The 2nd dropdown dynamically displays the content related to the users selection from the 1st dropdown.

 

This all works as I want it to.

 

The next step I want to achieve is when an item is selected from the 2nd dropdown and the form is submitted the relevant page opens (I have a column in the database table which stores the URL's)

 

Any help would be greatly appreciated!

 

 

 

These are my database tables:

 

table 'Category' for 1st dropdown

 

cat_id    category                          item_code

1            Moulded Letters                ML

2            Moulded Letters (Extra)     MLE

 

 

table 'Subcategory' for 2nd dropdown

 

cat_id    subcategory       destination_page         item_code

1            1700mm             detail_ml1700.php          ML

1            2000mm             detail_ml2000.php          ML

1            2500mm             detail_ml2500.php          ML

1            2900mm             detail_ml2900.php          ML

2            600mm               detail_mle600.php          MLE

2            900mm               detail_mle900.php          MLE

2            1200mm             detail_mle1200.php        MLE

2            1500mm             detail_mle1500.php        MLE

 

 

This is the code for my page, I think I need to tinker with the code in the FORM ACTION section but I can't seem to assign the URL's from the database.

 

See the section highlighted in bold:    echo "<form method=post name=f1 action= **URL in database** >";

 

 

 

 

 

<?php require_once('../Connections/conn_test.php'); ?>

<?php

if (!function_exists("GetSQLValueString")) {

function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")

{

  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

 

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

 

  switch ($theType) {

    case "text":

      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";

      break;   

    case "long":

    case "int":

      $theValue = ($theValue != "") ? intval($theValue) : "NULL";

      break;

    case "double":

      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";

      break;

    case "date":

      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";

      break;

    case "defined":

      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;

      break;

  }

  return $theValue;

}

}

 

mysql_select_db($database_conn_test, $conn_test);

$query_rsSigns = "SELECT * FROM subcategory";

$rsSigns = mysql_query($query_rsSigns, $conn_test) or die(mysql_error());

$row_rsSigns = mysql_fetch_assoc($rsSigns);

$totalRows_rsSigns = mysql_num_rows($rsSigns);

 

 

$dbservertype='mysql';

$servername='localhost';

$dbusername='****';

$dbpassword='****';

$dbname='***';

 

connecttodb($servername,$dbname,$dbusername,$dbpassword);

function connecttodb($servername,$dbname,$dbuser,$dbpassword)

{

global $link;

$link=mysql_connect ("$servername","$dbuser","$dbpassword");

if(!$link){die("Could not connect to MySQL");}

mysql_select_db("$dbname",$link) or die ("could not open db".mysql_error());

}

 

?>

 

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

 

<html>

 

<head>

<title>Multiple drop down list box</title>

<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>

<?

 

@$cat=$_GET['cat']; // Use this line or below line if register_global is off

if(strlen($cat) > 0 and !is_numeric($cat)){ // to check if $cat is numeric data or not.

echo "Data Error";

exit;

}

 

 

//@$cat=$HTTP_GET_VARS['cat']; // Use this line or above line if register_global is off

 

///////// Getting the data from Mysql table for first list box//////////

$quer2=mysql_query("SELECT DISTINCT category,cat_id FROM category order by category");

///////////// End of query for first list box////////////

 

/////// for second drop down list we will check if category is selected else we will display all the subcategory/////

if(isset($cat) and strlen($cat) > 0){

$quer=mysql_query("SELECT DISTINCT subcategory FROM subcategory where cat_id=$cat order by subcategory");

}else{$quer=mysql_query("SELECT DISTINCT subcategory FROM subcategory order by subcategory"); }

////////// end of query for second subcategory drop down list box ///////////////////////////

 

echo "<form method=post name=f1 action= **URL in database** >";

 

//////////        Starting of first drop downlist /////////

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

while($noticia2 = mysql_fetch_array($quer2)) {

if($noticia2['cat_id']==@$cat){echo "<option selected value='$noticia2[cat_id]'>$noticia2[category]</option>"."<BR>";}

else{echo  "<option value='$noticia2[cat_id]'>$noticia2[category]</option>";}

}

echo "</select>";

//////////////////  This will end the first drop down list ///////////

 

//////////        Starting of second drop downlist /////////

echo "<select name='subcat'><option value=''>Select one</option>";

while($noticia = mysql_fetch_array($quer)) {

echo  "<option value='$noticia[subcategory]'>$noticia[subcategory]</option>";

}

echo "</select>";

//////////////////  This will end the second drop down list ///////////

//// Add your other form fields as needed here/////

echo "<input type=submit value=Submit>";

echo "</form>";

?>

 

</body>

 

</html>

<?php

mysql_free_result($rsSigns);

?>

 

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points