/    Sign up×
Community /Pin to ProfileBookmark

3 level dynamic mysql dropdown

I’m trying to make 3 drop downs (based off of mysql tables), to narrow down the selection. Source, School, and Program.

So when when they select SOURCE(source), all the SCHOOLs in drop2 will be based on the SOURCE selected in the previous dropdown, and whatever school(cid) is selected, the program will be based off that school(cid) and display all the programs for that school.

i’ve google’d high and low for something, and no avail.

here is what i have, i am just trying to make 3 drop downs narrow in, without pressing submit (im new to ajax and javascript etc) The issue may be in that java part, i’ve look in the php and mysql, but nothing. I could be missing something, thank you!

[code=php]
<table width=”517″ border=”1″>
<tr>
<td width=”87″>Source:</td>
<td width=”414″><? echo'<select name=”source” onChange=”showUser(this.value)”><option value=””>–Select–</option><?php while() { } ?>’;

$res=mysql_query(“SELECT * FROM `sm_source`”);
if(mysql_num_rows($res)==0) echo “there is no data in table..”;
else
while ($row=mysql_fetch_assoc($res)) {
echo”<option value=” . $row[‘id’] . “>” . $row[‘name’] . “-” . $row[‘id’] . “</option>”;
}
echo'</select>’ ?></td>
</tr>
<tr>
<td>School:</td>
<td><? echo'<select name=”school” onChange=”showUser(this.value)”><option value=””>–Select–</option><?php while() { } ?>’;

$res=mysql_query(“SELECT * FROM sm_school WHERE status = ‘LIVE’ AND source = ‘”.$source.”‘ ORDER by name, cid”);
if(mysql_num_rows($res)==0) echo “there is no data in table..”;
else
while ($row=mysql_fetch_assoc($res)) {
echo”<option value=” . $row[‘cid’] . “>” . $row[‘name’] . “-” . $row[‘cid’] . “</option>”;
}
echo'</select>’ ?></td>
</tr>
<tr>
<td>Program:</td>
<td><? echo'<select name=”sub” onChange=”showUser(this.value)”><option value=””>–Select sub–</option><?php while() { } ?>’;

$res=mysql_query(“SELECT * FROM `sm_sub` WHERE status=’LIVE’ AND source = ‘”.$source.”‘ AND cid = ‘”.$cid.”‘ ORDER by name, cid”);
if(mysql_num_rows($res)==0) echo “there is no data in table..”;
else
while ($row=mysql_fetch_assoc($res)) {
echo”<option value=” . $row[‘cid’] . “-” . $row[‘sub_id’] . “>” . $row[‘category’] . “-” . $row[‘cid’] . “</option>”;
}
echo'</select>’ ?></td>
</tr>
</table>
</form>[/code]

my javascript

[CODE]var xmlhttp;

function showUser(str)
{
xmlhttp=GetXmlHttpObject();
if (xmlhttp==null)
{
alert (“Browser does not support HTTP Request”);
return;
}
var url=”getuser.php”;
url=url+”?q=”+str;
url=url+”&sid=”+Math.random();
xmlhttp.onreadystatechange=stateChanged;
xmlhttp.open(“GET”,url,true);
xmlhttp.send(null);
}

function stateChanged()
{
if (xmlhttp.readyState==4)
{
document.getElementById(“txtHint”).innerHTML=xmlhttp.responseText;
}
}

function GetXmlHttpObject()
{
if (window.XMLHttpRequest)
{
// code for IE7+, Firefox, Chrome, Opera, Safari
return new XMLHttpRequest();
}
if (window.ActiveXObject)
{
// code for IE6, IE5
return new ActiveXObject(“Microsoft.XMLHTTP”);
}
return null;
}[/CODE]

and my php code

[code=php]<?php
$src=$_GET[“src”];
$school=$_GET[“school”];
$sub=$_GET[“sub”];

$con = mysql_connect(‘xx’, ‘xx’, ‘xx’);
if (!$con)
{
die(‘Could not connect: ‘ . mysql_error());
}

mysql_select_db(“dataclerk”, $con);

$sql=”SELECT * FROM `sm_sub` WHERE status=’LIVE’ AND source = ‘”.$source.”‘ AND cid = ‘”.$cid.”‘ ORDER by name, cid”;

$result = mysql_query($sql);

echo “<table border=’1’>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Hometown</th>
<th>Job</th>
</tr>”;

while($row = mysql_fetch_array($result))
{
echo “<tr>”;
echo “<td>” . $row[‘FirstName’] . “</td>”;
echo “<td>” . $row[‘LastName’] . “</td>”;
echo “<td>” . $row[‘Age’] . “</td>”;
echo “<td>” . $row[‘Hometown’] . “</td>”;
echo “<td>” . $row[‘Job’] . “</td>”;
echo “</tr>”;
}
echo “</table>”;

mysql_close($con);
?>[/code]

right now, the resutls display in a table, i dont want that – i nee dthem to display as results in the next dropdown, thank you.

to post a comment
JavaScript

1 Comments(s)

×

Success!

Help @1337hovie spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.26,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...