<form action="maintest2.php" method="POST"> <table border="0" cellpadding="2" width="100%"> <tr> <td><b>Main Category:</b></td> <td> <select name="Name"> <option value="0">Select Country <?php include 'config.php'; include 'opendb.php'; $cate = mysql_result($result, 0); $result=mysql_query("SELECT c.`Name` FROM clientconfiguration.country c ORDER BY `Name`") or exit (mysql_error()); while ($row = mysql_fetch_assoc($result)) { echo '<option value="' . $row['Name'] . '"'; if ($row['Name'] == $cate) {echo ' selected';} echo '>' . $row['Name']; } include 'closedb.php'; ?> </select> </td> </tr> <tr><td colspan=2><input type="submit" name="submit" value="submit"></td></tr> </table> </form>
<select name="Name" id="firstname"> <option value="0">Select Country $cate = mysql_result($result, 0); $result=mysql_query("SELECT c.`Name` FROM clientconfiguration.country c ORDER BY `Name`") or exit (mysql_error()); while ($row = mysql_fetch_assoc($result)) { echo '<option value="' . $row['Name'] . '" id="first" onchange="doSecond()'; if ($row['Name'] == $cate) {echo ' selected';} echo '>' . $row['Name']; } <div id="second"> The HTML setup up here will depend upon how you are going to display the dropdown boxes but I have just setup a DIV to illustrate what you can do. </div> etc etc etc
<script language="javascript" type="text/javascript"> function getHTTPObject(){ if (window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP"); else if (window.XMLHttpRequest) return new XMLHttpRequest(); else { alert("Your browser does not support AJAX."); return null; } } function setSecond(){ if(httpObject.readyState == 4){ document.getElementById('second').innerHTML = httpObject.responseText; **** This sets up the new HTML in the DIV where the ID = second **** } } function doSecond(){ **** This function takes the value from your first dropdown which I gave the ID of 'firstname' (check the code above) and passes this to the PHP program which will create the new HTML code **** httpObject = getHTTPObject(); if (httpObject != null) { var testing = httpObject.open("GET", "seconddrop.php?firstname=" + document.getElementById('firstname').value, true); httpObject.send(null); httpObject.onreadystatechange = function() { setSecond(); } **** the above line looks strange but it forces the program to wait till there is a return from the PHP program before it them tries to create the new HTML **** } } var httpObject = null; </script>
$name = $_GET['firstname']; $returncode = '<select name="second" id="second">'; SELECT cl.`Name` FROM `client` cl, country co WHERE co.`Country ID` = cl.`Country ID` AND co.`Name` = '$firstname'; Obviously use proper code for the SQL statement im just doing this quickly. (mysql_error()); while ($row = mysql_fetch_assoc($result)) { $returncode .= '<option value="' . $row['Name'] . '"'; if ($row['Name'] == $cate) { $returncode .= ' selected'; } $returncode .= '>' . $row['Name']; } $returncode .= '</select>'; echo $returncode;
<? echo "<form name=sel>\n"; echo "Country : <font id=country><select>\n"; echo "<option value='0'>============</option> \n" ; echo "</select></font>\n"; echo "Client : <font id=client><select>\n"; echo "<option value='0'>=== none ===</option> \n" ; echo "</select></font>\n"; ?> <script language=Javascript> function Inint_AJAX() { try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch(e) {} try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {} try { return new XMLHttpRequest(); } catch(e) {} 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", "country.php?data="+src+"&val="+val); req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=iso-8859-1"); req.send(null); } window.onLoad=dochange('country', -1); </script>
<? header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); header ("Cache-Control: no-cache, must-revalidate"); header ("Pragma: no-cache"); header("content-type: application/x-javascript; charset=tis-620"); $data=$_GET['data']; $val=$_GET['val']; include 'config.php'; include 'opendb.php'; if ($data=='country') { echo "<select name='country' onChange=\"dochange('client', this.value)\">\n"; echo "<option value='0'>==== choose state ====</option>\n"; $result=mysql_query("SELECT c.`Country ID`, c.`Name` FROM clientconfiguration.country c ORDER BY `Name`") or exit(mysql_error()); while(list($id, $name)=mysql_fetch_array($result)){ echo "<option value=\"$id\" >$name</option> \n" ; } } else if ($data=='client') { echo "<select name='client' >\n"; echo "<option value='0'>====choose client ====</option>\n"; $result=mysql_query("SELECT cl.`Client ID`, cl.`Name` FROM `client` cl, country co WHERE cl.`Country ID` = co.`Country ID` AND cl.`Country ID` = '$val'") or exit(mysql_error()); while(list($id, $name)=mysql_fetch_array($result)){ echo "<option value=\"$id\" >$name</option> \n" ; } } echo "</select>\n"; ?>
<? echo "<form name=sel>\n"; echo "Country : <font id=country><select>\n"; echo "<option value='0'>============</option> \n" ; echo "</select></font>\n"; echo "Client : <font id=client><select>\n"; echo "<option value='0'>=== none ===</option> \n" ; echo "</select></font>\n"; echo "Environment : <font id=environment><select>\n"; echo "<option value='0'>=== none ===</option> \n" ; echo "</select></font>\n"; echo "System : <font id=system><select>\n"; echo "<option value='0'>=== none ===</option> \n" ; echo "</select></font>\n"; ?> <script language=Javascript> function Inint_AJAX() { try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch(e) {} try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {} try { return new XMLHttpRequest(); } catch(e) {} 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", "country.php?data="+src+"&val="+val); req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=iso-8859-1"); req.send(null); } window.onLoad=dochange('country', -1); </script>
<? header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); header ("Cache-Control: no-cache, must-revalidate"); header ("Pragma: no-cache"); header("content-type: application/x-javascript; charset=tis-620"); $data=$_GET['data']; $val=$_GET['val']; include 'config.php'; include 'opendb.php'; if ($data=='country') { echo "<select name='country' onChange=\"dochange('client', this.value), dochange('environment', this.value), dochange('system', this.value)\">\n"; echo "<option value='0'>====State====</option>\n"; $result=mysql_query("SELECT c.`Country ID`, c.`Name` FROM clientconfiguration.country c ORDER BY `Name`") or exit (mysql_error()); while(list($id, $name)=mysql_fetch_array($result)){ echo "<option value=\"$id\" >$name</option> \n" ; } } else if ($data=='client') { echo "<select name='client' onChange=\"dochange('environment', this.value), dochange('system', this.value)\">\n"; echo "<option value='0'>====Client====</option>\n"; $result=mysql_query("SELECT cl.`Client ID`, cl.`Name` FROM `client` cl, country co WHERE cl.`Country ID` = co.`Country ID` AND cl.`Country ID` = '$val' ORDER BY cl.`Name`") or exit(mysql_error()); while(list($id, $name)=mysql_fetch_array($result)){ echo "<option value=\"$id\" >$name</option> \n" ; } } else if ($data=='environment') { echo "<select name='environment' onChange=\"dochange('system', this.value)\">\n"; echo "<option value='0'>===Environment===</option>\n"; $result=mysql_query("SELECT en.`Environment ID`, env.`Type` FROM `environment` en, `client` cl, `environmenttype` env WHERE cl.`Client ID` = en.`Client ID` AND env.`ID` = en.`Type` AND en.`Client ID` = '$val'") or exit(mysql_error()); while(list($id, $name)=mysql_fetch_array($result)){ echo "<option value=\"$id\" >$name</option> \n" ; } } else if ($data=='system') { echo "<select name='system' >\n"; echo "<option value='0'>===System===</option>\n"; $result=mysql_query("SELECT sy.`System ID`, sy.`Label` FROM system sy, environment en WHERE en.`Environment ID` = sy.`Environment ID` AND sy.`Environment ID` = '$val'") or exit(mysql_error()); while(list($id, $name)=mysql_fetch_array($result)){ echo "<option value=\"$id\" >$name</option> \n" ; } } echo "</select>\n"; ?>
| DaniWeb Message | |
| Cancel Changes | |