Making a "Contact Us" Form

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • popnbrown
    New Member
    • Oct 2006
    • 24

    Making a "Contact Us" Form

    Hey umm how do you write a form that e-mails to you?
    I so far have created a form and some labels and textboxes

    Here is the code:
    [HTML]<form name="contact" id="contact" method="put" action"#">
    <label>Name: </label>
    <p>
    <input type="text" name="name" tabindex="1" class="ina"/>
    </p>
    <label>E-mail: </label>
    <p>
    <input type="text" name="email" tabindex="2" />
    </p>
    <label>Subject: </label>
    <p>
    <input type="text" name="subject" tabindex="3" />
    </p>
    <label>Messag e:
    <p>
    <textarea name="message"> </textarea>
    </p>
    </label>
    <p>
    <input type="submit" name="Submit" value="Submit" tabindex="5" />
    </p>
    </form>[/HTML]

    I would like to do a couple of things, first off make E-mail and Subject required, so that when a user presses Submit and if E-mail or Subject is not filled then a window pops up asking to fill in the all required fields.

    Secondly, when the user presses submit I would like a message to be sent to an e-mail address(Example : support@coolkki ds.com). The e-mail should be sent with all of the input the user provides.
    Thank you any1 for their help.
  • AricC
    Recognized Expert Top Contributor
    • Oct 2006
    • 1885

    #2
    To send an email you are going to need a server side scripting language like PHP or ASP. Are you able to run either? You can google for a script or we can post an example.

    HTH,
    Aric

    Comment

    • popnbrown
      New Member
      • Oct 2006
      • 24

      #3
      OK, umm i tried googling it didnt work, or im just really sucky at it.
      So if u could put up an exapmle tat would be great

      thank you

      Comment

      • steven
        New Member
        • Sep 2006
        • 143

        #4
        Does your web server support server side scripting languages such as PHP, PERL, JSP or ASP? If not, then an email form is not possible.

        Comment

        • popnbrown
          New Member
          • Oct 2006
          • 24

          #5
          Originally posted by steven
          Does your web server support server side scripting languages such as PHP, PERL, JSP or ASP? If not, then an email form is not possible.
          Luckily my web hosting site hostmonster.com does support CGI, Ruby (RoR), Perl, PHP, MYSQL

          Comment

          • ronverdonk
            Recognized Expert Specialist
            • Jul 2006
            • 4259

            #6
            Usually it is against my principle to show complete code in the forum, but this time I will make an exception. Especially since this answer really belongs in the PHP forum. The following sample shows a simple contact-me email form by Charles Reace. This code has ample comments, so you can work it out for your own requirements. Good luck![php]<?php
            ############### ############### ############### ############### ############### ######
            #
            # mail.php - general-purpose email form and script
            #
            # copyright June 2005 by Charles Reace
            #
            # This software available as open source software under the Gnu General Public
            # License: http://www.gnu.org/licenses/gpl.html
            #
            ############### ############### ############### ############### ############### #####
            ############### ############### ############### ############### ############### #####
            # user-defined variables - update for your email address(es) ############### ############### ############### ############### ############### #####
            # home page for link at top of page:
            $home = "http://www.yourdomain. com/";
            # Modify the following variables to set up where emails will be sent.
            # $toName will be concatenated with a "@" and then $toDomain to create
            # the complete email address.
            $toName = "johndoe"; # the part that goes before the @
            $toDomain = "yourdomain.com "; # the part that comes after the @
            # uncomment and edit the next two lines if you want to cc someone:
            $ccName = "ccname";
            $ccDomain = "ccdomain.c om";
            # uncomment and edit the next two lines if you want to bcc someone:
            $bccName = "bccname";
            $bccDomain = "bccdomain.com" ;
            ############### ############### ###
            # end of user-defined variables
            ############### ############### ####
            # email address validation function
            # kudos to http://iamcal.com/publish/articles/php/parsing_email/pdf/
            function is_valid_email_ address($email) {
            $qtext = '[^\\x0d\\x22\\x5 c\\x80-\\xff]';
            $dtext = '[^\\x0d\\x5b-\\x5d\\x80-\\xff]';
            $atom = '[^\\x00-\\x20\\x22\\x28 \\x29\\x2c\\x2e \\x3a-\\x3c'.
            '\\x3e\\x40\\x5 b-\\x5d\\x7f-\\xff]+';
            $quoted_pair = '\\x5c\\x00-\\x7f';
            $domain_literal = "\\x5b($dtext|$ quoted_pair)*\\ x5d";
            $quoted_string = "\\x22($qtext|$ quoted_pair)*\\ x22";
            $domain_ref = $atom;
            $sub_domain = "($domain_ref|$ domain_literal) ";
            $word = "($atom|$quoted _string)";
            $domain = "$sub_domain(\\ x2e$sub_domain) *";
            $local_part = "$word(\\x2e$wo rd)*";
            $addr_spec = "$local_part\\x 40$domain";
            return preg_match("!^$ addr_spec$!", $email) ? 1 : 0;
            }
            # Init. some variables:
            $error = "";
            $success = FALSE;

            # process form if submitted:
            if(isset($_POST['submit'])){
            foreach($_POST as $key => $val) {
            if (empty($_POST[$key])) {
            $error .= "You must enter a value for " .
            ucwords(str_rep lace("_"," ",$key)) . ". ";
            }
            else {
            if(get_magic_qu otes_gpc()) {
            $_POST[$key] = stripslashes($v al);
            }
            }
            if($key != 'message') {
            $_POST[$key] = preg_replace('/[\r\n]/', ' ', $val);
            }
            }
            if($_POST['email_address'] != $_POST['repeat_email']) {
            $error .= "Email Address is not the same as Repeat Email. ";
            }
            elseif(is_valid _email_address( $_POST['email_address']) == 0) {
            $error .= "'{$_POST['email_address']}' does not appear to be a valid email address. ";
            }
            if(empty($error )) # no errors in input, so go ahead and email it.
            {
            $to = "$toName@$toDom ain";
            $headers = "From: ".preg_replace( '/[\r\n]+/',' ', $_POST['email_address']);
            if(!empty($ccNa me) and !empty($ccDomai n)) {
            $headers .= "\r\nCc: $ccName@$ccDoma in";
            }
            if(!empty($bccN ame) and !empty($bccDoma in)) {
            $headers .= "\r\nBcc: $bccName@$bccDo main\r\n\r\n";
            }
            $headers .= "\r\nX-Mailer: PHP/" . phpversion();
            $msg = "From {$_POST['name']} ({$_POST['email_address']})";
            $msg .= "\n\n\n{$_P OST['message']}";
            echo "<pre>$headers" ; exit;
            $result = @mail($to,
            stripslashes($_ POST['subject']),
            $msg,
            $headers);
            if(!$result) {
            $error = "There was an unknown error while attempting to send your email.";
            }
            else {
            header("Locatio n: http://www.charles-reace.com/Email_Me/thanks.php");
            }
            }
            }
            ?>
            <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
            <html lang='en'>
            <head>
            <meta http-equiv='Content-Type' content='text/html; charset=ISO-8859-1'>
            <title>Email Me</title>
            <style type="text/css">
            body {background-color:lemonchif fon;color:#0022 00;margin:10px 150px 0 150px; font-family:verdana, arial,sans serif;font-size:12px;}
            .lab {margin-left:3px;displa y: block; float: left; width: 9em;}
            legend {font-size:13px;font-weight:bold;}
            </style>
            </head>
            <body>
            <?php
            if(!empty($erro r)){
            echo "<p style='color: #c03;'>$error</p>\n";
            }
            ?>
            <h2>Email Me</h2>
            <form action='<?php echo $_SERVER['PHP_SELF'] ?>' method=post>
            <fieldset>
            <legend>Your Contact Information</legend>
            <p><label for='name' class='lab'>Nam e:</label>
            <input type='text' name='name' id='name' size='30' maxlength='40'< ?php
            if(!empty($_POS T['name'])){
            echo "value='{$_ POST['name']}'";
            }
            ?>></p>
            <p><label for='email_addr ess' class='lab'>Ema il Address:</label>
            <input type='text' name='email_add ress' id='email_addre ss' size='30' maxlength='40'< ?php
            if(!empty($_POS T['email_address'])){
            echo "value='{$_ POST['email_address']}'";
            }
            ?>></p>
            <p><label for='repeat_ema il' class='lab'>Rep eat Email:</label>
            <input type='text' name='repeat_em ail' id='repeat_emai l' size='30' maxlength='40'< ?php
            if(!empty($_POS T['repeat_email'])) {
            echo "value='{$_ POST['repeat_email']}'";
            }
            ?>></p>
            </fieldset>
            <p> </p>
            <fieldset>
            <legend>Message </legend>
            <p><label for='subject' style="margin-left:3px;displa y: block; float: left; width: 9em;">Subject:</label>
            <input type='text' name='subject' id='subject' size='50' maxlength='60'< ?php
            if(!empty($_POS T['subject'])){
            echo " value='{$_POST['subject']}'";
            }
            ?>></p>
            <p><label for='message' style="margin-left:3px;displa y: block; float: left; width: 9em;">Message:</label>
            <textarea name='message' id='message' cols='50' rows='8'
            style="width: 375px"><?php
            if(!empty($_POS T['message'])){
            echo $_POST['message'];
            }
            ?></textarea></p>
            <p style="text-align: center;"><input type='submit' name='submit' value="Send Email"></p>
            </fieldset>
            </form>
            </body>
            </html>[/php]
            Ronald :cool:

            Comment

            • AricC
              Recognized Expert Top Contributor
              • Oct 2006
              • 1885

              #7
              I was going to post one but that is more complete than the one I use

              Comment

              • popnbrown
                New Member
                • Oct 2006
                • 24

                #8
                Well, I guess that will work (like I know!!!::sarcas m::)

                I guess no harm in trying it out. Will tell you my results on Thursday.

                Comment

                • ronverdonk
                  Recognized Expert Specialist
                  • Jul 2006
                  • 4259

                  #9
                  If you have questions/problems with that posted code sample, it is probably a good idea to post them in the PHP forum.

                  Not that I would want to lure you away from this forum, but it is likely that there are more PHP people in the php forum. So help will be quicker.

                  Ronald :cool:

                  Comment

                  • AricC
                    Recognized Expert Top Contributor
                    • Oct 2006
                    • 1885

                    #10
                    Originally posted by ronverdonk
                    If you have questions/problems with that posted code sample, it is probably a good idea to post them in the PHP forum.

                    Not that I would want to lure you away from this forum, but it is likely that there are more PHP people in the php forum. So help will be quicker.

                    Ronald :cool:
                    Maybe I should start hanging out in there if it's a happening place.

                    Comment

                    • popnbrown
                      New Member
                      • Oct 2006
                      • 24

                      #11
                      Well even though its not Thursday yet.
                      I copied the code and pasted it and I dont think it worked.
                      The link is cppinquiry.code tuts.com/contact.html

                      So wat do i do?

                      Comment

                      • AricC
                        Recognized Expert Top Contributor
                        • Oct 2006
                        • 1885

                        #12
                        You didn't include that on your page correctly. The problem is you have basically two HTML pages in one.

                        Comment

                        • popnbrown
                          New Member
                          • Oct 2006
                          • 24

                          #13
                          UMM so how do i make it one?

                          Comment

                          • AricC
                            Recognized Expert Top Contributor
                            • Oct 2006
                            • 1885

                            #14
                            Originally posted by popnbrown
                            UMM so how do i make it one?
                            Don't include the second
                            Code:
                            <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
                            <html lang='en'>
                            <head>
                            <meta http-equiv='Content-Type' content='text/html; charset=ISO-8859-1'>
                            <title>Email Me</title>
                            <style type="text/css">
                            etc....
                            Only include the form below this and the php above this.

                            Comment

                            • popnbrown
                              New Member
                              • Oct 2006
                              • 24

                              #15
                              i did do that but now its even more messed up

                              Comment

                              Working...