Onblur clears the field

Asked

Viewed 153 times

1

This code calculates 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 the birth date, when leaving the DateTime the age is calculated.

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

  • This your code gives a refresh on the page, you want to make the code to record in the bank in the same file that has the form? to fill the value just add value="<? php echo $_POST['Dn']? >" to the input

  • <input type="datetime"> Obsolete https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/datetime

1 answer

0

For starters replace

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

for

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

And linking to direct Javascrit methods in HTML is not recommended.

Use in this way:

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

Either in an external . js file or inside tags

"<script></script>"

Browser other questions tagged

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