Forum Moderators: coopster

Message Too Old, No Replies

adding additional fields to my mailform

         

lindatash

7:21 am on Apr 13, 2004 (gmt 0)

10+ Year Member



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:

<?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

[edited by: jatar_k at 4:13 pm (utc) on April 13, 2004]
[edit reason] fixed sidescroll [/edit]

mykel79

1:23 pm on Apr 13, 2004 (gmt 0)

10+ Year Member



Try doing it like this to add an occupation field:
Make sure the occupation field is read. Under
$mail_body = $HTTP_POST_VARS['mail_body'];

add
$occupation = $HTTP_POST_VARS['occupation'];

Add the occupation field to the body of the message. Change

if (empty($sender_name) ¦¦ $error_flag) : print_mail_form();  
elseif (mail(TO, $mail_subject, $mail_body, $mail_headers));

to
if (empty($sender_name) ¦¦ $error_flag) : print_mail_form();  
elseif {
$mail_body.="\nOccupation:".$occupation;
(mail(TO, $mail_subject, $mail_body, $mail_headers));
}

And finally add a field in the email form.
Under
<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>
add
<tr><td><b>Occupation:&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</b><input type="text" name="occupation" value="<? echo htmlspecialchars($occupation);?>" size="32" maxlength="60"></td>
</tr>

This will not check if something has been entered in the occupation field, but you probably don't want to force people to give that data.

[edited by: jatar_k at 4:16 pm (utc) on April 13, 2004]
[edit reason] fixed sidescroll [/edit]

lindatash

7:26 am on Apr 14, 2004 (gmt 0)

10+ Year Member



Thank you very much, mykel79!