2017-08-02 249 views
0

作为编程的新手,我需要您的帮助来构建一个小应用程序,该应用程序将使用php代码自动从以下表单域中计算dob的年龄!有没有办法实现这一点?使用javascript自动计算年龄dob使用javascript

我的HTML代码是:

<!DOCTYPE html> 
<html> 

<head> 
    <meta charset="utf-8"> 
    <meta http-equiv="X-UA-Compatible" content="IE=edge"> 
    <title>form practice</title> 

</head> 

<body> 

    <div class="form2" id="form2" name="form2"> 

     <form action="php/form2.php" method="post" id="Personalinfo"> 

      <label for="fname">Name:</label> 
      <input type="text" id="fname" name="firstname" placeholder="Client 
     Name.."> 

      <label for="lname">Lastname:</label> 
      <input type="text" id="lname" name="lastname" placeholder="Client 
     Lastname.."> 

      <label for="dob">Birthday:</label> 
      <input type="text" id="dob" name="dob" placeholder="yyyy/mm/dd.."> 

      <label for="age">Age:</label> 
      <input type="text" id="age" name="age" placeholder="Client Age.."> 

      <input type="submit" name="submitForm2" value="Submit"> 

     </form> 
    </div> 

</body> 

</html> 

和我的PHP代码连接到数据库的mysqli的:

<?php 
$servername = "localhost"; 
$username = "root"; 
$password = ""; 
$dbname = "testdb"; 

// Create connection 

$conn = new mysqli($servername, $username, $password, $dbname); 

// Check connection 

if ($conn->connect_error) 
{ 
    die("Connection failed: " . $conn->connect_error); 
} 

if (isset($_POST['submitForm2'])) 
{ 
    $firstname = $_POST['firstname']; 
    $lastname = $_POST['lastname']; 
    $dob = $_POST['dob']; 
    $age = $_POST['age']; 
    $sql = "INSERT INTO info (firstname, lastname, dob, age) 
     VALUES ('{$firstname}', '{$lastname}', '{$dob}', '{$age}')"; 
    if ($conn->query($sql) === TRUE) 
    { 
     echo "New record created successfully"; 
    } 
    else 
    { 
     echo "Error: " . $sql . "<br />" . $conn->error; 
    } 
} 
else 
{ 
    echo "Are you sure you enter a firstname and the name of your html submit 
     is submitForm"; 
} 

$conn->close(); 
?> 

我已经在互联网上搜索,看到很多例子,从出生日期计算年龄,但我在将结果嵌套在名为age的表单字段中并且将数据保留在数据库中时找不到任何相关内容。

+0

感谢伊万卡! –

回答

-1

有些朋友给出了我的答案的解决方案,但不幸的是有人必须擦除它,所以我给解决方案,因为我预先要复制脚本!非常感谢ho提供的这个脚本!

<head> 
<meta charset="utf-8"> 
<meta http-equiv="X-UA-Compatible" content="IE=edge"> 
<title>form practice</title> 

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> 
    <script> 
    $(document).ready(function(){ 

     $("#dob").change(function(){ 
      var value = $("#dob").val(); 
      var dob = new Date(value); 
      var today = new Date(); 
      var age = Math.floor((today-dob)/(365.25 * 24 * 60 * 60 * 1000)); 
      if(isNaN(age)) { 

      // will set 0 when value will be NaN 
      age=0; 

      } 
      else{ 
       age=age; 
      } 
      $('#age').val(age); 

     }); 

    }); 
    </script> 
</head> 

<body> 

<div class="form2" id="form2" name="form2"> 

<form action="php/form2.php" method="post" id="Personalinfo"> 

<label for="fname">Name:</label> 
<input type="text" id="fname" name="firstname" placeholder="Client 
Name.."> 

<label for="lname">Lastname:</label> 
<input type="text" id="lname" name="lastname" placeholder="Client 
Lastname.."> 

<label for="dob">Birthday:</label> 
<input type="text" id="dob" name="dob" placeholder="yyyy/mm/dd.."> 

<label for="age">Age:</label> 
<input type="text" id="age" name="age" placeholder="Client Age.."> 

<input type="submit" name="submitForm2" value="Submit"> 


</form> 
</div> 

</body> 
</html> 
+0

你的问题使用PHP和MySQL陈述,但你的答案既不使用这些东西,并使用JavaScript。 – Martin

+0

马丁答案不是我的!别人给了脚本作为替代方式来做我想要的东西,为了别人的帮助,我只是重现别人的回答!谢谢 –

+0

但是,您正在问一个关于PHP/MySQL的问题,并且您正在说[如何]您想要的答案是Javascript!请澄清你真正想要的,并且实际上正在努力实现!谁的答案并不重要,重要的是,你说这个答案是你需要的答案,尽管它与你问的实际问题没有直接关系! – Martin

0

您可以通过365.25

除以天数得到的年龄一个很好的近似我有充分根据,今天年龄有活着的人在47 - 所以你不能使用PHP时间值存储他们的出生日期(如果你使用的是32位系统,你将在2038年之后出现问题)。因此,我会建议在数据库中进行数学运算。事实上,the very first example在MySQL手册说明了这个确切的问题(这个方法不受当您通过365.25分创造了整偏差问题的影响):

SELECT name, birth, CURDATE(), 
TIMESTAMPDIFF(YEAR,birth,CURDATE()) AS age 
FROM pet; 

但是为什么你在数据库中存储的老去?在未来的某个时间点将是错误的。如果您有出生日期,则可以在检索数据时进行计算。

+0

非常感谢您的信息!真的帮助我! –

+0

不要忘记标记为答案。 – KaoriYui

0
  • 1)你想使用PHP DateTime类中。您制作两个DateTime对象,一个用于生日,另一个用于现在,然后比较这两个对象。

    有了,我们并不需要深入时区语言环境的假设(这是完全可能的,但超出这个问答& A的范围),这会给你一个准确的结果。

    DateTime类实例:

    (PHP 5.6。29)

    $now = new DateTime(); 
    
    // Note your SQL field should be a `DATE` column 
    // and formatted YYYY-MM-DD 
    // assumed in this example $sqlData['dob'] = "2015-01-10"; 
    
    $birthday = new DateTime($sqlData['dob']); 
    $age = $now->diff($birthday); 
    // 
    print "person is ".$age['y']." years old."; 
    

    $年龄的完整输出低于print_r($age)

    DateInterval Object 
    (
        [y] => 2 
        [m] => 6 
        [d] => 23 
        [h] => 5 
        [i] => 34 
        [s] => 11 
        [days] => 935 
    ) 
    

  • 2)正如Symcbean提到的,你真不” t需要(并且可能不应该)添加pe的年龄rson进入数据库,如:

    这将在未来某个时间点出错。如果您有出生日期,则可以在检索数据时进行计算。

    - symcbean


  • 3)当你使用MySQL的导向作用,在PHP对象积极的痕迹;你仍然受到SQL注入和潜在的数据库滥用。不要将PHP变量直接插入到SQL中,而是希望成为Using prepared statements and parameterized queries。在你有机会学习任何不良的PHP习惯之前,现在就做吧!


来源:

+0

非常感谢! –

+0

@ThanosZacharias如果有用的话可随时+1这个答案';-)' – Martin