Page 1 of 1

php/javascript passing values to txtboxes

Posted: Mon Dec 19, 2011 9:38 pm
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.

Re: php/javascript passing values to txtboxes

Posted: Mon Dec 19, 2011 10:25 pm
by Gopesh
HI,Pls post the code for other form fields also..

Re: php/javascript passing values to txtboxes

Posted: Mon Dec 19, 2011 10:53 pm
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>

Re: php/javascript passing values to txtboxes

Posted: Wed Dec 21, 2011 2:28 am
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.

Re: php/javascript passing values to txtboxes

Posted: Wed Dec 21, 2011 11:21 am
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.

Re: php/javascript passing values to txtboxes

Posted: Wed Dec 21, 2011 10:30 pm
by Gopesh
ok...Try it..Good Luck