Onblur cleans the field

1

This code computes the difference between dates:

<!DOCTYPE html>
<html>

<head>
  <title></title>
</head>

<body>
  <form method="post" action="">
    <input type="DateTime" onblur="clicaBotao()" name="dn">
    <input type="submit" id="buton" name="calcula" value="CALCULAR">


  </form>
  <script>
    function clicaBotao() {
      buton.click();
    }
  </script>
</body>

</html>
<?php
    date_default_timezone_set('Europe/Lisbon');
    $dn = new DateTime ($_POST['dn']);
    $hoje = new DateTime(Date());
    $idade = $hoje ->diff ($dn);

    echo "Tem {$idade->y} anos e {$idade->m} meses de idade";
?>

After typing in the birth date, when you exit the DateTime age calculation is performed.

Only the DateTime field is deleted and I want it to be kept, to be entered in the database. How can I get around this?

    
asked by anonymous 19.10.2017 / 16:58

1 answer

0

For startup replace

<input type="DateTime" onblur="clicaBotao()" name="dn">

by

<input type="date" onblur="clicaBotao()" id= "id" name="dn">

And linking to JavaScrit methods directly in HTML is not recommended.

Use this way:

$('#id').blur(function () {
  chameSuaFuncaoAqui();
});

Whether it can be in an external .js file or if you prefer within the tags

  

"<script></script>"

    
20.10.2017 / 17:10