Web Design Forum: Found this code for drop down lists, can some help tweak it for me? - Web Design Forum

Jump to content

WDF
WDF Premium Memberships Reseller Hosting
Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Found this code for drop down lists, can some help tweak it for me? Rate Topic: -----

#1 User is offline   dan... 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 54
  • Joined: 28-January 10
  • Reputation: 0

Posted 12 March 2010 - 10:35 PM

Hi

I found this code by googling, I would like to place the second list on a separate page and possible add a 3rd, can anyone help to tweak as i've stared at it for ages and it's over my head?

<?php
//***************************************
// This is downloaded from www.plus2net.com //
/// You can distribute this code with the link to www.plus2net.com ///
// Please don't remove the link to www.plus2net.com ///
// This is for your learning only not for commercial use. ///////
//The author is not responsible for any type of loss or problem or damage on using this script.//
/// You can use it at your own risk. /////
//*****************************************

$dbservertype='mysql';
$servername='localhost';
// username and password to log onto db server
$dbusername='';
$dbpassword='';
// name of database
$dbname='dd';

////////////////////////////////////////
////// DONOT EDIT BELOW /////////
///////////////////////////////////////
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());
}
//////// End of connecting to database ////////
?>

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

<html>

<head>
<title>Multiple drop down list box from plus2net</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>
<?

/*
If register_global is off in your server then after reloading of the page to get the value of cat from query string we have to take special care.
To read more on register_global visit.
http://www.plus2net....ter-globals.php
*/
@$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='dd-check.php'>";
/// Add your form processing page address to action in above line. Example action=dd-check.php////
////////// 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>";
?>
<center><a href='http://www.plus2net.com'>PHP SQL HTML free tutorials and scripts</a></center>
</body>

</html>
0

#2 User is offline   dan... 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 54
  • Joined: 28-January 10
  • Reputation: 0

Posted 13 March 2010 - 11:07 AM

View Postdan..., on 12 March 2010 - 10:35 PM, said:

Hi

I found this code by googling, I would like to place the second list on a separate page and possible add a 3rd, can anyone help to tweak as i've stared at it for ages and it's over my head?



if anyone is interested, i managed to tweak it myself, i have to files first called test.php second is test2.php, here is the code for test.php

<?php
$connect = mysql_connect ("localhost","root","root") or die(mysql_error());
mysql_select_db("dd") or die (mysql_error());
?>
<html>
<head>
<title>Multiple drop down list box from plus2net</title>
<script language=JavaScript>
function reload(form)
{
var val=form.cat.options[form.cat.options.selectedIndex].value;
self.location='test.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;
}


echo "<form method=get name=f1 action='test2.php'>";

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

echo "<select name='cat' onchange=\"reload(this.form)\"><option value=''>Please Select</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>";

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

?>

************************here is the code for tes2.php

<?php
$connect = mysql_connect ("********","********","********") or die(mysql_error());
mysql_select_db("******") or die (mysql_error());
?>
<html>
<head>
<title>Multiple drop down list box from plus2net</title>
<script language=JavaScript>
function reload(form)
{
var val=form.cat.options[form.cat.options.selectedIndex].value;
self.location='test2.php?cat=' + val ;
}
</script>
</head>
<body>
<?

@$cat=$HTTP_GET_VARS['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;
}


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"); }

echo "<select name='subcat'><option value=''>Please Select</option>";
while($noticia = mysql_fetch_array($quer)) {
echo "<option value='$noticia[subcategory]'>$noticia[subcategory]</option>";
}
echo "</select>";

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

?>


************************

I hope this helps someone like me who's new, i just need to figure out how to post the data to the database once all the selections have been made, anyone?
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users