Ajax requests

Asked

Viewed 112 times

0

Speak guys, I have a form and in it a field where the user will be able to select a professional for the desired service, and when selecting such service I have to inform the available schedules, making an ajax request using a select script in my database picking up the supposed schedules that are already filled by the professional and thus using JS to filter and expose to the user only the available schedules, but is giving error. Follows the code.

function buscarHorarios(dataConsu)
{
  $.ajax({
      url: 'select2.php', // declarando o script que vai conter os dados
      type: 'POST',
      async: true,
      dataType: 'json', // o tipo de arquivo de cambio que vai ser na usado na requisição
      data:{'dataConsu': dataConsu}, 

      success: function (resultado, xhr, status)
      {
        if (resultado.statusText == "OK")
        {
          var size = resultado.responseText[0].length;
          var resp = JSON.parse(resultado.responseText);

          document.getElementById("uhoraConsult").innerHTML = "<option></option>";

          if (resp.resp == false)
          {
            for (var i = 8; i < 18; i++)
            {
              document.getElementById("uhoraConsult").innerHTML += '<option value="'+ i +'">' + i + ':00</option>';
            }
          }
          else
          {
            var t;
            for (i = 8; i < 18; i++)
            {
              t =false;
              for (var j = 0; j < size; j++)
              {
                if (resposta[0][j].Horario == i)
                {
                  t = true;
                }
                if (t == false)
                {
                  document.getElementById("uhoraConsult").innerHTML += '<option value="'+ i +'">' + i + ':00</option>';
                }
              }
            }
          }
        }
},
      error: function (xhr, error, status)
      {
        alert (xhr.statusText + error + status);  
      }
  });
}

* A mistake I managed to capture in an Alert: Okparsererrorsyntaxerror: JSON.parse: Unexpected end of data at line 1 column 1 of the JSON data

*

<?php

require_once "conexao01.php";
class Horarios
{
    public $hora1;
    public $hora2;
}

    $connecting = conectaAoMySql(); // estabelecendo conexao com o banco de dados

    $vetorHorario = []; // declarando vetor como nulo

    $dataConsulta = $medico = "";

    if(isset($_POST["dataConsu"]))
        $dataConsulta = $_POST["dataConsu"];

    if(isset($_POST["medico"]))
        $medico = $_POST["medico"];

    $SQL = "
        SELECT Horario
        FROM Agenda 
        WHERE DataConsulta = '$dataConsulta'
        AND Medico = '$medico'
    ";

    if (!$resultadoSQL = $connecting->query($SQL))
        throw new Exception ("Ocorreu uma falha: ". $connecting->error);

    if ($resultadoSQL->num_rows > 0)
    {

        while ($row = $resultadoSQL->fetch_assoc())
        {
            $vetorHorario [] = $row ["Horario"];
        }

        $vsf = json_encode($vetorHorario);
        echo $vsf;
    }
?>
  • What is the result of resultado.responseText?

  • Guy as far as I know not to know, it already falls straight into the error function.

  • Give a console.log() after the variant resp. And async: true is unnecessary, since ajax is asynchronous.

  • Okay, console.log is not indicating any problems.

  • @Mauroalexandre, would have any idea what might be going on. One of the errors is that it is not returning a JSON.

  • @dvd, is returning empty

  • @dvd, in the form field the date is inserted in the BR standard and in the database is in the U.S standard, so in the select that receives the variable in the BR format you won’t find any right? A possible mistake?

  • Is there any way to put the U.S pattern in the form ? Or put the bd in the BR pattern?

  • vc can change the format before making the query.

  • vc wants to transform dd-MM-yyyy to yyyy-MM-dd?

  • Exactly, but any way that’s efficient, it’s worth

  • In this question you teach how: https://answall.com/q/224948/8063

  • Just make an explosion and then concatenate the 2-1-0 indexes

  • @dvd would need to convert from BR standard to U.S

  • So this you can do both in JS before you send Ajax and in PHP when you receive the date.

  • would have some example in JS?

Show 11 more comments
No answers

Browser other questions tagged

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