Submitting date input getting auto Dec 31, 1969 when left blank

393 Views Asked by At

I am using a self hosted email marketing service.

I have built the form.... embedded it..

But when someone does not add a value for their birthday.. it defaults to Dec 31, 1969 using a date input.

I want that to be a value of nothing if they did not add their bday as ( mm/dd/yyyy)

The reason why, if i leave it as is.. everyone who doesn't complete the birthday date filed will be getting auto emails saying Happy birthday on Dec 31, 1969.

How can a check to see if its blank in php and then give it a value that wont send out auto emails? Is this even possible?

2

There are 2 best solutions below

2
Tox On
if (empty($_POST['birthday'])) 
{
  $empty_bday_flag = true;
}
else
{
  $empty_bday_flag = false;
}

The empty function tests if a value is set and not empty, if nothing is set then the user didn't fill it in, so we can set the $empty_bday_flag to true, otherwise we set it to false.

EDIT 1: As gustavo.lei pointed out one has to use empty here rather than isset.
EDIT 2: As Dharman pointed out one can simplify this to $empty_bday_flag = empty($_POST['birthday']);.

0
gustavo.lei On

I believe in this case, you should use a empty instead isset, because your form will always be sent with the "birthday" field, your user filling data or not. So, doing isset, will only verify if the birthday field is set and if the value is empty, the code will return isset as true.

I'd do something like this:

$bdayFilled = true;
if(empty($_POST['birthday']))
    $bdayFilled = false;

or a one line code

 $bdayFilled = !empty($_POST['birthday'];

In the code above, if the value of birthday is empty, $bdayFilled will be set as false else will be true.

For better understanding, I suggest you read: