Page 1 of 1

adding additional fields to my mailform

Posted: Tue Apr 13, 2004 2:24 am
by lindatash
Hi!

I need your help, guys!

I have this PHP mail form that I want to use on my website, but I want to add additional fields to it.
How can I do this? I need to add fields like: your occupation? your profession? your education? your interests? etc...

PLEASE, HELP!

Here is a code:

Code: Select all

<?php
error_reporting(0);
define("TO", "Linda <myemail@mywebsite.com>");
include("design.php");
$error_flag = false;
if (sizeof($HTTP_POST_VARS) >= 4) {
$sender_name = $HTTP_POST_VARS['sender_name'];
$sender_mail = $HTTP_POST_VARS['sender_mail'];
$mail_subject = $HTTP_POST_VARS['mail_subject'];
$mail_body = $HTTP_POST_VARS['mail_body'];
if (strlen($sender_name) < 3 || strlen($sender_name) > 40) {
$error_flag = true;
$error_message = "<p class="error"><b><font color=#FC0433>Please, type your name correctly!</font></b></p>";
}
elseif (!eregi("^.+@(.+\.)+.+$", $sender_mail) || strlen($sender_mail) < 8 || strlen($sender_mail) > 40) {
$error_flag = true;
$error_message = "<p class="error"><b><font color=#FC0433>Please, type your e-mail correctly!</font></b></p>";
}
elseif (strlen($mail_subject) < 4) {
$error_flag = true;
$error_message = "<p class="error"><b><font color=#FC0433>What's your subject?</font></b></p>";
}
elseif (strlen($mail_body) < 4) {
$error_flag = true;
$error_message = "<p class="error"><b><font color=#FC0433>What's your message?</font><b></p>";
}
$mail_headers = "Return-Path: ".reserve."\n".
"From: ".$sender_name."<".$sender_mail.">\n".
"Reply-To: ".$sender_name."<".$sender_mail.">\n".
"Content-Type: text/plain; charset=windows-1251\n".
"Content-Transfer-Encoding: 8bit\n".
"Date: " . date("r")."\n".
"X-Mailer: mailer_".$SERVER_NAME;
}
head();

if (empty($sender_name) || $error_flag) : print_mail_form();
elseif (mail(TO, $mail_subject, $mail_body, $mail_headers)) :
?>
<table width="70%" border="0" cellspacing="0" cellpadding="8" class="mail_sended">
<td align=center><b>Your information has been received.<br>Thanks for your interest.</b></td>
</table>
<?php else : 
echo ("<p><b>Your message has not been sent.<br>Please, try again or try next time.</b></p>");
$error_message = "";
$error_flag = true;
print_mail_form();
endif;
foot();
function print_mail_form() {
global $error_flag, $error_message, $sender_name, $sender_mail, $mail_subject, $mail_body;
if (empty($sender_name)) $sender_name = "";
if (empty($sender_mail)) $sender_mail = "";
if (empty($mail_subject)) $mail_subject = "";
if (empty($mail_body)) $mail_body = "";
?>
<?php if ($error_flag) echo $error_message;?>
<form method="post" name="mail" id="mail">

<p align=center><b>Please, fill in this form:</p>
<br>
<table border="0" cellspacing="0" cellpadding="1">
<tbody>
<tr><td><b>Full name:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</b><input type="text" name="sender_name" id="sender_name" value="<? echo htmlspecialchars($sender_name);?>" size="32" maxlength="60"></td></tr>
<tr><td><b>E-mail address:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</b><input type="text" name="sender_mail" id="sender_mail" value="<? echo htmlspecialchars($sender_mail);?>" size="32" maxlength="60"></td>
</tr>
<tr><td><b>Subject:&nbsp;&nbsp; <b><input type="text" name="mail_subject" id="mail_subject" value="<? echo htmlspecialchars($mail_subject);?>" size="12"></td></tr>
<tr><td><b>Message:&nbsp;&nbsp;&nbsp;&nbsp;</b><textarea cols="27" rows="2" name="mail_body" id="mail_body" id="mail_body"><? echo htmlspecialchars($mail_body);?>
</textarea></td></tr>
<tr><td align=right><input type="submit" value="Send"></td></tr>
</tbody>
</table>
</form>
<?php }?>

</p>
</td></tr></table>

This form works perfectly! I just need to add more questions (fields) to it and I want them to come in the body of the e-mail with the name of that field and the answer of the user - so it could be convenient to read.

I would also need to add uploading a photo function in this form, but I guess that would be toooooo complicated.

Thanks in advance for your help!

Sincerely,
Linda

[Edit: Added PHP tas for eyecandy... --JAM]

Posted: Tue Apr 13, 2004 6:58 am
by JAM
There is a fileupload faq comming soon, so i wont mention that. It has however been discussed quite some times in the forum, so I'm sure you'll find information about it after a short period of searching.

Adding fields...
I wont do it for you, but give you the general idea on how you can do this:

Find

Code: Select all

if (empty($mail_subject)) $mail_subject = "";
This part is used to reuse the information posted in the form. If the field was blank, the variable is set to "" (or Null). Add your line:

Code: Select all

if (empty($mail_NEW)) $mail_NEW = "";

Further down you find:

Code: Select all

<input type="text" name="mail_subject" id="mail_subject" value="<? echo htmlspecialchars($mail_subject);?>" size="12">
Add a similiar line with:

Code: Select all

<input type="text" name="mail_NEW" id="mail_NEW" value="<? echo htmlspecialchars($mail_NEW);?>" size="12">
Do you sence the direction I'm heading at? Meaning; you should be able to copy and paste code, tweaking it around abit to fit your needs, just by looking at the various examples allready there in the script.

Hope that helped some.

Posted: Wed Apr 14, 2004 2:28 am
by lindatash
Thanks, JAM!!!