so i have a list of user that already loaded in dropdown list and i want to load the my second SQL query where it needs user id to be load. so after chosing the user i can't execute my second Query. apparently all seems good to me

this is what i have in my twig file

<select style="color: orangered;" id="visiteurSelectionne" class="zone">
            <option disabled selected value="0">Selectionnez un 
              visiteur</option>

            {% for user in users %}
                <option value={{ user.id }}>{{ user.username }}</option>
            {% endfor %}
        </select>
        <label class="titre">Mois :</label>

        <select style="color: orangered;" id="selectMoisDispo" class="zone">
            <option disabled selected value="0" class="date">Selectionnez une date</option>

        </select>

this is my Js function

$(document).ready(function () {
                $('#ficheInfos').hide();
                $('#visiteurSelectionne').on('change', function () {
                    $.ajax({
                        url: './valide',
                        type: 'POST',
                        dataType: 'json',
                        data: { 'id=' : + this.value },

                    success: function (data) {
                            $('#ficheInfos').show();
                        $('#date').text(data.mois);

                    }


                });
                });


            });

and this is my function in controller

    /**
     * @param Request $request
      * @return mixed
      * @Route("/valide")
     * @Method({"POST"})
     */

      public function ajaxAction(Request $request): Response
      {

    if (!$request->isXmlHttpRequest()) {
        return new JsonResponse(array('message' => 'You can access to this 
    url with ajax only'), 400);
    }

    $em = $this->getDoctrine()->getManager();
    $id = $request->request->get('id');
    $moisAnne = $em->getRepository("AppBundle:FicheFrais")->getMoisDispo($id);

    $response = new Response(json_encode(array(

        'mois' => $moisAnne

    )));



    $response->headers->set('Content-Type', 'application/json');
    /* la r�ponse doit �tre encod�e en JSON ou XML, on choisira le JSON
       * la doc de Symfony est bien faite si vous devez renvoyer un objet         *
       */


    new Response(json_encode($response));
    return $response;


}

and this is my SQL function

public function getMoisDispo($idUser)
{

    $sqlReq = "SELECT DISTINCT moisAnne FROM fiche_frais WHERE idUtilisateur =' .$idUser. '";

    $db= $this->getEntityManager()->getConnection()->prepare($sqlReq);
    $db->execute([]);
    return $db->fetchAll();

}

the resault that i'm waiting for is to load the all dates should be load in the second list by user id

1 Answers

0
Ugo T. On Best Solutions

$('#visiteurSelectionne').on('change', function() {
  console.log("Selected value is " + $(this).val());
    $.ajax({
        url: './valide',
        type: 'POST',
        dataType: 'json',
        data: {
            'id': $(this).val()
        },
        success: function(data) {}
    });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select style="color: orangered;" id="visiteurSelectionne" class="zone">
        <option disabled selected value="0">Selectionnez un visiteur</option>
        <option value="val-1">val-1</option>
        <option value="val-2">val-2</option>
        <option value="val-3">val-3</option>
    </select>
    <label class="titre">Mois :</label>
    <select style="color: orangered;" id="selectMoisDispo" class="zone">
        <option disabled selected value="0" class="date">Selectionnez une date</option>
    
    </select>