Hi, I'm really new to PHP and programming I can say. I really need your help.
Basically I'm doing a dynamic dropdown menu using PHP and MySQL and when user select the company name at dropdown, then email address will be populated at new textfield.
The company table consists of Id, company_name, email_add, and status_email fields.
I already done the dropdown menu but when select the company name, nothing happen. Here is the code:
<?
$query="SELECT * FROM company";
$result3 = mysql_query ($query); ?>
<select name="company_name" class="formfield" onChange="showEmail();">
<?
while($row=mysql_fetch_array($result3)) { $email_add=$row['email_add']; $id = $row['Id'];
$company_name = $row['company_name']; ?>
<option value="$id"><? echo $company_name; ?></option>
<? } ?>
</select>
<?
$sql="SELECT * FROM company WHERE Id=$id";
$sqlresult = mysql_query($sql);
while($row=mysql_fetch_array($sqlresult))
{ $email_add=$row['email_add'];
echo "<input name='email_add' type='text' value='$email_add'>";
}
Javascript codes:
<SCRIPT LANGUAGE="JavaScript" type="text/javascript">
function showEmail()
{
if ($id=="")
{
document.getElementById("email_add").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("email_add").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","surveynew.php?id="+$id,true);
xmlhttp.send();
}
Thanks in advance :)