Link to home
Start Free TrialLog in
Avatar of Puneet Arora
Puneet AroraFlag for India

asked on

Dropdown Not Populating From Ajax Post Call due to java script conflict

Dear ,

Drop Down Not Populating From Ajax Post Call due to java script conflict , But , if some dummy
option of Select control is filed {A,B,C } , on selection it shows data from ajax call .Kindly check the
images attached and the code embedded also .  


(function($){
		/* Complete jquery code must be wrapped under this function
			It is a good practice to include this complete code in a sepearte js file and include it after all other referred files in the document
			(I included this myjs.js just before </html>, or we can in the footer of file....  Programmers tend to to this for lesser conflict )
			If you are creating another seperate file better to use a another symbol in place of $ for this function
		*/

	$(document).ready(function(e) {
		//Best practice is to add this document.ready function only once in the file. 
        // get your ajax values before initializing the selectbox plugin 
        var fk_i_id = 0;	
		if(fk_i_id === 0)
		{
		$.ajax({
				 type:'post',
				 url:'../api1.php?getlist=categorya',
				 datatype: 'json',
				  data: {id:fk_i_id},

				  success: function(data){
				  var opts =  JSON.parse(data);
					 
				 $('#s2').empty();
				   $.each(opts, function(i, d) {
                     		   $('#s2').append('<option value="' + d.pk_i_id + '">' + d.s_name + '</option> ').html();
                                 });

			}

			});
		}    
         
         //  Inititalize first selectbox
         $("#s22").dropdownchecklist( { 
         	forceMultiple: true, 
         	onComplete: function(selector) {
         		var values = "";
         		for( i=0; i < selector.options.length; i++ ) {
         			if (selector.options[i].selected && (selector.options[i].value != "")) {
         				if ( values != "" ) values += ";";
         				values += selector.options[i].value;
         			}
         		}
         		alert( values );
         	}, 
         	onItemClick: function(checkbox, selector){
         		var justChecked = checkbox.prop("checked");
         		var checkCount = (justChecked) ? 1 : -1;
         		for( i = 0; i < selector.options.length; i++ ){
         			if ( selector.options[i].selected ) checkCount += 1;
         		}
         		if ( checkCount > 3 ) {
         			alert( "Limit is 3" );
         			throw "too many";
         		}

			}			
           
        });
 
        
		//  Inititalize second selectbox

        $("#s2").dropdownchecklist( {icon: {}, width: 750 } );
		
                  [embed=file 933796]
	 
});
			
})(jQuery);
// This (jQuery) callback is essential for making $ function work properly

Open in new window

2.png
3.png
Avatar of Ahmed Merghani
Ahmed Merghani
Flag of Sudan image

Can you display the returned value of ajax?? just after line no. 20 add:
alert(data);

Open in new window

Avatar of Puneet Arora

ASKER

Data is coming 1000% , if you need screen shot I can send it
Yes kindly print it.
Dear Ahmed Merghani

[{"FilterNameID":"4","FilterName":"Postet By","FilterValue":"Authorised Dealership","CategoryAID":"12","CategoryBID":"18"},{"FilterNameID":"4","FilterName":"Postet By","FilterValue":"Used Car Dealers","CategoryAID":"12","CategoryBID":"18"},{"FilterNameID":"4","FilterName":"Postet By","FilterValue":"Owner","CategoryAID":"12","CategoryBID":"18"},{"FilterNameID":"5","FilterName":"Fuel Type","FilterValue":"Petrol","CategoryAID":"12","CategoryBID":"18"},{"FilterNameID":"5","FilterName":"Fuel Type","FilterValue":"Diseal","CategoryAID":"12","CategoryBID":"18"},{"FilterNameID":"5","FilterName":"Fuel Type","FilterValue":"CNG","CategoryAID":"12","CategoryBID":"18"},{"FilterNameID":"5","FilterName":"Fuel Type","FilterValue":"LPG","CategoryAID":"12","CategoryBID":"18"}]
I was trying with this json data populate dropbox having check boxes based on <script type="text/javascript" src="dist/js/bootstrap-multiselect.js"></script>  library .

[{"FilterValue":"Petrol"},{"FilterValue":"Diseal"},{"FilterValue":"CNG"},{"FilterValue":"LPG"}]

The data is coming as above from ajax call , but is not rendered in dropdown , I don't  why , please help
in this case , I m using bootstrap-multiselect.js.
 
 function callC(FilterName) {
    $.ajax({
            type: "POST",
            url: "./api1.php?getlist=filtervals&id="+FilterName,
            contentType: "application/json; charset=utf-8",
             dataType: "json",
             
              success: function(result) {
             //var myHtml = '<p>' +FilterName+'<p> </br>';      //loop var categories = [];
            var myHtml = '';
            for (var i = 0; i < result.length; i++) {
                 var item = result[i].FilterValue ;
                 var myHtml =  '<option value="' + item + '">' + item+ '</option>';
                 //myHtml += "<li>"   +item + "</li>";
            }  
                
                  alert(myHtml);
                 $("#p10").append(myHtml);     
            }
    });
}

Open in new window


<select id="p10" multiple="multiple" name = "p10">
                            
                                              
                                        </select>

Open in new window

Where do you call function "callC(FilterName)" ??
I call this function in document ready function
ASKER CERTIFIED SOLUTION
Avatar of Pravin Asar
Pravin Asar
Flag of United States of America image

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
Thanks , the main reason was javascript conflict , I was able to solve it earlier , any way thanks for the
support !