Note that there are some explanatory texts on larger screens.

plurals
  1. POThird dropdown conditional to values selected in first and second dropdown lists
    text
    copied!<p>I am using the following code to populate a triple drop down list. It is working, however the third drop down must also be conditional to the value selected in the first dropdown. How do i change this code to to make the 3rd dropdown conditional to the first and second dropdown value.</p> <p>Here is my code:</p> <pre><code>&lt;?php ini_set("display_errors", 0); //turn off error reporting! $data=@$_GET['data']; $val=@$_GET['val']; $dbhost = "localhost"; $dbuser = "root"; $dbpass = ""; $dbname = "legalwise"; mysql_pconnect($dbhost,$dbuser,$dbpass) or die ("Unable to connect to MySQL server") ; if ($data=='categoria') { echo "&lt;select name='categoria' onChange=\"dochange('subcategoria', this.value)\"&gt;\n"; echo "&lt;option value='0'&gt;== SELECT ==&lt;/option&gt;\n"; $result=mysql_db_query($dbname,"SELECT name FROM firm GROUP BY name"); while(list($nomeCategoria)=mysql_fetch_array($result)){ echo "&lt;option value=\"$nomeCategoria\" &gt;$nomeCategoria&lt;/option&gt; \n" ; } } else if ($data=='subcategoria') { echo "&lt;select name='subcategoria' onChange=\"dochange('subcategoria2', this.value)\"&gt;\n"; echo "&lt;option value='0'&gt;== SELECT ==&lt;/option&gt;\n"; $result=mysql_db_query($dbname,"SELECT hub FROM firm WHERE name='$val' GROUP BY hub"); while(list($name)=mysql_fetch_array($result)){ echo "&lt;option value=\"$name\"&gt;$name&lt;/option&gt; \n" ; } } else if ($data=='subcategoria2') { echo "&lt;select name='subcategoria2' &gt;\n"; echo "&lt;option value='0'&gt;== SELECT ==&lt;/option&gt;\n"; $result=mysql_db_query($dbname,"SELECT id, area FROM firm WHERE hub='$val'"); while(list($id, $name)=mysql_fetch_array($result)){ echo "&lt;option value=\"$id\" &gt;$name&lt;/option&gt; \n" ; } } echo "&lt;/select&gt;\n"; ?&gt; Here is the other part of my program that calls this one. &lt;?php require_once('startsession.php'); require_once('connectvars.php'); require_once('header.php'); include('menu.php'); ?&gt; &lt;form action="" method="post"&gt; &lt;/BR&gt; &lt;?php echo "&lt;font id=\"categoria\"&gt;&lt;select&gt;\n"; echo "&lt;option value='0'&gt;Select the Firm&lt;/option&gt; \n" ; echo "&lt;/select&gt;&lt;/font&gt;\n"; ?&gt; &lt;?php echo "&lt;font id=\"subcategoria\"&gt;&lt;select&gt;\n"; echo "&lt;option value='0'&gt;Select the Claims Hub&lt;/option&gt; \n" ; echo "&lt;/select&gt;&lt;/font&gt;\n"; ?&gt; &lt;?php echo "&lt;font id=\"subcategoria2\"&gt;&lt;select&gt;\n"; echo "&lt;option value='0'&gt;Select the Area&lt;/option&gt; \n" ; echo "&lt;/select&gt;&lt;/font&gt;\n"; ?&gt; &lt;!DOCTYPE html PUBLIC &gt; &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt; &lt;head&gt; &lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /&gt; &lt;title&gt;Add Mattertype&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;script language=Javascript&gt; function Inint_AJAX() { try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch(e) {} //IE try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {} //IE try { return new XMLHttpRequest(); } catch(e) {} //Native Javascript alert("XMLHttpRequest not supported"); return null; }; function dochange(src, val) { var req = Inint_AJAX(); req.onreadystatechange = function () { if (req.readyState==4) { if (req.status==200) { document.getElementById(src).innerHTML=req.responseText; } } }; req.open("GET", "add_mattertype_firmb.php?data="+src+"&amp;val="+val); req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8"); // set Header req.send(null); } window.onLoad=dochange('categoria', -1); &lt;/script&gt; &lt;?php // Write out our query. $query = "SELECT * FROM mattertype ORDER by name"; // Execute it, or return the error message if there's a problem. $result = mysql_query($query) or die(mysql_error()); $dropdown = "&lt;select name='mattertype'&gt;"; while($row = mysql_fetch_assoc($result)) { $dropdown .= "\r\n&lt;option value='{$row['name']}'&gt;{$row['name']}&lt;/option&gt;"; } $dropdown .= "\r\n&lt;/select&gt;"; ?&gt; &lt;form method="post" align= "right"&gt; &lt;table &gt; &lt;tr&gt;&lt;td&gt;&amp;nbsp;&lt;/td&gt; &lt;tr&gt; &lt;td&gt;Select the Matter Type: &lt;/td&gt; &lt;td&gt;&lt;?php echo $dropdown; ?&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt;&lt;td&gt;&amp;nbsp;&lt;/td&gt; &lt;tr&gt; &lt;td style="text-align: left; "&gt;Active &lt;/td&gt; &lt;td align="left" &gt; &lt;input type="checkbox" name="active" value="1" /&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt;&lt;td&gt;&amp;nbsp;&lt;/td&gt; &lt;tr&gt; &lt;td&gt;&amp;nbsp;&lt;/td&gt; &lt;td&gt;&lt;input type="submit" name="submit" value="Add" align= "right" /&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;?php print "&lt;pre&gt;"; print_r($_POST); print "&lt;/pre&gt;"; if (isset($_POST['submit'])) { // Connect to the database $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); $firm = $_POST['categoria']; $hub = $_POST['subcategoria']; $area = $_POST['subcategoria2']; $mattertype = $_POST['mattertype']; $firm=trim($_POST['firm']) ; $active=trim($_POST['active']); $mistakes = array(); if (empty($name) || (!(ctype_alpha($name)))) { $mistakes[] = 'ERROR - Your title is either empty or should only contain ALPHABET CHARACTERS.'; } else { // accept title and sanitize it $name = mysql_real_escape_string(stripslashes($_POST['name'])); } if (sizeof($mistakes) &gt; 0) { echo "&lt;ul&gt;"; foreach ($mistakes as $errors) { echo "&lt;li&gt;$errors&lt;/li&gt;"; //echo "&lt;a href='areatitle_index.php'&gt;Back...&lt;/a&gt;"; } echo "&lt;/ul&gt;"; echo '&lt;br /&gt;'; } else { $sql = "INSERT INTO `firm_mattertype`(firm, hub, area, mattertype, active) VALUES ('$firm', '$hub', '$area', '$mattertype', '$active')"; echo $sql; $result = mysql_query($sql); if(!$result) { echo "Saved."; //echo "&lt;BR&gt;"; } else{ echo "ERROR - The same Title already exist in the database"; } } } ?&gt; &lt;/form&gt; &lt;table class="table2" &gt; &lt;br&gt; &lt;tr&gt; &lt;th&gt; Firm &lt;/th&gt; &lt;th&gt; Hub &lt;/th&gt; &lt;th&gt; Area &lt;/th&gt; &lt;th&gt; MatterType &lt;/th&gt; &lt;th&gt; Active &lt;/th&gt; &lt;th colspan = '1' &gt; &lt;/th&gt; &lt;/tr&gt; &lt;?php // Connect to the database $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); $result=mysql_query("SELECT * FROM firm_mattertype ORDER BY firm"); while($test = mysql_fetch_array($result)) { $id = $test['id']; echo "&lt;tr align='center'&gt;"; //echo"&lt;td&gt;&lt;font color='black'&gt;" .$test['id']."&lt;/font&gt;&lt;/td&gt;"; echo"&lt;td&gt;&lt;font color='black'&gt;" .$test['firm']."&lt;/font&gt;&lt;/td&gt;"; echo"&lt;td&gt;&lt;font color='black'&gt;" .$test['hub']."&lt;/font&gt;&lt;/td&gt;"; echo"&lt;td&gt;&lt;font color='black'&gt;" .$test['area']."&lt;/font&gt;&lt;/td&gt;"; echo"&lt;td&gt;&lt;font color='black'&gt;" .$test['mattertype']."&lt;/font&gt;&lt;/td&gt;"; echo"&lt;td&gt;&lt;font color='black'&gt;" .$test['active']."&lt;/font&gt;&lt;/td&gt;"; echo"&lt;td&gt; &lt;a href ='area_view.php?id=$id'&gt;Edit&lt;/a&gt;"; echo "&lt;/tr&gt;"; } // close connection mysql_close(); ?&gt; &lt;/table&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload