Return 2 date type in ajax

Asked

Viewed 464 times

-2

I have an ajax that returns me a date on onSelect of a datepicker, so far so good.. but I need the consultation to return me also the ID of a select (dentists) how can I make these 2 returns on a request?

follows my code:

javascript

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
        <script src="https://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
        <link type="text/css" rel="stylesheet" href="https://code.jquery.com/ui/1.11.4/themes/black-tie/jquery-ui.css" media="screen">

       <script>
       function retornarDentistas(data) {

            var categorias = "";
            categorias  = '<option value="" disabled selected>Selecione o dentista</option>';
            $.each(data, function(chave,valor){

                categorias += '<option value="' + valor.dentistaId + '">' + valor.nomeDentista + '</option>';
            });
            $('#dentistas').html(categorias);

        }

            $(document).ready(function()  {
            $('#datepicker').datepicker({
            dateFormat: 'yy-mm-dd',
            dayNames: ['Domingo','Segunda','Terça','Quarta','Quinta','Sexta','Sábado'],
            dayNamesMin: ['D','S','T','Q','Q','S','S','D'],
            dayNamesShort: ['Dom','Seg','Ter','Qua','Qui','Sex','Sáb','Dom'],
            monthNames: ['Janeiro','Fevereiro','Março','Abril','Maio','Junho','Julho','Agosto','Setembro','Outubro','Novembro','Dezembro'],
            monthNamesShort: ['Jan','Fev','Mar','Abr','Mai','Jun','Jul','Ago','Set','Out','Nov','Dez'],
            nextText: 'Próximo',
            prevText: 'Anterior',
            inline: true,
            onSelect:
              function () {
                var date = $("#datepicker").val();

                $.ajax({
                     type: "POST", 
                     url: "retornar_data.php",
                     data: { date: date } ,  
                     success: function(data) {

                        var json = JSON.parse(data);
                        for(var i = 0; i < json.length; i++) {
                        var obj = json[i];
                        var jsonString = JSON.stringify(obj.horaAgenda);

html

 <form id="pesquisarDentista">
  <select name="dentistas" id="dentistas" required>

</select>  
</form> 
<br>


    <div id="datepicker"></div> <br>
    <div id="teste"> </div>




 <script src="http://localhost/odonto/retornar_dentistas.php?callback=retornarDentistas"></script>    

</body>

ret_data.php

<?php 

    if ( isset($_POST['date']) ) {
    $date = $_POST['date'];
    } 



    $conecta = mysqli_connect("localhost","root","","odonto");

    $selecao = "SELECT * from agenda WHERE dataAgenda = '{$date}' ORDER BY horaAgenda ";
    $categorias = mysqli_query($conecta,$selecao);

    $retorno = array();
    while($linha = mysqli_fetch_object($categorias)) {
        $retorno[] = $linha;
    }   

    echo json_encode($retorno);

    // fechar conecta
    mysqli_close($conecta);
?>

ret_dentistas.php

<?php 
    $callback = isset($_GET['callback']) ?  $_GET['callback'] : false;
    $conecta = mysqli_connect("localhost","root","","odonto");

    $selecao = "SELECT dentistaId, nomeDentista FROM dentista";
    $categorias = mysqli_query($conecta,$selecao);

    $retorno = array();
    while($linha = mysqli_fetch_object($categorias)) {
        $retorno[] = $linha;
    }   

    echo ($callback ? $callback . '(' : '') . json_encode($retorno) . ($callback? ')' : '');

    // fechar conecta
    mysqli_close($conecta);
?>

1 answer

0

If you need "two returns" on ajax then use two indexes.

Example:

 echo json_encode('retorno' => $retorno, 'horario' => $horario))

In Javascript, you can capture these indexes that have been transformed into JSON.

  • I am sending by ajax a date. there he makes the query and returns me everything with that date.. soh that I need to filter this query further i.e., outside the date send also the ID of a select field.. how can I do this?

  • dentists = $('#dentists'). val(); $.ajax({ type: "POST", url: "return_data.php", data: { date: date, id:dentists }, ....

Browser other questions tagged

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