/    Sign up×
Community /Pin to ProfileBookmark

need help auto populating form fields

hello i’m a noob to all this and i was wondering how to automatically populate form fields based on what a user chooses in a combo box. what i would like is when a user chooses a branch from the combo box, the phone and fax numbers pertaining to that particular branch are automatically displayed in the corresponding text field. the data behind the combo box and the 2 fields i want populated all come from the same table in a mysql database. any help in this is much appreciated, thanks! here is my code so far:

[code=php]<?php require_once(‘../Connections/Directory.php’); ?>
<?php
mysql_select_db($database_Directory, $Directory);
$query_rsBranches = “SELECT * FROM branches”;
$rsBranches = mysql_query($query_rsBranches, $Directory) or die(mysql_error());
$row_rsBranches = mysql_fetch_assoc($rsBranches);
$totalRows_rsBranches = mysql_num_rows($rsBranches);
?>
<html>
<head>
<title>help me</title>
<meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″>
</head>
<body leftmargin=”0″ topmargin=”0″ marginwidth=”0″ marginheight=”0″>

<table width=”600″ border=”1″ align=”center” cellpadding=”5″ bordercolor=”#BFC3EB” bgcolor=”#FFFFFF”>
<tr>
<td class=”h1″><p>&nbsp;</p>
<form action=”” method=”post” name=”dirInsert” id=”dirInsert”>
<table width=”100%” border=”0″ cellspacing=”0″ cellpadding=”0″>
<tr>
<td width=”10%”>Last Name</td>
<td width=”25%”><input name=”txtLast” type=”text” id=”txtLast2″></td>
<td width=”12%”>&nbsp;</td>
<td width=”41%”>&nbsp;</td>
<td width=”2%”>&nbsp;</td>
<td width=”10%”>&nbsp;</td>
</tr>
<tr>
<td>First Name</td>
<td><input name=”txtFirst” type=”text” id=”txtFirst”></td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>Branch</td>
<td><select name=”selectBranch” id=”selectBranch”>
<?php
do {
?>
<option value=”<?php echo $row_rsBranches[‘branch_name’]?>”><?php echo $row_rsBranches[‘branch_name’]?></option>
<?php
} while ($row_rsBranches = mysql_fetch_assoc($rsBranches));
$rows = mysql_num_rows($rsBranches);
if($rows > 0) {
mysql_data_seek($rsBranches, 0);
$row_rsBranches = mysql_fetch_assoc($rsBranches);
}
?>
</select> </td>
<td>Phone</td>
<td><input name=”txtPhone” type=”text” id=”txtPhone”></td>
<td>Fax</td>
<td><input name=”txtFax” type=”text” id=”txtFax”></td>
</tr>
<tr>
<td>Title</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp; </td>
<td>&nbsp;</td>
<td> </td>
<td>&nbsp;</td>
<td>&nbsp; </td>
</tr>
</table>
</form>
<p>&nbsp;</p>
<p align=”right”>&nbsp; </p></td>
</tr>
</table>
<div align=”right”><br>
<br>
<br>
<br>
<br>
</div>
</body>
</html>
<?php
mysql_free_result($rsBranches);
?> [/code]

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@squallflightAug 06.2004 — You'll have to submit the form to get it working

hows this:
[code=php]
<?php require_once('../Connections/Directory.php'); ?>
<?php
mysql_select_db($database_Directory, $Directory);
$query_rsBranches = "SELECT * FROM branches";
$rsBranches = mysql_query($query_rsBranches, $Directory) or die(mysql_error());
$row_rsBranches = mysql_fetch_assoc($rsBranches);
$totalRows_rsBranches = mysql_num_rows($rsBranches);
?>
<html>
<head>
<title>help me</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">

<table width="600" border="1" align="center" cellpadding="5" bordercolor="#BFC3EB" bgcolor="#FFFFFF">
<tr>
<td class="h1"><p>&nbsp;</p>
<form action="<?php echo $PHP_SELF; ?>" method="post" name="dirInsert" id="dirInsert">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="10%">Last Name</td>
<td width="25%"><input name="txtLast" type="text" id="txtLast2"></td>
<td width="12%">&nbsp;</td>
<td width="41%">&nbsp;</td>
<td width="2%">&nbsp;</td>
<td width="10%">&nbsp;</td>
</tr>
<tr>
<td>First Name</td>
<td><input name="txtFirst" type="text" id="txtFirst"></td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>Branch</td>
<td><select name="selectBranch" id="selectBranch" onChange="form.submit()">
<?php
do {

if($row_rsBranches['branch_name'] == $_POST['selectBranch'])
{
?>
<option value="<?php echo $row_rsBranches['branch_name']?>" selected><?php echo $row_rsBranches['branch_name']?></option>
<?php
}
else
{
?>
<option value="<?php echo $row_rsBranches['branch_name']?>"><?php echo $row_rsBranches['branch_name']?></option>
<?php
}
} while ($row_rsBranches = mysql_fetch_assoc($rsBranches));
$rows = mysql_num_rows($rsBranches);
if($rows > 0) {
mysql_data_seek($rsBranches, 0);
$row_rsBranches = mysql_fetch_assoc($rsBranches);
}
?>
</select> </td>
<?php
//lets see if a branch has been selected
if(isset($_POST['selectBranch']))
{
$branch = $_POST['selectBranch'];
// get the data needed
$query_numbers = "SELECT * FROM branches WHERE branch_name = '$branch' LIMIT 1";
$result_numbers = mysql_query($query_numbers, $Directory) or die(mysql_error());
//lets see if there was a match
if(mysql_num_rows($result_numbers) > 0)
{
//we have a match lets get the array
$row_numbers = mysql_fetch_assoc($result_numbers);
//set the value of the fields
$txtPhone = $row_numbers['Phone_no'];
$txtFax = $row_numbers['Fax_no'];
}
}
else
{
//not submitted - default values
$txtPhone = 'Select a branch';
$txtFax = 'Select a branch';
}
?>
<td>Phone</td>
<td><input name="txtPhone" type="text" id="txtPhone" value="<?php echo $txtPhone; ?>"></td>
<td>Fax</td>
<td><input name="txtFax" type="text" id="txtFax" value="<?php echo $txtFax; ?>"></td>
</tr>
<tr>
<td>Title</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp; </td>
<td>&nbsp;</td>
<td> </td>
<td>&nbsp;</td>
<td>&nbsp; </td>
</tr>
</table>
</form>
<p>&nbsp;</p>
<p align="right">&nbsp; </p></td>
</tr>
</table>
<div align="right"><br>
<br>
<br>
<br>
<br>
</div>
</body>
</html>
<?php
mysql_free_result($rsBranches);
?>
[/code]

basically it now uses the onChange command to submit the form and uses the _POST array to determine what to call from the db. it then displays that and sets the initial value of the select list to that from the _POST array
×

Success!

Help @calisuave spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.6,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...