adding additional fields to my mailform

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lindatash
    New Member
    • Apr 2004
    • 1

    adding additional fields to my mailform

    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 <[email protected]>");
    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
  • sirspot
    New Member
    • Apr 2004
    • 22

    #2
    Adding a few more fields

    at the line:

    $mail_body = $HTTP_POST_VARS['mail_body'];

    you may change this to:

    $mail_body = 'Message: ' . $HTTP_POST_VARS['mail_body'] . "\n\n";
    $mail_body .= 'New Field: ' . $HTTP_POST_VARS['new_field'] . "\n\n";
    $mail_body .= 'New Field 2: ' . $HTTP_POST_VARS['new_field2'] . "\n\n";
    ...

    which basically appends the new fields to the mail message body along with field names and line breaks between the fields


    at the line:

    <tr><td align=right><input type="submit" value="Send"></td></tr>

    you may change this to:

    <tr><td><input type="text" name="new_field"></td></tr>
    <tr><td><input type="text" name="new_field2"></td></tr>
    <tr><td align=right><input type="submit" value="Send"></td></tr>
    ..

    which adds two fields to the form (you can change this to make the form look however you want)

    That should be all it takes.

    -Adam

    Comment

    widgetinstance 262 (Related Topics) skipped due to lack of content & hide_module_if_empty option.
    Working...