hey all..
i was wondering if anyone culd help me wid this..

i've a html file with javascript and a php file..
i need to access the db result generated by the php file in my javascript..to dynamically load drop down menu in the html file..

was wondering if i cud use somehin like..

var abc='load.php?array_name='+array_name;
to access the array in php file..

thanks in advance..:-)

Send File Code and Details

AJAX is usefull for this concept

on click of the select element run the php file through ajax which will fetch the values from the db and will populate the select element.

on click of the select element run the php file through ajax which will fetch the values from the db and will populate the select element.

it would be gr8 if you could help with the code snippets..coz m fairly new to php and ajax..

do you mean something like..
<select name=blabla onclick=function_name_inajax();>

thanks in adavance..
do not forget to send sample code..

It will be good if you try to code yourself, we are always there to help you out if you get stuck up somewhere in the code, to start with, here is the ajax functions you can use -

function httpRespObj(url)
{
   try
   {
	 xmlHttp=new XMLHttpRequest();        
   }
   catch (e)
   {
	 try
	 {
		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	 }
	 catch (e)
	 {
		try
		{
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");                
		}
		catch (e)
		{
			alert("Your browser does not support AJAX!");
			return false;
		}
	 }
   }
  
   xmlHttp.open("GET",url,false);       
   xmlHttp.onreadystatechange=responseBack;
   xmlHttp.send(null);
}

function responseBack()
{
   if(xmlHttp.readyState==4)
   {
   	//put the response to the html select element 
	 document.getElementById("selElement").value = xmlHttp.responseText;
   }
   else
   {
	 document.getElementById("selElement").value="";
   }         
}

you can call the httpRespObj(url) on focus of the select element(whatever way you like it).
"url" is your php script (i called it fetch.php) which fetches the data from the db.
<select onFocus="return httpRespObj('fetch.php?id=<?=$id?>');">
If you dont need to pass any id to the request, you can safely omit that part

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.