SoapDodger;10985721 wrote:
And the first part just doesn't seem to work full stop.
When coding, and especially when asking for help about coding, a statement such as the above is of absolutely no help. If you need help with something, it obviously isn't working (as you wishes it to do), otherwise you wouldn't have asked in the first place.
SoapDodger;10985721 wrote:
Regardless of whether they select the tick box for terms and conditions it runs the script 😛
This however, is the kind of information you need to provide. It also makes the error very very easy to spot.
if (isset($_POST["checkbox"])) { echo $_POST["checkbox"];}
If you used a common form of coding style and indentation, like this
if (isset($_POST["checkbox"]))
{
echo $_POST["checkbox"];
}
... it should be obvious that the code block executed for that if conditon contains only one line of code: echo $POST["checkbox"];
Yet if I understand you correctly, you don't wish the rest of the code to execute unless this if condition is true. Should this be the case, move the rest of the code inside this block of code.
And here you should be able to understand why I needed you to state that "Regardless of whether they select the tick box for terms and conditions it runs the script", since the above code is perfectly valid, and even makes sense (until your particaular wishes are known). If $POST['checkbox'] wasn't set, you can't echo it, and since we don't know what you want to do unless you tell us exactly what that is, we would believe that you wanted to show the value of $_POST['checkbox'] if it is set, rather than do something that the code doesn't do.
Your email validation function tells me that you either need to turn on error reporting, since the ereg family of functions has been deprecated since PHP 5.3, which came out over a year ago, or upgrade whatever version you are using to PHP 5.3.6 and then possibly turn on error reporting. Come PHP 6, these functions will no longer exist.
Moreover, I don't believe that it's good coding practice for a function such as this one to do redirects or exit program execution. In my opinion, you should do it along these lines instead
/* the function that validates email addresses now does NOTHING, other than
validate the email address. The choice to exit, redirect, display feedback to the
user etc now lies where it belongs: somewhere else.
*/
function email_valid($email)
{
if ('the actual validation test goes here')
return true;
else
return false;
}
$errors = array();
/* This part of code, rather than send the user away when they make a misstake
filling out your form, now gets informative feedback and can correct their
misstake.
*/
if (email_valid($email))
{
/* do what you need to with the validated email address:
send an email, store address in database etc
*/
}
else
{
$errors[] = 'Invalid email address. Please verify your input';
}
# And then, where you display your form, right before the actual form is shown
foreach ($errors as $e)
{
printf('<div style="color: red;">%s</div>', $e);
}
# and then inside the form
printf('<input type="text" name="email" value="%s" />',
isset($_POST['email']) ? htmlentities($_POST['email'], ENT_QUOTES, 'utf-8') : ''
);
And finally, when it comes to validating email addresses, if you want to do it properly you should google for something like "php email validation rfc compliant" which finds you this page among others. The linked page claims to
validates all parts of a given email address, according to RFCs 1123, 2396, 3696, 4291, 4343, 5321 & 5322.
.
If you don't care about properly accepting valid email addresses as valid, then you might as well just use [man]filter_var[/man]
filter_var($email, FILTER_VALIDATE_EMAIL)
which returns false on "invalid" email addresses, except that it doesn't properly validate email addreses. However, while failing its job, it seems to be failing somewhat less than your approach, at least when testing a few valid (according to RFC 3696) email addresses, such as
$valid_emails = array(
'Abc\@def@example.com',
'Fred\ Bloggs@example.com',
'Joe.\\Blow@example.com',
'"Abc@def"@example.com',
'!def!xyz%abc@example.com',
'customer/department=shipping@example.com',
'$A12345@example.com'
);
Your function fails validating all of them, filter_var "only" fails on 3 out of 7.