Jump to content

PHP Form not working properly...


morocco-iceberg

Recommended Posts

I'm having difficulties getting my form to work properly, it seems to ignore the first 'if' section even if the fields have nothing in them and skips to the email validation. Help? Alos it would be cool if someone could help me with making it spam proof? Thanks..

 

<?php
					$subject = $_POST['subject'];
					$name = $_POST['name'];
					$email = $_POST['email'];
					$message = $_POST['message'];
					$what = $_POST['what'];
					$re = $subject;
					$to = "[email protected]";
					$msg = "$message \r\n \r\n $name, \r\n $email";

					if (!$subject||!$name||!$email||!$message||!$what){
						echo "Error: All fields must be completed!";
					}else if($what!="Green"||$what!="green"){
						echo "You got the question wrong! Please try again.";
					}else{
						$email = trim($email);
						$_ename = "/^[-!#$%&\'*+\\.\/0-9=?A-Z^_'{|}~]+";
						$_host = "([0-9A-Z]+\.)+";
						$_tlds = "([0-9A-Z]){2,4}$/i";

						if (!preg_match($_ename."@".$_host.$_tlds,$email)){
							echo "E-mail address is not valid. Please enter a valid e-mail address.";
						}else{
							mail($to, $re, $msg);
							echo "Thank-you for your enquiry, your message was succesfully sent. We'll get back to you as soon as possible.";
							}
						}
				?>

Link to comment
https://forums.phpfreaks.com/topic/195171-php-form-not-working-properly/
Share on other sites

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.