Display the error

I have a simple form,
http://php_class.teamluke.net/Assignment_6/index.php
I tried to create a function that outputs a welcome message only if the form is filled out

$FirstName = $_POST['FirstName'];

function welcome($FirstName) {
	
	if(isset($FirstName)) {
		
		echo "<h1>Welcome ".$FirstName."</h1>";
		
	} else {
		
		return false;
		
	}
}

But when I submit the form, I get this (blank page)


so, the error is probable simple so I want to see it, I put

<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
?>

At the top to see the error, but oh noes, it didnt work. I looked at my php.ini file and it looks good?


How can I see the error and a return false means my form wont be submitted, right?

Thanks

ok, The form works, but why does it work when nothing if input?

Where do you call the function?

I call the function here…

<?php
//if the form is filled out, do the create a function & fire it off
if(!empty($_POST)) 
{
//create welcome message function
function welcome($FirstName) {
	
	if(isset($FirstName)) {
		
		echo "<h1>Welcome ".$FirstName."</h1>";
		return true;
		
	} else {
		
		return false;
		
	}
}
//fire off the function

welcome($_POST['FirstName']);

//if the form hasnt been submitted, show it
} else {
?>
...

This is the problem, it will always be set, even if it is set to an empty string, because you are setting it when you call the finction.
You would be better checking for an empty string instead.

1 Like

ok, so just to check if its an empty strying, changed the function to

//create welcome message function
function welcome($FirstName) {
	
	if(empty($FirstName)) {
		
		echo "<h1>Welcome ".$FirstName."</h1>";
		return true;
		
	} else {
		
		return false;
		
	}
}

print_r($_POST);

//fire off the function

welcome($_POST['FirstName']);

I dont understand why the pages still shows “welcome” if nothing is entered into the form, see


doesn’t the return false in the else thing tell the function to stop, so not even submitting the form

How about:-

if(!empty($FirstName))

:wink:

2 Likes

duh, me and my stupid logic errors, so I fixed it
Thanks
what does return false even do?

Exactly what it says on the tin.

For example you could have:-

if(welcome($_POST['FirstName'])) {
    // do something
}
else {
    // do something else
}

Because the function returns a boolean.

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