Reputation: 33
Can someone explain why this code shows me blank select box ? When I select first box which name is "firstbox" then it should be showing info in second box which id is "komandos" but it won't showing anything...
<script>
jQuery(function($) {
$("#komandos").change(function() {
var id = $("#id").val();
if(isNaN(id)) {return;}
$.ajax({
type: "GET",
url: "ajax.php",
data: {'update_tm': '', 'id': id},
dataType: 'json',
success: function(data){
$("#komandos").empty();
for (var i = 0; i < data.length; i++)
{
$("#komandos").append('<option>'+data[i].team+'</option>');
}
}
});
});
});
</script>
ajax.php
<?php
include_once('inc/conn.php');
include_once('inc/futbolas.php');
if(isset($_GET['update_tm']) && is_numeric($_GET['id']))
{
$query = $pdo->prepare("SELECT `pirma_komanda`,`antra_komanda` FROM futbolas WHERE `id` = ?");
$query->execute(array($_GET['id']));
if($query)
{
$query = $query->fetch();
$rez[0]['team'] = $query['pirma_komanda'];
$rez[1]['team'] = $query['antra_komanda'];
echo json_encode($rez);
}
}
Upvotes: 0
Views: 59
Reputation: 9635
you have to use JSON.parse
like
success: function(data){
$("#komandos").empty();
var result = JSON.parse(data);
for (var i = 0; i < result.length; i++)
{
$("#komandos").append('<option>'+result[i].team+'</option>');
}
}
});
See JS FIDDLE
Upvotes: 3