Select with php javascript ajax json

Asked

Viewed 438 times

0

<script src="http://yui.yahooapis.com/3.12.0/build/yui/yui-min.js"></script>
        <script  src="js/jquery-3.2.1.min.js" ></script>
        <script type="text/javascript">

   function run(){    

   var id = $("#id_Turma").val();
   console.log(id);


     $.ajax({
        url : "includes/disciplina.php/",
        type : "POST",
        data : { 
            id : id
             },
        success : function(JSON) {
            $("#idDiciplina").html("");
            console.log(JSON);



             $('#idDiciplina').append("<option value='' selected='selected'>Disciplinas</option>");

            for (var i = 0; i < JSON.length; i++) {
                $('#idDiciplina').append("<option value="+JSON[i][0]+">" + JSON[i][1] + "</option>");

            }

            }

    }); 

}

Look at the code I can’t identify because damn it I can’t capture the json values by the for.

<?php       

 //include('../seguranca.php');

 $id_turmar = $_POST['id'];
           $consulta = DB::getConn()->prepare("SELECT idDisciplina, Nome FROM `disciplina` WHERE Turma_idTurma = :Turma_idTurma;");
            $consulta->bindParam(':Turma_idTurma',  $id_turmar, PDO::PARAM_STR);
            $consulta->execute();
            while ($assunto = $consulta->fetch(PDO::FETCH_ASSOC))  { 

             $assunto_post[] = array(
            'idDisciplina'  => $assunto['idDisciplina'],
            'Nome' => utf8_encode($assunto['Nome']),
        );  

            }
   echo(json_encode($assunto_post));
  • What is the result of console.log(JSON)?

  • [{"idDisciplina":"13","Name":"Matematica"},{"idDisciplina":"14","Name":"Portugues"},{"idDisciplina":"15","Name":"Geography"}]

  • Then it shouldn’t be JSON[i].idDisciplina instead of JSON[i][0]?

  • If I place JSON[i][0] the result is [&#xA;cadastroQuestao.php:136 {&#xA;cadastroQuestao.php:136 "&#xA;cadastroQuestao.php:136 i&#xA;cadastroQuestao.php:136 d&#xA;cadastroQuestao.php:136 D&#xA;cadastroQuestao.php:136 i&#xA;cadastroQuestao.php:136 s&#xA;cadastroQuestao.php:136 c&#xA;cadastroQuestao.php:136 i&#xA;registration.php:136 p registration.php:136 l registration.php:136 i it puts a letter in each line and keeps bouncing.

  • If I put JSON[i]. idDiscipline arises as a result 136 Undefined

  • You putting "<option value="+JSON[i].idDisciplina+">" + JSON[i].Nome + "</option>" does not solve the problem?

Show 1 more comment

1 answer

0

Try using for ... in:

for (j in JSON) {
   $('#idDiciplina').append(
      "<option value="+ JSON[j].idDisciplina +">" + JSON[j].Nome + "</option>"
   );
}

I can’t remember if you need to use var -> for(var j in JSON){

  • I tested with and without Var.

  • Your JSON variable is an array ?

  • well it is to be I define it, I thought it was assigned automatically. how do I set it as array?

  • First I recommend you change the word JSON to another, date or result type, because JSON is a reserved word of javascript. In your console.log(JSON) this should appear: JSON Array(3)0: {idDiscipline: "13", Name: "Matematica"}1: {idDiscipline: "14", Name: "Portugue s"}2: {idDiscipline:)

  • then I put given[] or any other name and javascript will interpret it as array.

  • No. I actually meant that JSON is the name of a javascript variable that handles jsons, where you can convert a json to JSON.stringify(myObj) string or a string to JSON.parse(string). You using the word to catch the return of ajax you replaced this variable.

Show 1 more comment

Browser other questions tagged

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