Link to home
Start Free TrialLog in
Avatar of rknevitt
rknevitt

asked on

PHP Ajax MySQL dynamic drop down - Add Label to menu

Hi Guys,

I am not familiar with Ajax yet, so have lifted some code off the internet. I have changed it... and it works fine, but i need to add a label to the dynamically created drop down So that the menu items havea  label name, not just an ID.

The first menu is called makeid, when an option is selected it populates the second menu modelid with results, this works perfectly, except only the values are populated, and i cant work out how to add a label.

The label will also come from the same table (called tis_asset_models) the field i need is tis_asset_models_model

I dont know enough about ajax/javascript to be able to work this out :(

Any hel pwould be great

Thanks!
Rob
/////////////////////////////////////////
index.php
/////////////////////////////////////////
   <script type="text/javascript">
function AjaxFunction(make_id)
{
var httpxml;
try
  {
  // Firefox, Opera 8.0+, Safari
  httpxml=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
		  try
   			 		{
   				 httpxml=new ActiveXObject("Msxml2.XMLHTTP");
    				}
  			catch (e)
    				{
    			try
      		{
      		httpxml=new ActiveXObject("Microsoft.XMLHTTP");
     		 }
    			catch (e)
      		{
      		alert("Your browser does not support AJAX!");
      		return false;
      		}
    		}
  }
function stateck() 
    {
    if(httpxml.readyState==4)
      {
 
var myarray=eval(httpxml.responseText);
// Before adding new we must remove previously loaded elements
for(j=document.form1.modelid.options.length-1;j>=0;j--)
{
document.form1.modelid.remove(j);
}
 
 
for (i=0;i<myarray.length;i++)
{
var optn = document.createElement("OPTION");
optn.text = myarray[i];
optn.value = myarray[i];
document.form1.modelid.options.add(optn);
 
} 
      }
    }
	var url="dd.php";
url=url+"?make_id="+make_id;
url=url+"&sid="+Math.random();
httpxml.onreadystatechange=stateck;
httpxml.open("GET",url,true);
httpxml.send(null);
  }
</script>
<?php
	$query_make="select * from tis_manufacturers";
	$result_make=mysql_query($query_make);
?>
<select name=makeid onChange="AjaxFunction(this.value);">
<option value=''>Select One</option>
<?
//require "config.php";// connection to database 
$q=mysql_query("select * from tis_manufacturers ");
while($n=mysql_fetch_array($q)){
echo "<option value=$n[tis_manufacturers_id]>$n[tis_manufacturers_name]</option>";
}
 
?>
</select>
<select name=modelid>
 
</select>
 
 /////////////////////////////////////////
dd.php
/////////////////////////////////////////
<?php
$make_id=$_GET['make_id'];
 
$q=mysql_query("select * from tis_asset_models where tis_asset_models_makeid ='$make_id'");
echo mysql_error();
$myarray=array();
$str="";
while($nt=mysql_fetch_array($q)){
$str=$str . "\"$nt[tis_asset_models_id]\"".",";
}
$str=substr($str,0,(strLen($str)-1)); // Removing the last char , from the string
echo "new Array($str)";
 
?>
////////////////////////////////////////////////////////////////////////

Open in new window

Avatar of Ionut A. Tudor
Ionut A. Tudor
Flag of Romania image

var label = document.createElement("LABEL");
optn.appendChild(label );
using this is a simple task to integrate with your example. Good luck
ASKER CERTIFIED SOLUTION
Avatar of tg_wilk
tg_wilk

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rknevitt
rknevitt

ASKER

Both answers solved my problem so thank you :)

Rob