Computing.Net > Forums > Programming > Php email form code help

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Php email form code help

Reply to Message Icon

Name: ian_ok
Date: January 5, 2007 at 09:29:11 Pacific
OS: win xp 98 & 2k
CPU/Ram: p3 512
Comment:

I've 2 questions:

1. For the code below, how would I add a validation for if the field subject doesn't equal 'Level 1' 'Level 2' or 'Level 3' it outputs the Error message.

elseif ($subject != "Level 1")
{ echo "Error"; }

Question 2. I use the following code to stop certain submissions via my email forms,

function validate_fields($s) {
$forbidden = array('\r', '\n', 'banned words', 'more banned words', 'etc', 'etc');
foreach ($forbidden as $f)
if (strpos($s, $f) !== false) return false;
return true;
}
BUT how could I shorten the email file to verify the fields as I have nearly 100 fields which are numbered field1 TO field87.

code:
elseif (!validate_fields($_POST['field2']))
{ echo "Error"; }
elseif (!validate_fields($_POST['field3']))
{ echo "Error"; }
elseif (!validate_fields($_POST['field4']))
{ echo "Error"; }
elseif (!validate_fields($_POST['field5']))
{ echo "Error"; }

AND SO ON.

Thanks for any help.

Ian

Venta Sanlucar Sales Sotogrande



Sponsored Link
Ads by Google

Response Number 1
Name: ian_ok
Date: January 5, 2007 at 09:38:11 Pacific
Reply:

Ok I've answered Question 1 myself by using the && instead of the || (not sure why OR doesn't work and AND does!)

Any tips on Question 2???

Thanks Ian

Venta Sanlucar Sales Sotogrande


0

Response Number 2
Name: Michael J (by mjdamato)
Date: January 6, 2007 at 10:30:33 Pacific
Reply:

"(not sure why OR doesn't work and AND does!)"

When you use OR, then if any of the conditions is true then it resolves to true. With AND all the conditions must be true for it to resolve to true. So,

if ($subject != "Level 1" || $subject != "Level 1")

Would ALWAYS resolve to true, because at least one of thos statements would be true.

As to Q2, you could do something like this (assuming all the fields are sequential):

$error = false;
for ($i=1; $i<=87; $i++) {
if (validate_fields($_POST['field'.$i])) {
$error = true;
}
}

if ($error) {
//display error msg
} else {
//send the email
}

Michael J


0

Response Number 3
Name: ian_ok
Date: January 9, 2007 at 12:16:39 Pacific
Reply:

Hi Micheal

Thanks for that, me to confused with the AND / OR.

There are 3 forms all processed in one mail send file and by using the && it works the || doesn't....Each form has it OWN individual subject... Yes strange.

I'll do a test with your suggestion for Q2.

Thanks again.

Ian

Venta Sanlucar Sales Sotogrande


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Php email form code help

PHP Email Form - Input Type Text www.computing.net/answers/programming/php-email-form-input-type-text-/17036.html

PHP and Form Posting to Email www.computing.net/answers/programming/php-and-form-posting-to-email/12295.html

Need help with an online email form www.computing.net/answers/programming/need-help-with-an-online-email-form/4458.html