Your code at the moment is effectively:
if (! check) {
echo();
return;
}
if (! check) {
echo();
return;
}
But return
outside of a function isn't going to do anything - there's nowhere for PHP to return control to.
What you need to do is switch those checks so that they're formatted as:
if (isset($_POST['submit']))
{
if (!nameCheck($name))
{
die('<script type="text/javascript"> alert("Fail name!")</script>');
}
elseif
......
}
else
{
mail ("email@email.com", "this", "message");
echo '<script type="text/javascript"> alert("Success!")</script>';
}
Using die
will print out the contents of the string, and stop your code being executed right away.
As to functions....
if you're going to be using their return values, you need to make sure that they always return something. At the moment, emailCheck
is:
function emailCheck($email)
{
$regex = "/^[*\w]{1,25}+@[*\w]{1,20}+\.[*\w]{1,10}$/";
if (preg_match($regex, $email))
{
return true;
}
}
If there's no match, then it will return null
; though when you check that in your if
statement, that will evaluate to false
, so your check won't notice. When you call return
, your function immediately stops running, and control passes back to the code that called it, so you can write your check functions as:
function myCheck($parameter) {
if ($checkhere == 'something') {
return true; // returns true to the calling function
// stops running the code in this function
}
// The only way this will be run is if the check has failed, so we
// don't need an else
return false;
}
It's exactly the same as using an else
, but slightly less typing - it's a difference in style which way you choose.