i trying link 2 drop down using ajax data in json format. tried following code, shows blank drop down departments projects. not sure code wrong.
code of main page index.php given below:
<?php include_once("includes/connection.php");?> <html><head> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script> <script type="text/javascript"> function showdept(){ $('#deptdropdown').empty(); $('#deptdropdown').append("<option> loading...</option>"); $('#projectdropdown').append("<option value='0'> projects </option>"); $.ajax({ type:"post", url:"departmentdropdown.php", contenttype:"application/json; charset:utf-8", datatype:"json", success: function(data){ $('#deptdropdown').empty(); $('#deptdropdown').append("<option value='0'> departments </option>"); $.each(data,function(i,item){ $('#deptdropdown').append('<option value="'+ data[i].deptid +'">'+ data[i].deptname +'</option>'); }); }, complete: function(){ } }); } function showproject(departmentid){ $('#projectdropdown').empty(); $('#projectdropdown').append("<option> loading...</option>"); $.ajax({ type:"post", url:"projectdropdown.php", contenttype:"application/json; charset:utf-8", datatype:"json", success: function(data){ $('#projectdropdown').empty(); $('#projectdropdown').append("<option value='0'> projects </option>"); $.each(data,function(i,item){ $('#projectdropdown').append('<option value="'+ data[i].projectid +'">'+ data[i].projectname+'</option>'); }); }, complete: function(){ } }); } $(document).ready(function(){ showdept(); $("#deptdropdown").change(function(){ var deptid= $("#deptdropdown").val(); showproject(deptid); }); }); </script></head><body> <span>departments</span><br /> <select id="deptdropdown"></select> <br /><br /> <span>projects</span><br /> <select id="projectdropdown"></select> </body></html>
the code departmentdropdown.php file below:
<?php include_once("includes/connection.php"); $query=" select deptid, deptname departmentdetails active_status=1 order deptname"; $result=mysqli_query($con,$query); $data=array(); while($array=mysqli_fetch_assoc($result)){ $data[] = array('deptid' => $array['deptid'], 'deptname' => $array['deptname']); } header('content-type: application/json'); echo json_encode($data);?>
the code projectdropdown.php file below:
<?php include_once("includes/connection.php"); $query="select projectid, deptid projectname projectdetails active_status=1 , deptid='".$_get['departmentid']."' order projectname"; $result=mysqli_query($con,$query); $data=array(); while($array=mysqli_fetch_assoc($result)){ $data[] = array('proid' => $array['projectid'], 'depid' => $array['deptid'], 'projectname' => $array['projectname']); } header('content-type: application/json'); echo json_encode($data);?>
missing +
after data[i].deptid
, data[i].projectid
.change following
showdept()
$('#deptdropdown').append('<option value="'+ data[i].deptid '">'+ data[i].deptname +'</option>');
showproject(departmentid)
$('#projectdropdown').append('<option value="'+ data[i].projectid'">'+ data[i].projectname+'</option>');
to
showdept()
$('#deptdropdown').append('<option value="'+ data[i].deptid +'">'+ data[i].deptname +'</option>');
showproject(departmentid)
$('#projectdropdown').append('<option value="'+ data[i].projectid +'">'+ data[i].projectname+'</option>');
Comments
Post a Comment