Hi all, I'm new to php and mysql, and have a question that is puzzling me:
I have 2 tables in a database, staff and locations. On my staff form, I have a (as of now, empty) listbox, where I'd like to pull down and select which location the staff is at. Once the location is selected, I'd like to have a bunch of fields populate with the address info.
Could someone please guide me?
Thanks in advance,
Jason
Here's code from the page in question:
($table is staff & $table2 is locations)
<?
include ("include/header.inc.php");
?>
<h1>Edit / add address book entry</h1>
<?php
include("include/dbconnect.php");
if($submit)
{
$sql = "INSERT INTO $table (firstname, lastname, location, address1, address2, city, province, postal, office, ext, fax, cell) VALUES ('$firstname', '$lastname', '$location', '$address1', '$address2', '$city', '$province', '$postal', '$office', '$ext', '$fax', '$cell')";
$result = mysql_query($sql);
echo "<br><br>Information entered into address book.\n";
}
else if($update)
{
$sql = "UPDATE $table SET firstname='$firstname',lastname='$lastname',location='$location',address1='$address1',address2='$address2',city='$city',province='$province',postal='$postal',office='$office',ext='$ext', fax='$fax',cell='$cell' WHERE id=$id";
$result = mysql_query($sql);
echo "<br><br>Address book updated.\n";
}
else if($id)
{
$result = mysql_query("SELECT * FROM $table WHERE id=$id",$db);
$myrow = mysql_fetch_array($result);
?>
<form method="post" action="edit.php">
<table width="380" border="0" cellspacing="1" cellpadding="1">
<tr>
<td>
<input type="hidden" name="id" value="<?php echo $myrow["id"]?>">
Firstname: </td>
<td>
<input type="Text" name="firstname" size="35" value="<?php echo $myrow["firstname"]?>"> </td>
</tr>
<tr>
<td>Lastname:</td>
<td>
<input type="Text" name="lastname" size="35" value="<?php echo $myrow["lastname"]?>"> </td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td>Location:</td>
<td><label>
<select name="select">
</select>
</label></td>
</tr>
<tr>
<td>Location Code:</td>
<td><input type="text" name="location" size="4" maxlength="4" value="<?php echo $myrow["location"]?>" /></td>
</tr>
<tr>
<td>Address 1: </td>
<td>
<input type="text" name="address1" size="35" value="<?php echo $myrow["address1"]?>"> </td>
</tr>
<tr>
<td>Address 2: </td>
<td>
<input type="text" name="address2" size="35" value="<?php echo $myrow["address2"]?>"> </td>
</tr>
<tr>
<td>City:</td>
<td><input type="text" name="city" size="35" value="<?php echo $myrow["city"]?>" /></td>
</tr>
<tr>
<td>Province:</td>
<td><select name="province" id="province">
<option value="<?php echo $myrow["province"]?>" selected><?php echo $myrow["province"]?></option>
<option value="AB">AB</option>
<option value="BC">BC</option>
<option value="MB">MB</option>
<option value="NB">NB</option>
<option value="NL">NL</option>
<option value="NS">NS</option>
<option value="ON">ON</option>
<option value="QC">QC</option>
<option value="SK">SK</option>
</select></td>
</tr>
<tr>
<td>Postal Code:</td>
<td><input type="text" name="postal" maxlength="6" size="6" value="<?php echo $myrow["postal"]?>" /></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td>Telephone:</td>
<td> </td>
</tr>
<tr>
<td>Office:</td>
<td>
<input type="Text" name="office" size="10" maxlength="10" value="<?php echo $myrow["office"]?>">
* Numbers Only </td>
</tr>
<tr>
<td>Extension:</td>
<td><input type="text" name="ext" size="5" maxlength="5" value="<?php echo $myrow["ext"]?>" />
* Numbers Only </td>
</tr>
<tr>
<td>Fax:</td>
<td>
<input type="Text" name="fax" size="10" maxlength="10" value="<?php echo $myrow["fax"]?>">
* Numbers Only </td>
</tr>
<tr>
<td>Cell:</td>
<td>
<input type="Text" name="cell" size="10" maxlength="10" value="<?php echo $myrow["cell"]?>">
* Numbers Only </td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
</table>
<input type="Submit" name="update" value="Update information">
</form>
<?
}
else
{
?>
<form method="post" action="<?php echo $PHP_SELF?>">
<table width="380" border="0" cellspacing="1" cellpadding="1">
<tr>
<td><input type="hidden" name="id" value="<?php echo $myrow["id"]?>" />
Firstname: </td>
<td><input type="text" name="firstname" size="35" value="<?php echo $myrow["firstname"]?>" />
</td>
</tr>
<tr>
<td>Lastname:</td>
<td><input type="text" name="lastname" size="35" value="<?php echo $myrow["lastname"]?>" />
</td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td>Location:</td>
<td><label>
<select name="select">
</select>
</label></td>
</tr>
<tr>
<td>Location Code:</td>
<td><input type="text" name="location" size="4" maxlength="4" value="<?php echo $myrow["location"]?>" /></td>
</tr>
<tr>
<td>Address 1: </td>
<td><input type="text" name="address1" size="35" value="<?php echo $myrow["address1"]?>" />
</td>
</tr>
<tr>
<td>Address 2: </td>
<td><input type="text" name="address2" size="35" value="<?php echo $myrow["address2"]?>" />
</td>
</tr>
<tr>
<td>City:</td>
<td><input type="text" name="city" size="35" value="<?php echo $myrow["city"]?>" /></td>
</tr>
<tr>
<td>Province:</td>
<td><select name="province" id="province">
<option value="<?php echo $myrow["province"]?>" selected="selected"><?php echo $myrow["province"]?></option>
<option value="AB">AB</option>
<option value="BC">BC</option>
<option value="MB">MB</option>
<option value="NB">NB</option>
<option value="NL">NL</option>
<option value="NS">NS</option>
<option value="ON">ON</option>
<option value="QC">QC</option>
<option value="SK">SK</option>
</select></td>
</tr>
<tr>
<td>Postal Code:</td>
<td><input type="text" name="postal" maxlength="6" size="6" value="<?php echo $myrow["postal"]?>" /></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td>Telephone:</td>
<td> </td>
</tr>
<tr>
<td>Office:</td>
<td><input type="text" name="office" size="10" maxlength="10" value="<?php echo $myrow["office"]?>" />
* Numbers Only </td>
</tr>
<tr>
<td>Extension:</td>
<td><input type="text" name="ext" size="5" maxlength="5" value="<?php echo $myrow["ext"]?>" />
* Numbers Only </td>
</tr>
<tr>
<td>Fax:</td>
<td><input type="text" name="fax" size="10" maxlength="10" value="<?php echo $myrow["fax"]?>" />
* Numbers Only </td>
</tr>
<tr>
<td>Cell:</td>
<td><input type="text" name="cell" size="10" maxlength="10" value="<?php echo $myrow["cell"]?>" />
* Numbers Only </td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
</table>
<input type="Submit" name="submit" value="Enter information">
</form>
<?
}
include ("include/footer.inc.php");
?>