Home > Mobile >  why function and if statement don't work on php
why function and if statement don't work on php

Time:03-03

I have a PHP project and I need to put the birth date so I put it in function this function submit doesn't work with the if statement in the PHP file what I can do with it?


<body>
<?php


function submitt() {
    echo "<h3>your age is: $age  years old </h3>" ; 
    if(isset($_POST['submit'])) {
        submitt();
?>
<form method='POST'>
<h1>Please input your name and birthdate:</h1>
<h3>first name:</h3>
 <input type="text" name="name">
 <h3>second name:</h3>
 <input type="text" name="secName" >
 <h3>your birth date:</h3>
 <input type="text" name="birth" >
 <input type="submit" name="submit" value="Submit " /> 
 </form>
<?php
$name = $_POST['name'];
$secName=$_POST['secName'];
$now_date= date("Y");
$birth=$_POST['birth'];
$age=$now_date-$birth;
echo "<h3> Hello $name $secName </h3>";
?>

CodePudding user response:

Your code is wrong. Build the function and call it outside. And close the function and the if statement with }

<?php


function submitt() {
 echo "FUNCTION SUBMIT";
}

echo "<h3>your age is: $age  years old </h3>" ; 

   if(isset($_POST['submit'])) {
        submitt();
    }
?>

CodePudding user response:

You missed function and if closing brackets.

Correct code:

<?php
   function submitt() {
       $name = $_POST['name'];
       $secName=$_POST['secName'];
       $now_date= date("Y");
       $birth=$_POST['birth'];
       $age=$now_date-$birth;
       echo "<h3> Hello $name $secName </h3>";
       echo "<h3>your age is: $age  years old </h3>" ;
    }
    if(isset($_POST['submit'])) {
        submitt();
    }
?>
    <form method='POST'>
        <h1>Please input your name and birthdate:</h1>
        <h3>first name:</h3>
        <input type="text" name="name">
        <h3>second name:</h3>
        <input type="text" name="secName" >
        <h3>your birth date:</h3>
        <input type="text" name="birth" >
        <input type="submit" name="submit" value="Submit " /> 
    </form>
  • Related