Page 1 of 1

Contact form - begginer's question

Posted: Tue Mar 01, 2011 2:35 pm
by remocomp
Hi,

I created a contact form, and I'm trying to get the error messages to appear on the same page (or at a popup window).
Here's the current code:

Code: Select all

<?php
if(isset($_POST['email'])) {
     
    $email_to = "myemail@mysite.com";
    $email_subject = "Contact form";
     
     
    function died($error) {
        echo "Error:<br /><br />";
        echo $error."<br /><br />";
        echo "Please go back and fix these errors.<br /><br />";
        die();
    }
     
    // validation expected data exists
    if(!isset($_POST['first_name']) ||
        !isset($_POST['email']) ||
        !isset($_POST['comments'])) {
        died('We are sorry, but there appears to be a problem with the form you submitted.');      
    }
     
    $first_name = $_POST['first_name']; // required
    $email_from = $_POST['email']; // required
    $comments = $_POST['comments']; // required
     
    $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 you entered does not appear to be valid.<br />';
  }
    $string_exp = "^[a-z .'-]+$";
  if(!eregi($string_exp,$first_name)) {
    $error_message .= 'Full name is required.<br />';
  }
  if(strlen($comments) < 2) {
    $error_message .= 'Message is required.<br />';
  }
  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 .= "First Name: ".clean_string($first_name)."\n";
    $email_message .= "Email: ".clean_string($email_from)."\n";
    $email_message .= "Comments: ".clean_string($comments)."\n";
     
     
// create email headers
$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
}
?>
Any idea how can I make it happen?

Re: Contact form - begginer's question

Posted: Tue Mar 01, 2011 3:41 pm
by kristen
Using Javascript (AJAX, JQuery, so forth) is the way to go here. You can accomplish what you want with PHP (and you still will NEED to scrub the data once submitted) but using something like AJAX would give the user a better experience.

Re: Contact form - begginer's question

Posted: Tue Mar 01, 2011 4:12 pm
by John Cartwright
While I agree ajax is good for interactive interfaces, if you wanted to avoid javascript then in pseudo it would look something like

[syntax]$errors = array();
if (form_is_posted()) {
check_field_1
- if field_1 is invalid, then $errors['field_1'] = 'Some error for this field;
check_field_2
- if field_2 is invalid, then $errors['field_2'] = 'Some error for this field;
etc

if (count($errors) == 0) {
we have a form that was submitted and looks like valid, lets do something, and possibily redirect afterwards
}
}

if (field1 has errors) {
show field 1 error
}
showfield1(); - repopulate value="" attribute if it exists

if (field2 has errors) {
show field 2 error
}
showfield2(); - repopulate value="" attribute if it exists[/syntax]