I want to fetch data from PostgreSQL into my Combobox using query Ajax in html page.
Here is the code of popup box where I want to fetch the data:
<html>
<body>
<script src="../webmap/jquery-1.11.2.min.js"></script>
<script type="text/javascript">
function dropdown(){
$('#a1_title').empty();
$('#a1_title').append("<option>loading....</option>");
$.ajax({
type: "POST",
url: "drpdwn.php",
contentype: "application/json; charset=utf-8",
dataType: "json",
sucess:function(data){
$('#a1_title').empty();
$('#a1_title').append("<option value='0' >----select name-</option>");
$.each(data,function(i,item){
$('#a1_title').append('<option value="'+ data[i].id +'" >'+ data[i].name +'</option>');
});
},
complete: function(){
}
});
}
$(document).ready(function(){
dropdown();
});
</script>
<select id="a1_title">
</select>
</body>
</html>
This is drpdwn.php
file:
<?php
$host = "localhost";
$user = "postgres";
$pass = "admin";
$db = "Querybuilderdb";
$con = pg_connect("host=$host dbname=$db user=$user password=$pass")
or die ("Could not connect to server
");
$query = "SELECT id, name FROM tab";
$result = pg_query($con, $query);
if(pg_num_rows($result)){
$data=array();
while($row=pg_fetch_array($result))
{ $data[] = array(
'id'=>$row['id'],
'name' =>$row['name']
);
}
echo json_encode($data);
}
?>
I have update my code here Can anyone please tell me where i'm doing wrong?