I have a job application form returning a person's details among other things.
Right now, these are returned on separate lines. And if there are no entries in any field, there is a blank line in the email that is sent.
I'd like to make an IF
statement so that if there is no entry in any field, it will return "Not Applicable" in the mail.
example:
$nrel1 = $_POST['nrel1'];
$nrel11_name = $_POST['nrel1_name'];
$nrel1_age = $_POST['nrel1_age'];
$nrel1_gender = $_POST['nrel1_gender'];
$nrel1_education = $_POST['nrel1_education'];
$nrel2_employment = $_POST['nrel2_employment'];
Now if the applicant makes no entry in the $nrel1_age
field, I want it to return "Not Applicable" in the mail.
function valueOrNotApplicable($array, $key)
{
if (isset($array[$key]) && !empty($array[$key]))
return $array[$key];
else
return 'Not Applicable';
}
$nrel1 = valueOrNotApplicable($_POST, 'nrel1');
$nrel11_name = valueOrNotApplicable($_POST, 'nrel1_name');
$nrel1_age = valueOrNotApplicable($_POST, 'nrel1_age');
$nrel1_gender = valueOrNotApplicable($_POST, 'nrel1_gender');
$nrel1_education = valueOrNotApplicable($_POST, 'nrel1_education');
$nrel2_employment = valueOrNotApplicable($_POST, 'nrel2_employment');
It's good to check both isset()
and !empty()
. If you don't check for isset()
then you may get "PHP Notice: Undefined index" warnings.
if(!isset($nrel1_age) && (trim($nrel1_age)!=''))
return "Not applicable";
That should do the trick. Cheers
EDIT
Forgot the trim part.
$nrel1_age = empty($_POST['nrel1_age']) ? 'Not Applicable' : $_POST['nrel1_age'];
You can use conditional operator as below
$nrel1_age = isset($_POST['nrel1_age']) && trim($_POST['nrel1_age'])!=''?$_POST['nrel1_age']:'Not Available';
Try
$nrel1 = (!empty(trim($nrel1_age))) ? $_POST['nrel1'] : "Not applicable";