Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Noob: fill multiple form fields with a dynamic drop down menu

Status
Not open for further replies.

LesSjo

Technical User
Nov 23, 2009
7
US
I have searched various forums for this seemingly "basic" function with no avail. I posted in a javascript forum and it was suggested that I try AJAX for my solution.

Here is my scenario. I have a drop down menu listing a number of locations (101 to be exact). When a customer selects their location, the response needs to pull the information linked to that location from a mySQL database and auto-fill 6 text fields.

this is the javascript code I have so far.

Code:
function selectCompany(selObj) {
var id=<?PHP echo $row_franchise['id']; ?>;
var companyArray = new Array();
companyArray.id=new Array();

companyArray.id.company=<?PHP echo $row_franchise['company']; ?>;
companyArray.id.address=<?PHP echo $row_franchise['address']; ?>;
companyArray.id.city=<?PHP echo $row_franchise['city']; ?>;
companyArray.id.state=<?PHP echo $row_franchise['state']; ?>;
companyArray.id.zip=<?PHP echo $row_franchises['zip']; ?>;
companyArray.id.phone=<?PHP echo $row_franchise['phone']; ?>;

document.getElementById('company').value = companyArray[id][0];
document.getElementById('address').value = companyArray[id][1];
document.getElementById('city').value = companyArray[id][2];
document.getElementById('state').value = companyArray[id][3];
document.getElementById('zip').value = companyArray[id][4];
document.getElementById('telephone').value = companyArray[id][5];
}

The call to the function is coming from a dynamic drop down menu. The code is here:

Code:
<select name="franchise" id="franchise" onchange="selectCompany(this)">
<?php
do {  
?>
<option value="<?php echo $row_franchise['id']?>"<?php if (!(strcmp($row_franchise['id'], $row_franchise['id']))) {echo "selected=\"selected\"";} ?>><?php echo $row_franchise['company']?></option>
<?php
} while ($row_franchise = mysql_fetch_assoc($Franchises));
  $rows = mysql_num_rows($Franchises);
  if($rows > 0) {
      mysql_data_seek($Franchises, 0);
	  $row_franchise = mysql_fetch_assoc($Franchises);
  }
?>
</select>

This equates to a 101 line menu when viewed in source code from the browser.

I am looking for someone to point me in the right direction.

Thanks in advance

Thanks,
Lester
 
Hi

First I would take a look at the JSON used in thread216-1569757 and would rewrite your code. ( Note that is pointless to instantiate companyArray as [tt]Array[/tt] because you are not using it as such. With the same effort you could instantiate it as [tt]Date[/tt] too. )

After a rewrite will be more clear what you have to modify for an AJAX solution. Will help with that if needed, but I have no time to review your current ugly code. ( That is why I not jumped in in your original thread216-1585634. )

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top