Forum Moderators: coopster
this is a simple script i wrote to sign up to a mailing list. as far as i can see, this thing should work, but it is working very funny. if i submit with a blank textbox, ie 'add' not set, it doesnt catch that, but prints 'thanks for signing up...'
any idea as to why the script does not stop after executing the first if statement...?
thanks
k
<?php
if (!isset($_POST['add']))
{
echo 'HA! HA! very funny, now ENTER an address befdore you submit, moron!';
}
else
{
$add = $_POST['add'];
if ($add == 'test')
{
echo 'Is it so hard to proviude a proper address....?';
}
else
{
$fh = fopen('adds.php3', 'a');
fwrite($fh, $add);
fclose($fh);
echo 'Thank You for signing up to the list. you will get regular updates.';
//header("Location: index.php3?art=$art");
}
}
?>
When you have an html form that's submitted, there will be elements in the $_POST or $_GET array for each field, whether it's filled in or not. But when it's not filled in, it will be 'empty.' So for this kind of thing use empty.
<?php
$emailpattern = '^([._a-zA-Z0-9-]){2,255}@([._a-zA-Z0-9-]){2,255}\.([.a-zA-Z]){2,7}$';
if (!isset($_POST['add']) ¦¦ empty($_POST['add'])) {
echo 'HA! HA! very funny, now ENTER an address befdore you submit, moron!';
} else if (!ereg($pattern, $_POST['add'])) {
echo 'Is it so hard to proviude a proper address....?';
} else {
$add = $_POST['add'];
$fh = fopen('adds.php3', 'a');
fwrite($fh, $add);
fclose($fh);
echo 'Thank You for signing up to the list. you will get regular updates.';
//header("Location: index.php3?art=$art");
}
?>
also gives you a proper pattern test for email addresses.
always remember to replace pipe chars with real pipe characters as this forum software breaks pipes
Regular Expressions [webmasterworld.com]