dependent drop down boxes

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jrsl
Forum Newbie
Posts: 4
Joined: Wed Nov 26, 2008 7:35 pm

dependent drop down boxes

Post by jrsl »

Hi my name is Justin,

I'm having trouble with my dependent drop down boxes. They currently work fine, however when i want to insert my data into mysql it returns the combo box value rather than the text.

Making the boxes dependent on each other requires me to use numbers rather than the text i wish to be inserted to the database. so my question is can i make the database insert the text from the boxes or can i have some code in my php code stating:

" if cmbBox1 value = '1' then value = 'Txt from box 1' "

i have included my html and php code so you can have a look at my code...

Thanks in advance,

Justin


************HTML/JAVASCRIPT*****

Code: Select all

<script type="text/javascript">
function setOptions(chosen, selbox) {
 
selbox.options.length = 0;
if (chosen == "9") {
  selbox.options[selbox.options.length] = new Option('Please Select Price Range',' ');
  setTimeout(setOptions('8',document.thisform.wType),0);
}
if (chosen == "8") {
  selbox.options[selbox.options.length] = new Option('Please Select Price Range',' ');
  setTimeout(setOptions('7',document.thisform.salePrice),0);
}
if (chosen == "7") {
  selbox.options[selbox.options.length] = new Option('Please Select Price Range','2');
}
//---------------------------------------- Second drop down box -----------------------------------------------------
//option two Manufactures Warranty (Years):
if (chosen == "1") {
  selbox.options[selbox.options.length] = new Option('12 Months','11'); //0 - 300 //11
  selbox.options[selbox.options.length] = new Option('24 Months','12');//12
setTimeout(setOptions('11',document.thisform.wType),5);
}
if (chosen == "2") {
  selbox.options[selbox.options.length] = new Option('12 Months','21');//301 - 1000
  selbox.options[selbox.options.length] = new Option('24 Months','22');
setTimeout(setOptions('21',document.thisform.wType),5);
}
if (chosen == "3") {
  selbox.options[selbox.options.length] = new Option('12 Months','31');//1001 - 2001
  selbox.options[selbox.options.length] = new Option('24 Months','32');
setTimeout(setOptions('31',document.thisform.wType),5);
}
//---------------------------------------- Third drop down box -----------------------------------------------------
//option three Desired Extended Warranty:
if (chosen == "11") {
  selbox.options[selbox.options.length] = new Option('3 Years Total Warranty','111');//0 - 300
  selbox.options[selbox.options.length] = new Option('5 Years Total Warranty','112');
  setTimeout(setOptions('111',document.thisform.salePrice),5);
}
if (chosen == "12") {
  selbox.options[selbox.options.length] = new Option('3 Years Total Warranty','121'); //0 - 300
  selbox.options[selbox.options.length] = new Option('5 Years Total Warranty','122');
    setTimeout(setOptions('121',document.thisform.salePrice),5);
}
if (chosen == "21") {
  selbox.options[selbox.options.length] = new Option('3 Years Total Warranty','211'); //301 - 1000
  selbox.options[selbox.options.length] = new Option('5 Years Total Warranty','212');
    setTimeout(setOptions('211',document.thisform.salePrice),5);
}
if (chosen == "22") {
  selbox.options[selbox.options.length] = new Option('3 Years Total Warranty','221');
  selbox.options[selbox.options.length] = new Option('5 Years Total Warranty','222');
    setTimeout(setOptions('221',document.thisform.salePrice),5);
}
if (chosen == "31") {
  selbox.options[selbox.options.length] = new Option('3 Years Total Warranty','311');//1001 - 2001
  selbox.options[selbox.options.length] = new Option('5 Years Total Warranty','312');
    setTimeout(setOptions('311',document.thisform.salePrice),5);
}
if (chosen == "32") {
  selbox.options[selbox.options.length] = new Option('3 Years Total Warranty','321');
  selbox.options[selbox.options.length] = new Option('5 Years Total Warranty','322');
    setTimeout(setOptions('321',document.thisform.salePrice),5);
}
//---------------------------------------- Fourth drop down box -----------------------------------------------------
//Option Four Your Price calculated
if (chosen == "111") {
  selbox.options[selbox.options.length] = new Option('$50.00','1111'); //0 - 300 + 12w + 3 year extended
}
if (chosen == "112") {
  selbox.options[selbox.options.length] = new Option('$65.00','1121'); //0 - 300 + 12w + 5 year ew
}
if (chosen == "121") {
  selbox.options[selbox.options.length] = new Option('$35.00','1211'); //0 - 300 + 24w + 3 year ew
 
}
if (chosen == "122") {
  selbox.options[selbox.options.length] = new Option('$45.00','1221'); //0 - 300 + 24w + 5 year ew
 
}
if (chosen == "211") {
  selbox.options[selbox.options.length] = new Option('$100.00','2111');//301 - 1000 + 12w + 3 year extended
 
}
if (chosen == "212") {
  selbox.options[selbox.options.length] = new Option('$130.00','2121'); //301 - 1000 + 12w + 5 year ew
 
}
if (chosen == "221") {
  selbox.options[selbox.options.length] = new Option('$70.00','2211'); //301 - 1000 + 24w + 3 year ew
 
}
if (chosen == "222") {
  selbox.options[selbox.options.length] = new Option('$90.00','2221'); //301 - 1000 + 24w + 5 year ew
 
}
if (chosen == "311") {
  selbox.options[selbox.options.length] = new Option('$200.00','3111'); //1001 - 2001 + 12w + 3 year extended
 
}
if (chosen == "312") {
  selbox.options[selbox.options.length] = new Option('$260.00','3121'); //1001 - 2001 + 12w + 5 year ew
 
}
if (chosen == "321") {
  selbox.options[selbox.options.length] = new Option('$140.00','3211'); //1001 - 2001 + 24w + 3 year ew
 
}
if (chosen == "322") {
  selbox.options[selbox.options.length] = new Option('$180.00','3221'); //301 - 1000 + 24w + 5 year ew
 
}
}
 
</script>

************************************************PHP*****

Code: Select all

<?php
$title = $_POST ['title'];
$fName = $_POST['fName'];
$sName = $_POST ['sName'];
$address = $_POST ['address'];
$suburb = $_POST ['suburb'];
$state = $_POST ['state'];
$pCode = $_POST ['pCode'];
$homePh = $_POST ['homePh'];
$mobilePh = $_POST ['mobilePh'];
$make = $_POST ['make'];
$model = $_POST ['model'];
$pRange = $_POST ['pRange'];
$mWarranty = $_POST ['mWarranty'];
$wType = $_POST ['wType'];
$pPrice = $_POST ['pPrice'];
$salePrice = $_POST ['salePrice'];
$pDate = $_POST ['pDate'];
 
$sql = ("INSERT INTO customer (title, fName, sName, address, suburb, state, pCode, homePh, mobilePh, make, model, pRange, mWarranty, wType, pPrice, salePrice, pDate)
VALUES('$_POST[title]','$_POST[fName]','$_POST[sName]','$_POST[address]','$_POST[suburb]','$_POST[state]','$_POST[pCode]','$_POST[homePh]','$_POST[mobilePh]','$_POST[make]','$_POST[model]','$_POST[pRange]','$_POST[mWarranty]','$_POST[wType]','$_POST[pPrice]','$_POST[salePrice]','$_POST[pDate]')");
 
 
mysql_query($sql) or die ('error updating database');
 
$sql2 = mysql_query("SELECT * FROM customer ORDER BY certNum DESC LIMIT 0,1;") 
   or die(mysql_error());
   
if (mysql_num_rows($sql2) > 0) {  
echo'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">';
echo'<html xmlns="http://www.w3.org/1999/xhtml">';
echo'<head>';
echo'<link href="stylesheet.css" rel="stylesheet" type="text/css" />';
 
echo'<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
echo'<title>Extended Warranty</title>';
echo'</head>';
echo'<body>';
echo'<div id="page">';
    echo'<div id="header">';
        echo'<a href="./index.html" class="logo"></a>';
    echo'</div>';
    echo'<div id="menulinks">';
    echo'<?PHP include "includes/links.php"; ?>';
    echo'</div>';
    echo'<div id="mainarea">';
        echo'<div id="contentarea"><table>';
        echo'<tr>';
 
  echo'<td width="282" valign="top">    <div id="sidebar">';
  echo'</div></td>';
   echo'<td width="657"><h2>Extended Warranty Invoice</h2>';
   echo '<table cellpadding="3">';   
  
   
 
   
   while ($row = mysql_fetch_assoc($sql2)) {
      
            echo"<tr><td><strong>Certificate Number:</strong></td><td>{$row['certNum']}</td><td><strong>Date of Purchase</strong><td>{$row['wDate']}</td></tr>";
            
            echo"<tr><td><strong>Name:</strong></td><td>{$row['title']}{$row['fName']}{$row['sName']}</td></tr>";
            echo"<tr><td><strong>Address:</strong></td><td>{$row['address']}</td></tr>";
echo"<tr><td><strong>Suburb:</strong></td><td>{$row['suburb']}</td></tr>";
echo"<tr><td><strong>State:</strong></td><td>{$row['state']}</td></tr>";
echo"<tr><td><strong>Post Code:</strong></td><td>{$row['pCode']}</td></tr>";
echo"<tr><td><strong>Home Phone Number:</strong></td><td>{$row['homePh']}</td></tr>";
echo"<tr><td><strong>Mobile Phone Number:</strong></td><td>{$row['mobilePh']}</td></tr>";
echo"<tr><td><strong>Make:</strong></td><td>{$row['make']}</td></tr>";
echo"<tr><td><strong>Model:</strong></td><td>{$row['model']}</td></tr>";
echo"<tr><td><strong>Manufactures Warranty:</strong></td><td>{$row['mWarranty']}</td></tr>";
echo"<tr><td><strong>Extended Warranty:</strong></td><td>{$row['wType']}</td></tr>";
echo"<tr><td><strong>Product Price:</strong></td><td>{$row['pPrice']}</td></tr>";
echo"<tr><td><strong>Product Purchase Date:</strong></td><td>{$row['pDate']}</td></tr>";
echo"<tr><td><strong>Extended Warranty Charge Total:</strong></td><td>{$row['salePrice']}</td></tr>";
        
            
            
          
   }
 
  echo'</td>';
  echo'</tr>';
   echo '</table>';
    echo '</table>';
    echo'</div>';
    
        echo'</div>';
    
    echo'<div id="footer">';
    echo'<?PHP include "includes/footer.php"; ?>';
    echo'</div>';
echo'</div>';
 
echo'</body>';
echo'</html>';
   
}
else
   echo 'No rows in selected table';
 
mysql_close($conn);
 
?>
Last edited by jrsl on Mon Dec 01, 2008 3:12 am, edited 1 time in total.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: dependent drop down boxes

Post by requinix »

Edit your post and put [ code ] or [ php ] tags around your code or the boogeyman will come and do it for you. And he uses red ink.

It returns a number instead of the text? Well yeah, that's how the code is designed. I guess you're supposed to take that number and look up the value.
However you should be able to replace all those numbers with something else; if you do it consistently then it will work fine.

Code: Select all

if (chosen == "1") { // this 1 probably needs to change
selbox.options[selbox.options.length] = new Option('12 Months','12 Months');
selbox.options[selbox.options.length] = new Option('24 Months','24 Months');
...
if (chosen == "12 Months") {
selbox.options[selbox.options.length] = new Option('3 Years Total Warranty','3 Years Total Warranty');
selbox.options[selbox.options.length] = new Option('5 Years Total Warranty','5 Years Total Warranty');
...
if (chosen == "3 Years Total Warranty") {
selbox.options[selbox.options.length] = new Option('$50.00','$50.00');
...
jrsl
Forum Newbie
Posts: 4
Joined: Wed Nov 26, 2008 7:35 pm

Re: dependent drop down boxes

Post by jrsl »

Thanks for your reply,

I have thought of that but i thought i would run into problems when i add the real data (lots of information). If for some reason this does not work is there a way to change the combo box value in the php code such as if combo value = 1 then change value = text as mentioned above?

Regards,
Justin
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: dependent drop down boxes

Post by requinix »

Well yeah, that's what I mentioned as your first option.

Your script just needs to know what number means what value, then you can use something like a switch block to do the "conversion". It's ugly, but it means less stuff to fix.
Post Reply