0

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..:-)

3
Contributors
4
Replies
5
Views
7 Years
Discussion Span
Last Post by network18
0

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.

0

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..

0

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.