Courtney
Courtney

Reputation: 5

How to make 2 drop downs work with links to other pages in PHP/HTML?

I am still a beginner with PHP. I am having a problem with drop downs and links. I want to be able to choose an option in the drop down menu and it go to the specified page. For some reason only the links for the second drop down menu. (MUNICIPALITIES) will work correctly by linking to the specific municipality page. The crime drop down menu will not link to the specific crime page. I have included the code for the crime drop down and municipality drop down. I have also included the script below.

Ultimately my question is why aren't both drop downs functioning correctly with the link?

<script language="JavaScript" type="text/javascript">

    function gopage(theLink) {
        if (document.dropdown.theLink.value != "") {
            location.href = document.dropdown.theLink.value;
        }
    }

</script>

<form name="dropdown">
    <select name="theLink" onchange="gopage(theLink)">
        <option value="ALL">Choose Crime associated with the Gang</option>
        <?php
        //echo 'NOWWWWWWWWWWWWWWWWWWWWWW';
        if ($length3 <> 0) {
            for ($m = 0; $m < $length3; $m++) {
                $rows = $resultset3[$m][crime_name];
                $trackchoices = $rows;

                $options2 = "<option value=\"crimesmain.php?crime=$trackchoices\">$trackchoices</option>";
                echo "$options2";
            }
        }
        else if ($length3 == 0) {
            $trackanswer = "NO CRIMES";
            $options5 = "<option value=\"$trackanswer\">$trackanswer</option>";
            echo "$options5";
        }
        ?>
    </select>
</form>

<br>
<br>

<form name="dropdown">
    <select name="theLink" onchange="gopage(this)"> 
        <option value="ALL">Choose Municipality associated with the Gang</option>
        <?php
        if ($length12 <> 0) {
            for ($q = 0; $q < $length12; $q++) {
                $rows2 = $resultset6[$q][municipality_name];
                $trackchoices2 = $rows2;
                //try

                $options3 = "<option value=\"municipalitymain.php?mun=$trackchoices2\">$trackchoices2</option>";
                //echo "<a href='municipalitymain.php?mun=$options3'>";
                echo "$options3";
            }
        }
        else if ($length12 == 0) {
            $trackanswer = "NO MUNICIPALITY";
            $options6 = "<option value=\"$trackanswer\">$trackanswer</option>";
            echo "$options6";
        }
        ?>
    </select>
</form>

Upvotes: 0

Views: 208

Answers (1)

boug
boug

Reputation: 1877

I think a better solution would be to pass the id of the select element to gopage so your function looks like this

function gopage(elId)
{
   if (document.getElementById(elId).value != "") {
        location.href = document.getElementById(elId).value;
    }
}

First Dropdown

<select name="theLink" id="crime" onchange="gopage('crime')">

Second Dropdown

<select name="theLink2" id="muni" onchange="gopage('muni')">

Upvotes: 1

Related Questions