Creating Required Comments Box

Dear Everyone,

I have been struggling with how to make my comments box required. Here is my HTML and PHP code. I believe the problem is textarea versus textfield, since a comments textfield does work - I need a textarea, though. I’ve been researching this a lot, so any help you can give me would be greatly appreciated! I’m including the entire form in PHP, as I am no longer sure which part holds the answer.

Thank you!!
Chava

HTML

<textarea name="comments" cols="26" rows="5" style="background-color:#fff;"></textarea>

[php]

<?php if(isset($_POST['email'])) { $email_to = "[email protected]"; $email_subject = "Contact Form"; function died($error) { echo "

Sorry, we found errors in your form submission.

"; echo $error."

"; echo "Please fix these errors here, so I can properly respond to your submission.

"; die(); } // validation expected data exists if(!isset($_POST['fname']) || !isset($_POST['lname']) || !isset($_POST['email']) || !isset($_POST['telephone']) && !isset($_POST['comments'])) { died('We are sorry, but there appears to be a problem with the form your submitted.'); } $fname = $_POST['fname']; $lname = $_POST['lname']; $email_from = $_POST['email']; $telephone = $_POST['telephone']; $comments = $_POST['comments']; $error_message = ""; $email_exp = "^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$"; if(!eregi($email_exp,$email_from)) { $error_message .= 'The e-mail address is either blank or in the wrong format.
'; } $string_exp = "^[a-z .'-]+$"; if(!eregi($string_exp,$fname)) { $error_message .= 'The first name is either blank or includes invalid characters.
'; } /*$string_exp = "^[a-z .'-]+$"; if(!eregi($string_exp,$lname)) { $error_message .= 'The name you entered does not appear to be valid.
'; }*/ $string_exp = "^[a-z .'-]"; if(!eregi($string_exp,$comments)) { $error_message .= 'The comments box is blank.
'; } if(strlen($error_message) > 0) { died($error_message); } $email_message = "Form details below.\n\n"; function clean_string($string) { $bad = array("content-type","bcc:","to:","cc:","href"); return str_replace($bad,"",$string); } $email_message .= "fname".clean_string($fname)."\n"; $email_message .= "lname".clean_string($lname)."\n"; $email_message .= "email".clean_string($email_from)."\n"; $email_message .= "telephone".clean_string($telephone)."\n"; $email_message .= "comments".clean_string($comments)."\n"; $headers = 'From: '.$email_from."\r\n". 'Reply-To: '.$email_from."\r\n" . 'X-Mailer: PHP/' . phpversion(); @mail($email_to, $email_subject, $email_message, $headers); ?> <? } ?>

[/php]

Don’t look to see if its set (isset()), look to see if its empty (empty()). Also, never use die() for error reports, all it does is force the user to go back and start all over again, i almost never bother with it.

Something like [php]<?php
function clean_string($string) {
$bad = array(“content-type”,“bcc:”,“to:”,“cc:”,“href”);
return str_replace($bad,"",$string);
}

if(isset($POST[‘submit’])) { // this should the name of your submit button
$email_to = "[email protected]";
$email_subject = “Contact Form”;
$error_message = “”;
$email_exp = "^[A-Z0-9.
%-]+@[A-Z0-9.-]+.[A-Z]{2,4}$";
$string_exp = “^[a-z .’-]+$”;

// validation expected data exists
if(empty($_POST[‘fname’]) {
$error[‘fname’] = “Firstname is Requird”;
} elseif(empty($_POST[‘lname’]) {
$error[‘lname’ = “Last name is Required”;
} elseif(empty($_POST[‘email’])) {
$error[‘email’] = “Email address is Required”;
} elseif(empty($_POST[‘telephone’]))
$error[‘telephone’] = “A Contact Number is Required”;
} elseif(empty($_POST[‘comments’])) {
$error[‘comments’] = “You must include a comment”;
} else {
// nothing is empty, proceed
if(!preg_match($string_exp,$fname)) {
$error_message .= ‘The first name is either blank or includes invalid characters.
’;
} else {
$fname = $_POST[‘fname’];
}
$lname = $_POST[‘lname’];

  if(!preg_match($email_exp,$email_from)) {
     $error_message .= 'The e-mail address is in the wrong format.<br />';
  } else {
     $email_from = $_POST['email'];
  }
  $telephone = $_POST['telephone'];
  $comments = $_POST['comments'];

  if(!error_message) {
    exit($error_message);
  } else {
    $email_message = "Form details below.\n\n";
    $email_message .= "fname".clean_string($fname)."\n";
    $email_message .= "lname".clean_string($lname)."\n";
    $email_message .= "email".clean_string($email_from)."\n";
    $email_message .= "telephone".clean_string($telephone)."\n";
    $email_message .= "comments".clean_string($comments)."\n";

    $headers = 'From: '.$email_from."\r\n".'Reply-To: '.$email_from."\r\n" .'X-Mailer: PHP/' . phpversion();@mail($email_to, $email_subject, $email_message, $headers);
  }

}
}?>[/php]

To show the error messages, just call them like you would a sql result

Sponsor our Newsletter | Privacy Policy | Terms of Service