Validating date of birth from web form with php

I am using php to validate the age that user submit from a registration form. When i submits the form nothing happens, i just keep been redirected to the same page.
Here is my code behind

if(isset($_POST['register'])){
	

$dateOfBirth = $_POST['DOB'];
$today = date("Y-m-d");
$diff = date_diff(date_create($dateOfBirth), date_create($today));

if($diff->format('%y%') < 16){
	die("you are too young to register");
}

In what format is the user input date coming in?

var_dump($_POST['DOB']);

Did you use a date picker input (`type=“date”)?

Well I don’t see where you tell the script to do anything if the date is valid. What do you expect/want to happen?

normal html date format input type=“date” name=“DOB” id=“DOB”

Yes, exactly what i am using.

I have already tried var_dump but no luck.

You’re missing an ending curly brace. Also, replace if(isset($_POST['register'])) with if($_SERVER[REQUEST_METHOD'] == 'POST').

No luck, I want to validate the DOB before it gets to the database so i need to validate the post variable but even when i try to echo the $_POST[‘register’], i am getting no output.

That is the point. Stop relying on those amaetur hacks and start using the correct way. The reason why you see nothing happening is because “register” doesn’t exist in the POST array. Therefore, you can’t even get pass a simple form submission checking. I have already given you the correct form submission checking above.

HI,

Is your html submit button has name=“register” ?

I’m guessing that is the case, on account of its use in the initial if clause.
But @spaceshiptrooper is correct in saying the request method should be tested instead of that.

1 Like

Is “%y%” valid as a format, or would it just ignore the trailing percent-symbol?

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.