Hello, I hope this is posted in the correct forum. This is a mix of php and javascript and what I am trying to do combines the two so I hope this is the best place for this post.
I have 2 situations, one when adding an employee and the other is modifying the employee. I have a drop list the holds all of the companies that when you select a company, it changes the droplist of departments to show the available departments for the chosen company. So the dept droplist dynamically changes based off the company you choose. I have this working perfectly. THis is for adding an employee.
My problem I have now is I need to modify the employee and when you first pull up the form with the employee info, it is populated from a mysql query. The droplist for companies is selected to the correct company, however, the droplist for depts is not because the javascript code I have is set to fill the dept list on an onchange event which on page load does not exist.
If they select a different company, my code works fine but when they first hit the page, the dept list is blank because the onchange event has not been kicked off. I realize this is javascript but I am not sure where my changes need to be made.
below is my code for the companies and depts only. I will leave out the other employee info becasue it is irrelavent. I will post what I have so far and hopefully you can guide me in the right direction 😃
//here we query for all of the companies to show in a drop list
$query = "select * from company order by Name";
$result2 = @mysql_query($query);
//query for the employee info
$query = "select * from employees where ID = ".$HTTP_POST_VARS["employee"];
$result = @mysql_query($query);
$row = @mysql_fetch_array($result);
$value = "<SCRIPT LANGUAGE=\"JavaScript\" type=\"text/javascript\">
/*
Script by RoBorg
RoBorg@geniusbug.com
http://javascript.geniusbug.com
Please do not remove or edit this message
*/
//Start setup [Edit below here]
var d = new dynamicSelect();
d.addSelect('company');\n";
for ($i = 0; $i < @mysql_num_rows($result2); $i++)
{
//grab a company row
$company_row = @mysql_fetch_array($result2);
//setup our first company array
$value .= "d.selects['company'].addOption('".$company_row["ID"]."');\n";
//here I select all the departments for the company we are on and loop through
//setting up our array
$query = "select ID, Dept from departments where CompanyID = ".$company_row["ID"]." order by Dept";
$tempresult = @mysql_query($query);
//loop through all the depts for the current company and build our dynamic dept select list
for ($j = 0; $j < @mysql_num_rows($tempresult); $j++)
{
$dept_row = @mysql_fetch_array($tempresult);
$value .= "d.selects['company'].options['".$company_row["ID"]."'].createOption('".$dept_row["Dept"]."', '".$dept_row["ID"]."');\n";
}
}
$value .= "//End setup [Edit above here]
function dynamicSelect()
{
this.selects = new Array();
this.addSelect = function(name)
{
this.selects[name] = new selectObj();
}
this.updateOptions = function(source, target)
{
var form = source.form;
var target = form.elements[target];
var value = source.options[source.selectedIndex].value;
while(target.options.length) target.remove(0);
if(!this.selects[source.name].options[value])
{
//alert('Invalid selection.'); //For debugging while you set it up
return;
}
var data = this.selects[source.name].options[value].options;
for(var x=0; x<data.length; x++)
{
try
{
target.add(data[x]);
}
catch(e)
{
target.add(data[x], null);
}
}
target.selectedIndex = 0;
}
}
function selectObj()
{
this.options = new Array();
this.addOption = function(value)
{
this.options[value] = new optionObj();
}
}
function optionObj()
{
this.options = new Array();
this.createOption = function(name, value)
{
this.options[this.options.length] = new Option(name, value);
}
}
</script>
<form name=employees method=post action=../admin/employee_submit.php onSubmit=\"return TheFormCheck()\";>
<table width=650 align=center border=0>
<tr>
<td>
<b style=color:E3593B>*</b> Companies
</td>
</tr>
<tr>
<td>
<select name=company onchange=\"d.updateOptions(this, 'dept');\">
<option value=\"\">-- Companies --";
@mysql_data_seek($result2,0);
for ($i = 0; $i < @mysql_num_rows($result2); $i++)
{
$row2 = @mysql_fetch_array($result2);
if ($row2["ID"] == $row["CompanyID"])
$value .= "<option value=\"".$row2["ID"]."\" selected>".$row2["Name"];
if ($row2["ID"] <> $row["CompanyID"])
$value .= "<option value=\"".$row2["ID"]."\">".$row2["Name"];
}
$value .= "</select>
</td>
</tr>
<tr>
<td colspan=2>
<br>
</td>
</tr>
<tr>
<td>
<b style=color:E3593B>*</b> Department
</td>
</tr>
<tr>
<td>
<select name=dept>
</select>
</td>
</tr>
<tr>
<td>
<input type=submit value=submit>
</td>
</tr>
</table>
</form>
So again when they pull this page up, I would like the company list to hold what is in the database for that employee as well as the droplist for depts to hold all the depts for the selected company and the selected element in the dept list to be what is in the mysql database for the dept as well. And if they change the company, it continue to work as normal and repopulate the dept list with the new set of depts for the chosen company.
thank you for any help with this.