简体   繁体   中英

How to change dropdown menu depending on table mysql

Hello all: i have 2 dropdowns list that populate themselves from two different TABLES. The first TABLE (ID|Project_Name|isActive). And the second TABLE is for users (Project_Id|Name) I want to show the options from the second dropdown according on what is selected from the first one. For example in the first TABLE i have 3 Projects: 1:Project1 2:Project2 3:Project3

And on the second TABLE which is for users: The user has an id that depends on the project from first TABLE: User A works on Project1 User B works on Project1 User C works on Project2

<html>
<body>
<script type="text/javascript">
function changeProject()
{
alert(document.getElementById("ProjOptionId_id").value);
}
</script>
<form method="POST" action="page.php">
<table>
<tr>
<td>
<?php Insertprojects(); ?>
</td>
</tr>
<tr>
<td>
<?php InsertUsers(); ?>
</td>
</tr>
<form>
</body>
</html>

<?php
function Insertprojects()
{
$Search ="SELECT * FROM proj_database";
$query= mysql_query($Search);
echo "<select name=\"ProjOptionId\" id=\"ProjOptionId_id\" onchange=\"changeProject()\">";
$bFirstLoop=0;
while($Row = mysql_fetch_array($query))
echo "<option value=\"$Row[ID]\">$Row[Project_Name]</option>";
if($bFirstLoop==0)
{
$GLOBALS['sProjectId'] = $Row['ID'];
}
$bFirstLoop++;
}
echo "</select>";
}
?>

I made the javascript function to get the index of the dropdown, so i can change the second dropdown depending on the element selected.

How can i do when you select Project1 on first dropdown, to appear in second dropdown the users that works only with Project1 and so on without reloading the page.

I manage to solve this by jQuery:

$(function() 
{
$("#projectSelector").change(function() 
{
$("#customerSelector").load("getter.php?project_id=" + $("#projectSelector").val());
});
});

Where this is my first dropdown:

<select id="projectSelector" name="projectSelector">
<option selected value="base">--Select an option--</option>
<?php
/*Code to populate from Mysql query
SELECT* FROM......*/
?>
</select>

and the second dropdown that takes the value according on what is selected from first one

<select id="customerSelector">
<option>--Select an Option--</option>
</select>

and the getter.php is an external files that contains a varible to use when the dropdown value changes:

<?php
$sProjectId = $_GET['project_id'];
mysql_query = SELECT * FROM database1 WHERE tablecolum = $sProjectId;
while ($row = mysql_fetch_array($result)
{
echo "<option>".$row['tablecolumn']."</option>";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM