Jump to content


Email Form Snippet, Advice/Help


3 replies to this topic

#1 ikonia

    Etomite Forum Fan

  • Member
  • Pip
  • 117 posts

Posted 29 October 2007 - 11:42 AM

I'm looking to put a email to form on a website I have. Its a motorsport website and there is a planned seminar/show/event type thing so I'm trying to put a form together to email the orginiser peoples requests

I've looked at the snippet libraries and found some good examples,

EmailHandler, mailform, and Lloyds "enquiry" form etc etc.

All are excellent, however over complex.

I've decided to work with the emailhandler form from the old snippet library.
http://www.etomite.com/browsesnippets.html...;lst_category=3


This seems the easiest to work with to modify.

I'm trying to modify some of the fields and add some additional fields.

So for example, the default form provides

1.) Your name
2.) Your Email Address
3.) Your Message


I'd like to do

1.) Your Name
2.) Your Email Address
3.) date you would like to attend (doesn't have to be a date field - just text)
4.) time you would like to attend (as above)
5.) number of people in your party.


Now in theory - this looks quite simple to do, add some fields and append them together for the message var

I've modified the existing code
$sendemail =
isset($_POST['sendemail']) && $_POST['sendemail']!= "{{" && $_POST['sendemail']!= "[[" &&
$_POST['sendemail']!= "[(" && $_POST['sendemail']!= "[~" && $_POST['sendemail']!= "[*" ?
$_POST['sendemail'] : "";

$name =
isset($_POST['name']) && $_POST['name']!= "{{" && $_POST['name']!= "[[" &&
$_POST['name']!= "[(" && $_POST['name']!= "[~" && $_POST['name']!= "[*" ?
$_POST['name'] : "";

$email =
isset($_POST['email']) && $_POST['email']!= "{{" && $_POST['email']!= "[[" &&
$_POST['email']!= "[(" && $_POST['email']!= "[~" && $_POST['email']!= "[*" ?
$_POST['email'] : "";

$subject =
isset($_POST['subject']) && $_POST['subject']!= "{{" && $_POST['subject']!= "[[" &&
$_POST['subject']!= "[(" && $_POST['subject']!= "[~" && $_POST['subject']!= "[*" ?
$_POST['subject'] : "";

$message =
isset($_POST['message']) && $_POST['message']!= "{{" && $_POST['message']!= "[[" &&
$_POST['message']!= "[(" && $_POST['message']!= "[~" && $_POST['message']!= "[*" ?
$_POST['message'] : "";

// check errors
$pattern = ".+@.+..+";
$error = '<font color="#FF0000">Required</font><br>';
$bad_email = '<font color="#FF0000">Make sure that you have entered a valid e-mail address.</font><br>';
if($sendemail=="sendemail"){
  if($name==""){$name_error = $error;}
  if($email==""){$email_error = $error;}
  elseif(!eregi($pattern,$email)){$email_error = $bad_email;}
  if($subject==""){$subject_error = $error;}
  if($message==""){$message_error = $error;}
}

if(($name!="")&&(eregi($pattern,$email))&&($subject!="")&&($message!="")){
  if($_SERVER["HTTP_HOST"] != "benreeves"){mail($to, $subject, $message, "From: $email");}
  $EmailHandler = "<b>Your message has been sent.</b><p>You will recieve feedback as soon as possible.";
} else {
  $EmailHandler .= "<b>Use the form below to contact via e-mail.</b>rn";
  $EmailHandler .= '<form name="EmailHandler" action="" method="post">';
  $EmailHandler .= '<input name="sendemail" type="hidden" value="sendemail">';
  $EmailHandler .= '<p>'.$name_error.'<b>Your Name:</b><br><input type="text" name="name" class="text" value="'.$name.'">';
  $EmailHandler .= '<p>'.$email_error.'<b>Your e-mail address:</b><br><input type="text" name="email" class="text" value="'.$email.'">';
  $EmailHandler .= '<p>'.$subject_error.'<b>Subject:</b><br><input type="text" name="subject" class="text" value="'.$subject.'">';
  $EmailHandler .= '<p>'.$message_error.'<b>Message:</b><br><textarea name="message" rows=5 cols=50 wrap="off">'.$message.'</textarea>';
  $EmailHandler .= '<p><input type="submit" name="sub" class="button" value="Submit">';
  $EmailHandler .= '</form>';
}

return $EmailHandler;


to

$sendemail =
isset($_POST['sendemail']) && $_POST['sendemail']!= "{{" && $_POST['sendemail']!= "[[" &&
$_POST['sendemail']!= "[(" && $_POST['sendemail']!= "[~" && $_POST['sendemail']!= "[*" ?
$_POST['sendemail'] : "";

$name =
isset($_POST['name']) && $_POST['name']!= "{{" && $_POST['name']!= "[[" &&
$_POST['name']!= "[(" && $_POST['name']!= "[~" && $_POST['name']!= "[*" ?
$_POST['name'] : "";

$email =
isset($_POST['email']) && $_POST['email']!= "{{" && $_POST['email']!= "[[" &&
$_POST['email']!= "[(" && $_POST['email']!= "[~" && $_POST['email']!= "[*" ?
$_POST['email'] : "";

$subject =
isset($_POST['subject']) && $_POST['subject']!= "{{" && $_POST['subject']!= "[[" &&
$_POST['subject']!= "[(" && $_POST['subject']!= "[~" && $_POST['subject']!= "[*" ?
$_POST['subject'] : "";

$telno =
isset($_POST['telno']) && $_POST['telno']!= "{{" && $_POST['telno']!= "[[" &&
$_POST['telno']!= "[(" && $_POST['telno']!= "[~" && $_POST['telno']!= "[*" ?
$_POST['telno'] : "";


$date =
isset($_POST['date']) && $_POST['date']!= "{{" && $_POST['date']!= "[[" &&
$_POST['date']!= "[(" && $_POST['date']!= "[~" && $_POST['date']!= "[*" ?
$_POST['date'] : "";

$time =
isset($_POST['time']) && $_POST['time']!= "{{" && $_POST['time']!= "[[" &&
$_POST['time']!= "[(" && $_POST['time']!= "[~" && $_POST['time']!= "[*" ?
$_POST['time'] : "";

$attend =
isset($_POST['attend']) && $_POST['attend']!= "{{" && $_POST['attend']!= "[[" &&
$_POST['attend']!= "[(" && $_POST['attend']!= "[~" && $_POST['attend']!= "[*" ?
$_POST['attend'] : "";









$message =
isset($_POST['message']) && $_POST['message']!= "{{" && $_POST['message']!= "[[" &&
$_POST['message']!= "[(" && $_POST['message']!= "[~" && $_POST['message']!= "[*" ?
$_POST['message'] : "";

// check errors
$pattern = ".+@.+..+";
$error = '<font color="#FF0000">Required</font><br>';
$bad_email = '<font color="#FF0000">Make sure that you have entered a valid e-mail address.</font><br>';
if($sendemail=="sendemail"){
  if($name==""){$name_error = $error;}
  if($email==""){$email_error = $error;}
  elseif(!eregi($pattern,$email)){$email_error = $bad_email;}
 // if($subject==""){$subject_error = $error;}
  // if($message==""){$message_error = $error;}
}

if(($name!="")&&(eregi($pattern,$email))&&($subject!="")&&($message!="")){
  if($_SERVER["HTTP_HOST"] != "benreeves"){mail($to, $subject, $message, "From: $email");}
  $EmailHandler = "<b>Your message has been sent.</b><p>You will recieve feedback as soon as possible.";
} else {
  $EmailHandler .= "<b>Use the form below to make a free booking request via the site.</b>\n";
  $EmailHandler .= '<form name="EmailHandler" action="" method="post">';
  $EmailHandler .= '<input name="sendemail" type="hidden" value="sendemail">';
  $EmailHandler .= '<p>'.$name_error.'<b>Your Name:</b><br><input type="text" name="name" class="text" value="'.$name.'">';
  $EmailHandler .= '<p>'.$email_error.'<b>Your e-mail address:</b><br><input type="text" name="email" class="text" value="'.$email.'">';
  $EmailHandler .= '<p>'.$subjet_error.'<b>Your contact telephone number</b><br><input type="text" name="telno" class="text" value="'.$telno.'">';
 $EmailHandler .= '<p>'.$subjet_error.'<b>Booking date requested</b><br><input type="text" name="date" class="text" value="'.$date.'">';
 $EmailHandler .= '<p>'.$subjet_error.'<b>Booking time requested</b><br><input type="text" name="time" class="text" value="'.$time.'">';
 $EmailHandler .= '<p>'.$subjet_error.'<b>Number of attendees</b><br><input type="text" name="attend" class="text" value="'.$attend.'">';


$subject = 'FF1600 Fee event Web Booking';
$message = $telno.$date.$time.$attend;




  $EmailHandler .= '<p><input type="submit" name="sub" class="button" value="Submit">';
  $EmailHandler .= '</form>';
}

return $EmailHandler;


I can see why this has gone wrong.

1.) by changing the value of the message box, that code that checks if the message box is populated is broke
2.) I've tried to append together the output of the firleds telno date time attend ( I forgot to say I also wanted to add a telephone number field) Not only do I believe I have done this wrong, I'd also like to do this a little better so the email is formated with
Hi $name, would like to book $attend at $time on $date for the FF1600 free event. You can contact him on $telno
so that its easier for the person reading to make sense of.


But first things first I need to fix what I've broke. Before I actually try to start fixing I'd like to understand am I correct in why/how I've broke this form ?

How can I maintain the error checking on name, and the valid email checker on the email, plus add the same "must be populated" error checking to the other fields ?


Thanks for your time

Edited by ikonia, 29 October 2007 - 11:43 AM.


#2 ikonia

    Etomite Forum Fan

  • Member
  • Pip
  • 117 posts

Posted 29 October 2007 - 12:55 PM

A bit more focus on my part, and a bit more effort has got me to a better spot

$sendemail =
isset($_POST['sendemail']) && $_POST['sendemail']!= "{{" && $_POST['sendemail']!= "[[" &&
$_POST['sendemail']!= "[(" && $_POST['sendemail']!= "[~" && $_POST['sendemail']!= "[*" ?
$_POST['sendemail'] : "";

$name =
isset($_POST['name']) && $_POST['name']!= "{{" && $_POST['name']!= "[[" &&
$_POST['name']!= "[(" && $_POST['name']!= "[~" && $_POST['name']!= "[*" ?
$_POST['name'] : "";

$email =
isset($_POST['email']) && $_POST['email']!= "{{" && $_POST['email']!= "[[" &&
$_POST['email']!= "[(" && $_POST['email']!= "[~" && $_POST['email']!= "[*" ?
$_POST['email'] : "";

$subject =
isset($_POST['subject']) && $_POST['subject']!= "{{" && $_POST['subject']!= "[[" &&
$_POST['subject']!= "[(" && $_POST['subject']!= "[~" && $_POST['subject']!= "[*" ?
$_POST['subject'] : "";

$telno =
isset($_POST['telno']) && $_POST['telno']!= "{{" && $_POST['telno']!= "[[" &&
$_POST['telno']!= "[(" && $_POST['telno']!= "[~" && $_POST['telno']!= "[*" ?
$_POST['telno'] : "";

$reqdate =
isset($_POST['reqdate']) && $_POST['reqdate']!= "{{" && $_POST['reqdate']!= "[[" &&
$_POST['reqdate']!= "[(" && $_POST['reqdate']!= "[~" && $_POST['reqdate']!= "[*" ?
$_POST['reqdate'] : "";

$reqtime =
isset($_POST['reqtime']) && $_POST['reqtime']!= "{{" && $_POST['reqtime']!= "[[" &&
$_POST['reqtime']!= "[(" && $_POST['reqtime']!= "[~" && $_POST['reqtime']!= "[*" ?
$_POST['reqtime'] : "";

$attend =
isset($_POST['attend']) && $_POST['attend']!= "{{" && $_POST['attend']!= "[[" &&
$_POST['attend']!= "[(" && $_POST['attend']!= "[~" && $_POST['attend']!= "[*" ?
$_POST['attend'] : "";



// check errors
$pattern = ".+@.+..+";
$error = '<font color="#FF0000">Required</font><br>';
$bad_email = '<font color="#FF0000">Make sure that you have entered a valid e-mail address.</font><br>';
if($sendemail=="sendemail"){
  if($name==""){$name_error = $error;}
  if($email==""){$email_error = $error;}
  elseif(!eregi($pattern,$email)){$email_error = $bad_email;}
  if($subject==""){$subject_error = $error;}
  if($telno==""){$telno_error = $error;}
  if($reqdate==""){$reqdate_error = $error;}
  if($reqtime==""){$reqtime_error = $error;}
  if($attend==""){$attend_error = $error;}


}

if(($name!="")&&(eregi($pattern,$email))&&($subject!="")&&($telno!="")){
  if($_SERVER["HTTP_HOST"] != "benreeves"){mail($to, $subject, $telno, "From: $email");}
  $EmailHandler = "<b>Your message has been sent.</b><p>You will recieve feedback as soon as possible.";
} else {
  $EmailHandler .= "<b>Use the form below to contact via e-mail.</b>rn";
  $EmailHandler .= '<form name="EmailHandler" action="" method="post">';
  $EmailHandler .= '<input name="sendemail" type="hidden" value="sendemail">';
  $EmailHandler .= '<p>'.$name_error.'<b>Your Name:</b><br><input type="text" name="name" class="text" value="'.$name.'">';
  $EmailHandler .= '<p>'.$email_error.'<b>Your e-mail address:</b><br><input type="text" name="email" class="text" value="'.$email.'">';
  $EmailHandler .= '<p>'.$subject_error.'<b>Subject:</b><br><input type="text" name="subject" class="text" value="'.$subject.'">';
  $EmailHandler .= '<p>'.$telno_error.'<b>Message:</b><br><input type="text" name="telno" >'.$telno.'</input>';
  $EmailHandler .= '<p>'.$reqdate_error.'<b>Required Date:</b><br><input type="text" name="reqdate">'.$reqdate.'</input>';
$EmailHandler .= '<p>'.$reqtime_error.'<b>Required Time:</b><br><input type="text" name="reqtime" >'.$reqtime.'</input>';
$EmailHandler .= '<p>'.$attend_error.'<b>Number Of Attendies:</b><br><input type="text" name="attend">'.$reqtime.'</input>';

  $EmailHandler .= '<p><input type="submit" name="sub" class="button" value="Submit">';
  $EmailHandler .= '</form>';
}

return $EmailHandler;


This currently has all the fields with error checking working, howver I've not set it to send all the fields and I need to remove the "subject" line to I can hard code it.

#3 Cris D.

    Loves Etomite Forums!

  • Developers
  • PipPipPipPip
  • 1,104 posts
  • Gender:Male

Posted 29 October 2007 - 08:22 PM

Quote

{mail($to, $subject, $telno, "From: $email");}

You have not added all the fields to the mail function.

In the first code sample, the fields were concatenated
$message = $telno.$date.$time.$attend;

However, the line where the mail() function was supposed to be processing the $message variable, $message was defined as:
$message =
isset($_POST['message']) && $_POST['message']!= "{{" && $_POST['message']!= "[[" &&
$_POST['message']!= "[(" && $_POST['message']!= "[~" && $_POST['message']!= "[*" ?
$_POST['message'] : "";

The $message with the concatendated variables appeared AFTER is was required for the mail() function.

Also, there was no form field < ...name='message'...> to send the $_POST['message'] field being collected to mail()...

In the second example,
you have fixed the incorrect naming of the form fields and collected them well, however, the fields being passed to the mail function do not comply with the required parameters to pass to that function. You can either concatenate all the variables and send them as a single value like the $message variable in the first example, or read below how to add additional fields to the mail function.

See php.net/function.mail.

Quote

bool mail ( string $to, string $subject, string $message [, string $additional_headers [, string $additional_parameters]] )

See also php.net/mail for more indepth assistance on the surrounding parameters that make the mail function work..

Edited by Cris D., 30 October 2007 - 02:47 AM.


#4 ikonia

    Etomite Forum Fan

  • Member
  • Pip
  • 117 posts

Posted 30 October 2007 - 05:23 PM

fantastic help, thank you.

I knew I had not added the new fields to the mail function yet, as I was just figuring out the best way to do this.

I'll see what I can put together from the info you've suggested.

thank you .





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users