Ajax/js function does not work

Asked

Viewed 100 times

1

It follows below my code, with the function and the field. When testing does not work anything, I do not know what is wrong. I did this function to check if such a period already exists in the bank. But I don’t even know how to check if the php file is receiving the data or if the result is coming back and I’m not displaying correctly.

Thanks in advance for the suggestions.

function verifica(){                  
      var id  = document.getElementsByName("id")[0].value;
      var dt1 = document.getElementsByName("datahora_inicial")[0].value;
      var dt2 = document.getElementsByName("date")[0].value;
      var parametros = {
              method: "GET"
      };
      fetch("php/verifica.php?id=" + id + "&dt1=" + dt1 + "&dt2=" + dt2, parametros).then(function(resposta) {
          return resposta.json();
      }).then(function(retorno){
          console.log(retorno)
          if (retorno == 1) {
            document.getElementById("resultado").value = "Período indisponível! Altere-o.";
            document.getElementById("resultado").style.display = 'inline-block';
            document.getElementById("link").style.display = 'inline-block';
          }else{
            document.getElementById("resultado").style.display = 'none';
          }
      });
  }




<label class="col-sm-2 col-sm-2 control-label">Fim</label>
<div class="col-md-5">
  <input type="text" class="form-control round-input" name="date" required="required" id="dtfim" onchange="verifica();" value="" disabled="disabled">
   <input type="text" class="form-control round-input" id="resultado" disabled="disabled" style="display: none; width: 60px;"><a style="display: none;" href='link.php' id="link">Link</a>                                                            
</div>

php:

header('Content-Type: json/application'); 
$id  = $_GET['id'];
$dt1 = $_GET['dt1'];
$dt1 = date('Y-m-d', strtotime($dt1));

$dt2 = $_GET['dt2'];
$dt2 = date('Y-m-d', strtotime($dt2));

$select    = "SELECT * FROM lista WHERE id = '$id' and start <= '$dt1' and end >= '$dt2'";
$result    = mysqli_query($conexao, $select);

if ((mysqli_num_rows($result) > 0)){
    $mostrar = 1;
    echo json_encode($mostrar); 
}else{
    $mostrar = 2;
    echo json_encode($mostrar);
}
  • 1

    What appears on console ? Request works correctly ?

  • There is no element called id in the GIFT. There is no element called datahora_inicial in the DOM. What is the file code php/verifica.php? If in the first then you return resposta.json(), is sure that in the second the expected value for retorno is 1? I reinforce Otto’s query: does something appear on the console? The request is made?

  • The first time I did, yes, I returned 1 or 2, but now nothing.

  • And you changed what in the project?

  • The place where I’m calling the function is correct?

  • The final date field is no longer digitized, it only receives one result. Maybe something in this question is getting in the way of getting his value. And I don’t know how to check if the request returned any records.

Show 1 more comment
No answers

Browser other questions tagged

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