I'm not (as I am certain you'll notice below) a programmer or coder. I write network documentation manuals.
That said, I do have some familiarity with HTML, php, css, and so forth. But what follows has stumped me utterly and completely:
I am attempting to build a site for a friend of mine who is in a bind, and though everything else is going fine, I do have to redirect the end-user to a webpage built specifically for their community.
What I need is a way for the end-user to click on a dropdown menu which lists (through a MySQL query) all of the states where records are available, which, upon their selection of an option within the first menu, a second menu is populated by (again by way of MySQL) the communities within that state.
Though I've scoured the internet for things that might be useful, I find that I am simply not well-equipped for the logic of coding. I did find, however, one site that shows a basic version of what I am trying to do (see here), but I can't seem to get it to work with MySQL. This is where I am so far:
script --
<script>
function swapOptions(ArrayName) {
var ExSelect = document.theForm.sites;
var theArray = eval(ArrayName);
setOptionText(ExSelect, theArray);
}
function setOptionText(theSelect, theArray) {
for (loop = 0; loop < theSelect.options.length; loop++) {
theSelect.options[loop].text = theArray[loop];
}
}
</script>
and HTML --
<form name="theForm">
<select name="chooseCat" onchange="swapOptions(this.options[selectedIndex].text);">
<option value="">Please select your state ...</option>'
<?php
$query = mysql_query("SELECT DISTINCT state FROM sites ORDER BY state")
or die(mysql_error());
while ($result = mysql_fetch_assoc($query)) {
$stateChoice = $result['state'];
echo '<option value="';
echo "$stateChoice";
echo '">';
echo "$stateChoice";
echo '</option>';
echo '<br />';
}
?>
</select>
<select name="sites" style="min-width: 100px;" onchange="location.href='reports.php?page=' + this.options[this.selectedIndex].value;">
<?php
$query = mysql_query("SELECT * FROM sites")
or die(mysql_error());
while ($result = mysql_fetch_assoc($query)) {
echo '<option value="';
echo '">';
echo $result['longname'];
echo '</option>';
echo '<br />';
}
?>
</select>
</form>
This returns two dropdowns. The first with the results of my query for the states available, listed alphabetically in the dropdown. The second has the long-names of all of the sites listed, but for all of the sites in the table, not just the ones within a certain state (the state just chosen).
In the end, dropdown 1 should be populated with all of the available states. Once the user selects their state, this should trigger the population of dropdown 2 with all of the communities within their chosen state. Once the choice of community is clicked in dropdown 2, it should redirect the user's browser to, for example, http://www.webpage.com/reports.php?page=communityNameGoesHere ...
Thanks so much for any advice you might have here :)
echowhen 2 times is enough? You may use concatenation.echos withecho "<option value=\"$stateChoice\">$stateChoice</option><br />";.