Click here to Skip to main content
15,881,812 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
JavaScript
function update_name(id)
{

    var xmlhttp=new XMLHttpRequest();
     xmlhttp.onreadystatechange=function()
{
    if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
     document.getElementById("txt").innerHTML=xmlhttp.responseText;
     }
 }
    xmlhttp.open("GET","update_data.php?q="+id,true);
    xmlhttp.send();
}


i m using this code from http://www.w3schools.com/php/php_ajax_database.asp for populating dropdown depending on first dropdown.
I am getting the values and it displys in dropdown, but its not displaying in source code and not getting on form submit.

What I have tried:

i m trying code from http://www.w3schools.com/php/php_ajax_database.asp link,
replaced table with dropdown
Posted
Updated 23-Sep-16 3:51am
v2
Comments
Karthik_Mahalingam 23-Sep-16 9:22am    
what do you mean by " its not displaying in source code"
Sinisa Hajnal 23-Sep-16 9:40am    
printout responseText...depending on what you get back you may need to loop through the result or do some server side li creation...

Also, putting links in what have I tried doesn't work simply because you changed the code for you needs so its not the same anymore.

1 solution

Unless you are forcing yourself to use plain javascript I would encourage you to look at jQuery.

Also, I don't know what "I am getting the values and it displys in dropdown, but its not displaying in source code and not getting on form submit." means so I'll just ignore this part and give you the basic idea of dependent drop down selections.

Anyways. Lets say you've got the following drop downs

HTML
<select name="option1" id="option1">   <option value="1">Select 1</option></select>
   <option value="2">Select 2


<select name="option2" id="option2">   <option value="">Select</option></select>
</option>


Option 1 has to have some data in it because you need to start the dependent drop downs somehow. So based on that, you'll have an change event tied to option1

JavaScript
$("#option1").change(function(){
	$.ajax({
		url: "Url to however you get data here.php",
		type: "GET",
		dataType: "json",
		contentType: "application/json;charset=utf-8",
		async: true,
	}).success(function(data){
		var options = "<option value=''>Select</option>";
		$(data).each(function(k, v){ 
			options += "<option value='"+v.Value+"'>"+v.Display+"</option>";
		});
		
		$("#option2").html(options);
	}).error(function(){
		// something blew up, show error here
	});
});


So in that code, you make an ajax call to get your data, then inside the .success, you loop over the returned results to create your new options for the option2 drop down.

Disclaimer, I typed this code and didn't run it. The general idea here is correct for dependent drop downs and given I don't know what your data looks like/server side code this is as far as I'll take it. If you need anymore help, here is a google search that has tons of results that can help you accomplish this functionality.

jquery dependent dropdowns php
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900