Hi–I’m working with a free PHP contact form script I found online. I am really new to PHP and need some help: I’d like to remove the “$email” and “$subject” fields and make the “$name” field optional. Can anyone give me some pointers… I’d really appreciate it!!!
Here’s the code:
[php]
<?php if (session_id() == '') session_start(); include_once "contact-config.php"; $error_message = ''; if (!isset($_POST['submit'])) { showForm(); } else { //form submitted $error = 0; if(!empty($_POST['name'])) { $name[2] = clean_var($_POST['name']); } else { $error = 1; $name[3] = 'color:#FF0000;'; } if(!empty($_POST['email'])) { $email[2] = clean_var($_POST['email']); if (!validEmail($email[2])) { $error = 1; $email[3] = 'color:#FF0000;'; $email[4] = 'Invalid email'; } } else { $error = 1; $email[3] = 'color:#FF0000;'; } if(!empty($_POST['subject'])) { $subject[2] = clean_var($_POST['subject']); if (function_exists('htmlspecialchars')) $subject[2] = htmlspecialchars($subject[2], ENT_QUOTES); } else { $error = 1; $subject[3] = 'color:#FF0000;'; } if(!empty($_POST['message'])) { $message[2] = clean_var($_POST['message']); if (function_exists('htmlspecialchars')) $message[2] = htmlspecialchars($message[2], ENT_QUOTES); } else { $error = 1; $message[3] = 'color:#FF0000;'; } if(empty($_POST['captcha_code'])) { $error = 1; $code[3] = 'color:#FF0000;'; } else { include_once "contact-securimage.php"; $securimage = new Securimage(); $valid = $securimage->check($_POST['captcha_code']); if(!$valid) { $error = 1; $code[3] = 'color:#FF0000;'; $code[4] = 'Incorrect code'; } } if ($error == 1) { $error_message = 'Please correct/enter field(s) in red.'; showForm(); } else { if (function_exists('htmlspecialchars_decode')) $subject[2] = htmlspecialchars_decode($subject[2], ENT_QUOTES); if (function_exists('htmlspecialchars_decode')) $message[2] = htmlspecialchars_decode($message[2], ENT_QUOTES); $body = "$name[0]: $name[2]\r\n\r\n"; $body .= "$email[0]: $email[2]\r\n\r\n"; $body .= "$message[0]:\r\n$message[2]\r\n"; if (!$from) $from_value = $email[2]; else $from_value = $from; $headers = "Content-type: text/plain; $charset" . "\r\n"; $headers .= "From: $from_value" . "\r\n"; $headers .= "Reply-To: $email[2]" . "\r\n"; mail($to,"$subject_prefix - $subject[2]", $body, $headers); if (!$thank_you_url) { if ($use_header_footer) include $header_file; echo ''."\n"; echo '{$name[0]} | |
{$email[0]} | {$email[4]} |
{$subject[0]} | |
{$message[0]} | {$message[2]} |
{$code[0]} | {$code[4]}
(Please enter the text in the image above. Text is not case sensitive.) Click here if you cannot recognize the code. |
All fields are required. | |
[/php]