Hi,
I am trying to create a drop down menu which links to another dropdwon menu.
I can run a simple version of this from javascript I found on the the net and it works fine, like this,
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html dir="ltr" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Dynamic Select Boxes example</title>
<style type="text/css">
</style>
<script type="text/javascript">
function populate(o)
{
d=document.getElementById('de');
if(!d){return;}
var mitems=new Array();
mitems['topic1']=['subtopic1','subtopic2'];
mitems['topic2']=['subtopic3','subtopic4'];
d.options.length=0;
cur=mitems[o.options[o.selectedIndex].value];
if(!cur){return;}
d.options.length=cur.length;
for(var i=0;i<cur.length;i++)
{
d.options[i].text=cur[i];
d.options[i].value=cur[i];
}
}
</script>
</head>
</html>
I have a table of topics and subtopic which are linked by the âidâ field in the âtopicsâ table being the same as the âtopicidâ field in the âsubtopicsâ table. So each topic has subtopics associated with it.
I am trying to create a pulldown menu of topics which when clicked displays another pulldown menu of only the subtopics associated with that topic.
The lines to do that in the javascript above are,
mitems['topic1']=['subtopic1','subtopic2'];
mitems['topic2']=['subtopic3','subtopic4'];
But how do I adapt this for my purpose? The tables look like this for âtopicsâ
and for âsubtopicsâ
I build the $topics array like this,
<?php
include $_SERVER['DOCUMENT_ROOT'] . '/artgibney/includes/db.inc.php';
// Build the list of topic rows
try
{
$result = $pdo->query('SELECT id, topic FROM topics');
}
catch (PDOException $e)
{
$error = 'Error fetching list of topics.';
include 'error.html.php';
echo $e;
exit();
}
foreach ($result as $row)
{
$topics[] = array('id' => $row['id'], 'topic' => $row['topic']);
}
And the subtopics array like this,
<?php
include $_SERVER['DOCUMENT_ROOT'] . '/artgibney/includes/db.inc.php';
// Build the list of subtopics rows
try
{
$result = $pdo->query('SELECT id, subtopic, topicid FROM subtopics');
}
catch (PDOException $e)
{
$error = 'Error fetching list of subtopics.';
include 'error.html.php';
echo $e;
exit();
}
foreach ($result as $row)
{
$subtopics[] = array('id' => $row['id'], 'subtopic' => $row['subtopic'], 'topicid' => $row['topicid']);
}
The actually forms themselves currently look like this,
<td>
<select name="topic" id="topic">
<?php foreach($topics as $topical):
$value = $topical['topic'];
$selected = ($topic == $value) ? 'selected' : '';
echo "<option value='$value' $selected>$value</option>";
endforeach; ?>
</select>
</td>
<td>
<select name="subtopic" id="subtopic">
<?php foreach($subtopics as $subtopical):
$value = $subtopical['subtopic'];
$selected = ($subtopic == $value) ? 'selected' : '';
echo "<option value='$value' $selected>$value</option>";
endforeach; ?>
</select>
</td>
So when the topic âMechanicsâ is chosen only âMomentumâ should show in the subtopic menu.
Another issue with this is that this form is used for both inputting new data and for editing previous data.
I am trying to keep it that way.
Any help would be greatly appreciated.
Thanks,
Shane