php/javascript passing values to txtboxes

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
cjkeane
Forum Contributor
Posts: 217
Joined: Fri Jun 11, 2010 1:17 pm

php/javascript passing values to txtboxes

Post by cjkeane »

Hi everyone,

I need to select a companyname from a dropdownlist which in turn will populate the rest of the form fields. I'm not sure what I'm doing wrong.

Code: Select all

<script type="text/javascript">
	var compInfoArray = new Array();
	<?php
		$funderquery = "SELECT * FROM icbcagencylookup ORDER BY FunderName";
		$funderresult = mysql_query($funderquery) or die(mysql_error());
		// build javascript array
		while($funderrow=mysql_fetch_array($funderresult)){ 
			echo 'compInfoArray['.$funderrow['FunderName'].'] = new Array();';
			echo 'compInfoArray['.$funderrow['FunderName'].']["FunderTitle"] = "'.$funderrow['FunderTitle'].'";';	
			echo 'compInfoArray['.$funderrow['FunderName'].']["AgencyName"] = "'.$funderrow['AgencyName'].'";';
		}
	?>
	function showname() {
		var FunderName = document.form1.FunderName.value;
		document.form1.FunderTitle.value = compInfoArray[FunderName]["FunderTitle"];
		document.form1.FunderAgency.value = compInfoArray[FunderName]["AgencyName"];
	}
	window.onload=function() {
		showname();
	} 
</script>
and the dropdownlist code:

Code: Select all

<?php  
    $result=mysql_query("SELECT FunderName FROM icbcagencylookup order by FunderName ASC"); 
    $options = '';
         while ($row = mysql_fetch_assoc($result))  { 
               $selected = ($row['FunderName']==$FunderName) ? ' selected="selected"' : '';
                $options .= "<option value=\"{$row['FunderName']}\"{$selected}>{$row['FunderName']}</option>\n"; 
           } 
?>
<select name="FunderName" id="FunderName" onchange="showname()" >
     <option value="">< select > <?php echo $options ?></option>
</select>


the dropdown list finds the companynames (or rather the funders) but nothing happens when the name is changed in the list. Can anyone offer any ideas? Thanks.
Gopesh
Forum Contributor
Posts: 143
Joined: Fri Dec 24, 2010 12:48 am
Location: India

Re: php/javascript passing values to txtboxes

Post by Gopesh »

HI,Pls post the code for other form fields also..
cjkeane
Forum Contributor
Posts: 217
Joined: Fri Jun 11, 2010 1:17 pm

Re: php/javascript passing values to txtboxes

Post by cjkeane »

here's the entire form. I intend to populate all fields once the fundername is chosen from the list.

Code: Select all

<table width="100%" border="0" cellspacing="0" cellpadding="0">
        <tr>
          <td width="50%"><span class="text-align">Name</span>
              <?php  
    $result=mysql_query("SELECT FunderName FROM icbcagencylookup order by FunderName ASC"); 
    $options = '';
         while ($row = mysql_fetch_assoc($result))  { 
               $selected = ($row['FunderName']==$FunderName) ? ' selected="selected"' : '';
                $options .= "<option value=\"{$row['FunderName']}\"{$selected}>{$row['FunderName']}</option>\n"; 
           } 
?>
<select name="FunderName" id="FunderName" onchange="showname()" >
     <option value="">< select > <?php echo $options ?></option>
</select>
</td>
<td><span class="text-align">Address</span>
<input name="Funder_Address1" type="text" id="Funder_Address1" value="<?php echo $Funder_Address1; ?>" size="26" /></td>
</tr>
<tr>
   <td><span class="text-align">Title</span>
        <input name="FunderTitle" type="text" id="FunderTitle" size="26" />
   </td>
   <td><span class="text-align">City</span> <input name="Funder_City" type="text" id="Funder_City" value="<?php echo $Funder_City; ?>" size="26" />
</td>
</tr>
<tr>
  <td><span class="text-align">Agency</span>
       <input name="FunderAgency" type="text" id="FunderAgency" size="26" />
  </td>
   <td><span class="text-align">Province</span>
         <input name="Funder_Province" type="text" id="Funder_Province" value="<?php echo $Funder_Province; ?>" size="26" /></td>
        </tr>
        <tr>
          <td>&nbsp;</td>
          <td><span class="text-align">Postal Code</span>
            <input name="Funder_PostalCode" type="text" id="Funder_PostalCode" value="<?php echo $Funder_PostalCode; ?>" size="12" /></td>
        </tr>
      </table>
Gopesh
Forum Contributor
Posts: 143
Joined: Fri Dec 24, 2010 12:48 am
Location: India

Re: php/javascript passing values to txtboxes

Post by Gopesh »

Hi,Check this link http://stackoverflow.com/questions/3657 ... lect-value . Hope that it will suit to ur needs.Change the id of the input boxes according to ur form.Sorry for the delay in replying.
cjkeane
Forum Contributor
Posts: 217
Joined: Fri Jun 11, 2010 1:17 pm

Re: php/javascript passing values to txtboxes

Post by cjkeane »

Thanks for the reply. I 'can' use the id, however I'd prefer to use the companyname (or rather fundername) as the key as it is unique. i've had a look at the link you provided. i think it might accomplish what i need to do. I'll let you know if i still have difficulty.

thanks.
Gopesh
Forum Contributor
Posts: 143
Joined: Fri Dec 24, 2010 12:48 am
Location: India

Re: php/javascript passing values to txtboxes

Post by Gopesh »

ok...Try it..Good Luck
Post Reply