Problem with filter via JSON

Asked

Viewed 218 times

1

Good night,

I am here with a problem with filter via json for districts and counties I will explain.

The user chooses a district and it filters and lists in another selectbox the counties that are associated that district and the system is working the only problem and that this in the counties appear various options as NULL.

Script

<script type="text/javascript">
$(function(){
    $('.carregando').hide();
    $('#distritos').change(function(){
        if( $(this).val() ) {
            $('#cod_cidades').hide();
            $('.carregando').show();
            $.getJSON('ajax/processa_concelhos.php?search=',{distritos: $(this).val(), ajax: 'true'}, function(j){
                var options = '<option value=""></option>'; 
                for (var i = 0; i < j.length; i++) {
                    options += '<option value="'+j[i].id_concelho+'">' +j[i].titulo+'</option>';
                }   
                $('#concelhos').html(options).show();
                $('.carregando').hide();
            });
        } else {
            $('#concelhos').html('<option value="">– Escolha um Concelho –</option>');
        }
    });
});

Select districts

<select name="distritos" id="distritos">
<option value="0">-- Escolha um Distrito --</option>
<?php
$sql = "select * from distritos order by titulo";
$res = mysql_query($sql);
while ($row=mysql_fetch_assoc($res)){
    echo '<option value="'.$row['id'].'">'.utf8_encode($row['titulo']).'</option>';
}
?>
</select>

Select councils

<select name="concelhos" id="concelhos">
 <option value="">-- Escolha um estado --</option>
</select>

Processa_concelhos.php

<?php
require_once("../gtm/bd/funcoes.php");
ligarBd();  

$distritos =  $_REQUEST['distritos'] ;

$concelhos = array();

$sql = "select * from concelhos where id_mae='".$distritos."' order by titulo";
$res = mysql_query($sql);
while ($row=mysql_fetch_assoc($res)){
$concelhos[] = array(
    'id_concelho'   => $row['id'],
    'titulo'        => $row['titulo'],
);
}
echo( json_encode( $concelhos ) );
?>
  • Write the query code that returns json.

  • It’s all right there

  • Well I had to see and I found out what was causing the return of NULL values was because of the accents in the return code json I added to the title utf8_encode and it worked fine

  • @Césarsousa post a reply with the resolution. Your solution can be used by a person with the same (or similar) problem in the future.

  • Of course I will do that I was really thinking about doing that but I was wondering if I was allowed to answer the question itself

  • @There’s no problem with that, as you can see at that link. Even that subject has been discussed at the Meta.

  • I did not know thank you for making yourself available to help and for this knowledge so I already know thank you

Show 2 more comments

1 answer

3

The problem that was having NULL values return was due to the accentuation of some counties, so the solution went by using utf8_encode when returning the values to read the accents.

Code corrected with solution

<?php
require_once("../gtm/bd/funcoes.php");
ligarBd();  

$distritos =  $_REQUEST['distritos'] ;

$concelhos = array();

$sql = "select * from concelhos where id_mae='".$distritos."' order by titulo";
$res = mysql_query($sql);
while ($row=mysql_fetch_assoc($res)){
$concelhos[] = array(
'id_concelho'   => $row['id'],
'titulo'        => utf8_encode($row['titulo']), //linha corrigida
);
}
echo( json_encode( $concelhos ) );
?>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.