I can't immediately see why it wouldn't be working. You might have spaces in some of the fields which would then throw the ( ! $variable) check because it does actually contain something (spaces).
You could try something (which I haven't tested here and now) like:
// Function to fetch values in $_POST array.
// Returns NULL if value doesn't exist (wasn't submitted) or was only spaces.
function get_post($post_value)
{
if (isset($_POST["$post_value"] AND strlen(trim($_POST["$post_value"])) != 0)) {
return trim($_POST["$post_value"]);
}
else {
return NULL;
}
}
$subject = get_post('subject');
$name = get_post('name');
// etc...
if ( ! $subject OR ! $name) { // etc...
echo "bad...";
}
This is pretty shallow in terms of validation checks but it might be fine.
As for spam... that's a bit more complicated. There are lots of PHP classes/libraries etc. that help with this:
http://recaptcha.net/http://www.phpcaptcha.org/etc.
Some sites will get by with something as simple as a hidden field that shouldn't be filled in (because it's hidden). Spam bots will generally fill this out automatically - so you can assume that it's spam if it's filled out... Not a 100% solution but it works for a lot of basic sites. It obviously won't deter human spammers though.
The spam question is pretty big...
Hope this helps.