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 = "info@klds.com.au";
					$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
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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.