Ricky55 Posted July 4, 2008 Share Posted July 4, 2008 Hi I have a form setup which works fine but it requires that all fields are complete before you submit. I want all fields to be required apart from the email address, could anyone have a look at the script and tell me how it needs to be modified to take out the email field from being required. Thanks By the way I am a complete newbie with PHP. the script <?php $after = "thanks.html"; $oops = "oops.html"; if (!isset($_POST['submit']) || $_SERVER['REQUEST_METHOD'] != "POST") { exit("<p>You did not press the submit button; this page should not be accessed directly.</p>"); } else { $exploits = "/(content-type|bcc:|cc:|document.cookie|onclick|onload|javascript|alert)/i"; $profanity = "/(beastial|bestial|blowjob|clit|cock|cum|cunilingus|cunillingus|cunnilingus|cunt|ejaculate|fag|felatio|fellatio|****|fuk|fuks|gangbang|gangbanged|gangbangs|hotsex|jism|jiz|kock|kondum|kum|kunilingus|orgasim|orgasims|orgasm|orgasms|phonesex|phuk|phuq|porn|pussies|pussy|spunk|xxx)/i"; $spamwords = "/(viagra|phentermine|tramadol|adipex|advai|alprazolam|ambien|ambian|amoxicillin|antivert|blackjack|backgammon|texas|holdem|poker|carisoprodol|ciara|ciprofloxacin|debt|dating|porn)/i"; $bots = "/(Indy|Blaiz|Java|libwww-perl|Python|OutfoxBot|User-Agent|PycURL|AlphaServer)/i"; if (preg_match($bots, $_SERVER['HTTP_USER_AGENT'])) { exit("<p>Known spam bots are not allowed.</p>"); } foreach ($_POST as $key => $value) { $value = trim($value); if (empty($value)) { exit("<p>Empty fields are not allowed. Please go back and fill in the form properly.</p>"); } elseif (preg_match($exploits, $value)) { exit("<p>Exploits/malicious scripting attributes aren't allowed.</p>"); } elseif (preg_match($profanity, $value) || preg_match($spamwords, $value)) { exit("<p>That kind of language is not allowed through our form.</p>"); } $_POST[$key] = stripslashes(strip_tags($value)); } if (!ereg("^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,6})$",strtolower($_POST['email']))) { exit("<p>That e-mail address is not valid, please use another.</p>"); } $recipient = "[email protected], [email protected]"; $subject = "Web Site Enquiry"; $message .= "Name: {$_POST['name']} \n"; $message .= "Email: {$_POST['email']} \n"; $message .= "Contact: {$_POST['contact']} \n"; $message .= "Address: {$_POST['address']} \n"; $message .= "Postcode: {$_POST['postcode']} \n"; $message .= "Vehicle: {$_POST['vehicle']} \n"; $message .= "Damage: {$_POST['damage']} \n"; $headers .= "Reply-To: <{$_POST['email']}>"; if (mail($recipient,$subject,$message,$headers)) { echo "<meta HTTP-EQUIV=\"REFRESH\" content=\"0; url=$after\">"; } else { echo "<meta HTTP-EQUIV=\"REFRESH\" content=\"0; url=$oops\">"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/113265-problem-with-php-form-script/ Share on other sites More sharing options...
papaface Posted July 4, 2008 Share Posted July 4, 2008 <?php $after = "thanks.html"; $oops = "oops.html"; if (!isset($_POST['submit']) || $_SERVER['REQUEST_METHOD'] != "POST") { exit("<p>You did not press the submit button; this page should not be accessed directly.</p>"); } else { $exploits = "/(content-type|bcc:|cc:|document.cookie|onclick|onload|javascript|alert)/i"; $profanity = "/(beastial|bestial|blowjob|clit|cock|cum|cunilingus|cunillingus|cunnilingus|cunt|ejaculate|fag|felatio|fellatio|****|fuk|fuks|gangbang|gangbanged|gangbangs|hotsex|jism|jiz|kock|kondum|kum|kunilingus|orgasim|orgasims|orgasm|orgasms|phonesex|phuk|phuq|porn|pussies|pussy|spunk|xxx)/i"; $spamwords = "/(viagra|phentermine|tramadol|adipex|advai|alprazolam|ambien|ambian|amoxicillin|antivert|blackjack|backgammon|texas|holdem|poker|carisoprodol|ciara|ciprofloxacin|debt|dating|porn)/i"; $bots = "/(Indy|Blaiz|Java|libwww-perl|Python|OutfoxBot|User-Agent|PycURL|AlphaServer)/i"; if (preg_match($bots, $_SERVER['HTTP_USER_AGENT'])) { exit("<p>Known spam bots are not allowed.</p>"); } foreach ($_POST as $key => $value) { $value = trim($value); if (empty($value)) { if ($key != "email") { exit("<p>Empty fields are not allowed. Please go back and fill in the form properly.</p>"); } } elseif (preg_match($exploits, $value)) { exit("<p>Exploits/malicious scripting attributes aren't allowed.</p>"); } elseif (preg_match($profanity, $value) || preg_match($spamwords, $value)) { exit("<p>That kind of language is not allowed through our form.</p>"); } $_POST[$key] = stripslashes(strip_tags($value)); } if (!ereg("^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,6})$",strtolower($_POST['email']))) { exit("<p>That e-mail address is not valid, please use another.</p>"); } $recipient = "[email protected], [email protected]"; $subject = "Web Site Enquiry"; $message .= "Name: {$_POST['name']} \n"; $message .= "Email: {$_POST['email']} \n"; $message .= "Contact: {$_POST['contact']} \n"; $message .= "Address: {$_POST['address']} \n"; $message .= "Postcode: {$_POST['postcode']} \n"; $message .= "Vehicle: {$_POST['vehicle']} \n"; $message .= "Damage: {$_POST['damage']} \n"; $headers .= "Reply-To: <{$_POST['email']}>"; if (mail($recipient,$subject,$message,$headers)) { echo "<meta HTTP-EQUIV=\"REFRESH\" content=\"0; url=$after\">"; } else { echo "<meta HTTP-EQUIV=\"REFRESH\" content=\"0; url=$oops\">"; } } ?> Try that. Quote Link to comment https://forums.phpfreaks.com/topic/113265-problem-with-php-form-script/#findComment-581933 Share on other sites More sharing options...
Ricky55 Posted July 5, 2008 Author Share Posted July 5, 2008 thanks mate I appreciate the help I will try the code and let you know how I get on Quote Link to comment https://forums.phpfreaks.com/topic/113265-problem-with-php-form-script/#findComment-582067 Share on other sites More sharing options...
Ricky55 Posted July 5, 2008 Author Share Posted July 5, 2008 Hi That worked when I also removed this section of code if (!ereg("^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,6})$",strtolower($_POST['email']))) { exit("<p>That e-mail address is not valid, please use another.</p>"); } With that present even when the email is left blank it through up the error about the email address not being valid is there an way we can modify this still checks that the email address is valid when one is used but also allows no email to be used. Thanks very much Ricky55 Quote Link to comment https://forums.phpfreaks.com/topic/113265-problem-with-php-form-script/#findComment-582482 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.