I've looked around and from what I can tell, the code seems right. Basically crating a JS array and passing PHP values to it to use in an onChange to fill a text field.

The dropdown gets populated fine but the text field does not autopopulate onChange. I'm not too fluent with JS but have a good analytical mind and from what I can see, the logic seems right.

Here is the code...any help would be appreciated.

<?php
	
		  		include('connectdb.php');
				$CENTRE = $_SESSION['centreAFB'];
    
                $qryFormList = mysql_query("SELECT * FROM tblLogin WHERE ACCT_TYPE = '2' AND loginCENTRE = '$CENTRE' ORDER BY loginNOM");
				
				echo "<script type='text/javascript'> var formCourriel = new Array()";
								
                while($resFormList = mysql_fetch_assoc($qryFormList))
                {
					echo "formCourriel[".$resFormList['loginID']."] = ".$resFormList['COURRIEL'].";";
	            }
					
				mysql_close($database);
				
				echo "
				
				function changed(sender)
				{
					document.getElementById('AFB_FORM_COURRIEL').value = formCourriel[sender];
				}
				
				";
				
				echo "</script>";
				
    			echo "<select name='AFBFORM' id='AFBFORM' onChange='changed(this.value)'>";
				
   		 		echo "<option selected disabled>Sélectionnez</option>";
				
                mysql_data_seek($qryFormList,0);
				while($resFormList = mysql_fetch_assoc($qryFormList))
				{
					echo '<option value="'.$resFormList['loginID'].'"';
								if($_SESSION['FORMDATA']['AFBFORM'] == "".$resFormList['loginID']."")
								{
									echo 'selected';
								}
								else
								{
									echo ' ';
								}

						echo '>'.$resFormList['loginNOM'].'</option>';
				}
				
   			 echo "</select>";
	
              ?>

Recommended Answers

All 7 Replies

where is the code for the text field?

You are getting that element by id: AFB_FORM_COURRIEL but I do not see that element in the code.

Is there a link we can see?

The field is immediately below the php script...

</td>
    <td width="128" class="menutd">Courriel<font color="red"><sup>*</font></sup></td>
    <td width="268" ><input name="AFB_FORM_COURRIEL" type="text" id="AFB_FORM_COURRIEL" size="30" value="<?php echo $_SESSION['FORMDATA']['AFBFORMCOURRIEL']; ?>" /></td>

what message comes up if you set the first line of your changed function to:

alert(sender);

Again, any chance on a link to see this? Makes it much easier.

Unfortunately this is on my development machine and not online.

I've added the line but nothing happens. I've checked that JS is enabled and other functions work fine.

This is my first time integrating JS within PHP tags...I must be missing something.

Here is the current code that's pertinent...

<td width="226" colspan="2">
    <?php
	
		  		include('connectdb.php');
				$CENTRE = $_SESSION['centreAFB'];
    
                $qryFormList = mysql_query("SELECT * FROM tblLogin WHERE ACCT_TYPE = '2' AND loginCENTRE = '$CENTRE' ORDER BY loginNOM");
				
				echo "<script type='text/javascript'> var formCourriel = new Array()";
								
                while($resFormList = mysql_fetch_assoc($qryFormList))
                {
					echo "formCourriel[".$resFormList['loginID']."] = ".$resFormList['COURRIEL'].";";
	            }
					
				mysql_close($database);
				
				echo "
				
				function changed(sender)
				{
					alert(sender);
					document.getElementById('AFB_FORM_COURRIEL').value = formCourriel[sender];
				}
				
				";
				
				echo "</script>";
				
    			echo "<select name='AFBFORM' id='AFBFORM' onChange='changed(this.value)'>";
				
   		 		echo "<option selected disabled>Sélectionnez</option>";
				
                mysql_data_seek($qryFormList,0);
				while($resFormList = mysql_fetch_assoc($qryFormList))
				{
					echo '<option value="'.$resFormList['loginID'].'"';
								if($_SESSION['FORMDATA']['AFBFORM'] == "".$resFormList['loginID']."")
								{
									echo 'selected';
								}
								else
								{
									echo ' ';
								}

						echo '>'.$resFormList['loginNOM'].'</option>';
				}
				
   			 echo "</select>";
	
              ?>   
                 
    </td>
    <td width="128" class="menutd">Courriel<font color="red"><sup>*</font></sup></td>
    <td width="268" ><input name="AFB_FORM_COURRIEL" type="text" id="AFB_FORM_COURRIEL" size="30" value="<?php echo $_SESSION['FORMDATA']['AFBFORMCOURRIEL']; ?>" /></td>
  </tr>

if nothing happens, then the function is not being called. Are you using Firefox browser? If so, click on the tools menu option then error console. Click on the errors tab, then click to clear all the errors. Go to your webpage and see what errors you get. Also, try changing the select field and see what error you are getting.

Line 9 is missing a semi-colon in the javascript:

echo "<script type='text/javascript'> var formCourriel = new Array();";

Hey Solid, I really appreciate your help...I was unaware of the error console :)

turns out I was missing encapsulating ' ' around my variable. The script was escaping because I had periods in the email addresses.

Original line:

echo "formCourriel[".$resFormList['loginID']."] = ".$resFormList['COURRIEL'].";";

Correct line:

echo "formCourriel[".$resFormList['loginID']."] = [B]'[/B] ".$resFormList['COURRIEL']." [B]'[/B] ;";
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.