When entering, value is incorrect

Asked

Viewed 38 times

0

When checking the mounted Insert PHP with the form information, I can understand the value of the field data_final is not captured correctly, I would like to know what is wrong, because I believe it is a matter of formats.

Thanks in advance for the suggestions.

function that results in the data_final:

function calcula(){ 
      var periodo  = document.getElementsByName("periodo")[0].value;
      var dt1      = document.getElementsByName("datahora_inicial")[0].value;
      var val_t    = document.getElementsByName("f")[0].value;
      var parametros = {
              method: "GET"
      };

      fetch("php/calcula.php?periodo=" + periodo + "&dt1=" + dt1 + "&val_t=" + val_t, parametros).then(function(resposta) {
          return resposta.json();
      }).then(function(retorno){
          console.log(retorno)
          document.getElementById("dtfim").value = retorno;                         
      });
  }

<label class="col-sm-2 col-sm-2 control-label">Fim</label>//campo data 
  <div class="col-md-5">
    <input type="text" class="form-control round-input" name="datahora_final" required="required" id="dtfim" onchange="verifica();" value="" disabled="disabled"> 
 </div> 

php insert.

$datahora_final   = $_POST['datahora_final'];
$datahora_final   = date('Y-m-d H:i:s', $datahora_final);
$sql               = "INSERT INTO tabela(data_fim) VALUES ('$datahora_final')";

//Ao inserir, sql da data retorna este valor:
'1970-01-01 01:00:00'
e o seguinte erro:
Undefined index: datahora_final
  • A var_dump($_POST['datahora_final']) returns what?

  • The same I quoted above

  • 2

    Your code seems to be incomplete, or completely confused. In Javascript code you make a GET request to the file calcula.php, but you presented the code of insere.php that is not called in the passage presented and gets the values in $_POST. The very function calcula Javascript is not called in the code shown, but a function verifica, that was not presented, is called in the event onchange of input. You quote the field data_final and in the code there is datahora_final; they are the same field or you have not posted the excerpt that contains the first?

1 answer

1


If the final date field is being filled with date in the format 20/05/2017 then the problem is no date('Y-m-d H:i:s', $datahora_final) do so:

$datahora_final_sem_formatar   = $_POST['datahora_final'];
$datahora_final   = substr($datahora_final_sem_formatar,6,4) . "-" . substr($datahora_final_sem_formatar,3,2) . "-" . substr($datahora_final_sem_formatar,0,2);
  • That doesn’t work

  • What do you mean it doesn’t work?... what’s going on in your Post?.... if it is dd/mm/yyyy formed date the line I modified by removing the date only makes the change to yyyy-mm-dd to add in the seat, what may be missing is you insert with " quotation marks...because it works even if your field in the database is datetime, in fact if it is just add in the variable $time_end to the hour, minutes and second also, if you get these values in the post

  • But I’m inserting it with quotes. And that’s what you did I already do, it’s in the code I posted

  • First you’re not making the same code I posted you’re using $datahora_final = date('Y-m-d H:i:s', $datahora_final); which returns a totally wrong date, my code does that $datahora_final = substr($datahora_final_sem_formatting,6,4) . " -" . substr($datahora_final_sem_format,3,2) . " -" . substr($datahora_final_sem_format,0,2); to return the correct date formatted to insert into the database

  • According to your field is as disabled="disabled" change to readonly="readonly" if you are going through form, because you do not pass any value if it is the case and if it is by form you have to have <form method="post"> the post method

  • Thanks! Your code just hadn’t worked before due to disabled.

Show 1 more comment

Browser other questions tagged

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