[PHP] Simple, neat PHP contact form

Results 1 to 6 of 6
  1. #1
    Web Developer Markshall is offline
    MemberRank
    Oct 2009 Join Date
    EnglandLocation
    628Posts

    [PHP] Simple, neat PHP contact form

    I've just finished coding this after about 30-40 minutes of coding and testing it.

    Features:
    - checks if all fields are filled in
    - doesn't require the website field, but if it is entered it will put 'http://www.', before it
    - sends IP Address, IP Host and browser details
    - checks for valid email address
    - disables the 'Send Message' button if the form sending was successful so they can not spam you
    - sends email in HTML format

    Screenshot of how the email looks:


    Please note:
    There are personal things on there of mine as I made this for my website in the first place, so remove the junk that doesn't need to be there such as the 'Why does this site suck so damn bad?' link at the bottom

    Please keep my copyright in there or at least a link back to my website (Mark-Eriksson.com - Contact Me). You don't have to, but if you want to show appreciation towards me if you use this, that will do.

    Codes:

    index.php
    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
        <title>Mark-Eriksson.com - Contact Me</title>
        <link type="text/css" rel="stylesheet" href="global.css">
        <script language="javascript">
            function sf(){
                document.contactfrm.name.focus();
            }
            function why()
            {
                var whyisthissitesodamnbadlolquestionmark = 'Because I am making a new portfolio so I have took the main site down for the time being.';
                alert( whyisthissitesodamnbadlolquestionmark );
            }
        </script>
    </head>

    <body onload="sf()">
        <center>
            <h1>Contact Me</h1>
            <h4>i'm working on a new portfolio.. again.</h4>
        </center>
        <div id="contact_container">
            <?php
            
    function sendMail$to$subject$message$name$email )
            {
                
    $headers 'From: ' $name ' <' $email '>\r\n';
                
    $headers .= 'MIME-Version: 1.0' "\r\n";
                
    $headers .= 'Content-type: text/html; charset=iso-8859-1' "\r\n";
                
    mail$to$subject$message$headers );
            }
            function 
    validEmail$email )
            {
                return ( ( 
    preg_match"/^[^@]*@[^@]*\.[^@]*$/"$email ) ) ? true false );
            }
            if( 
    $_POST[send] )
            {
                
    $name strip_tagsstripslashes$_POST['name'] ) );
                
    $email strip_tagsstripslashes$_POST['email'] ) );
                
    $subject strip_tagsstripslashes$_POST['subject'] ) );
                
    $website = ( ( $_POST['website'] ) ? 'http://www.' str_replace'http://'''str_replace'www.'''strip_tagsstripslashes$_POST['website'] ) ) ) ) : "" );
                
    $comments1 $_POST['comments'];
                
    $comments2 nl2brstrip_tagsstripslashes$comments1 ) ) );
                
    $ip $_SERVER['REMOTE_ADDR'];
                
    $host gethostbyaddr$ip );
                
    $error '';
                if( empty( 
    $name ) )
                {
                    
    $error .= 'Your must enter your name.<br>';
                }
                if( empty( 
    $email ) )
                {
                    
    $error .= 'Your must enter your email address.<br>';
                }
                if( !
    validEmail$email ) )
                {
                    
    $error .= 'You must enter a valid email address.<br>';
                }
                if( empty( 
    $comments1 ) )
                {
                    
    $error .= 'Your must enter a message.<br>';
                }
                if( 
    $error )
                {
                    echo 
    "<div id=\"error\"><strong>Error(s) occurred</strong><br>" $error "</div>";
                    
    $disablebutton false;
                }
                else
                {
                    
    $comments3 '<strong>From:</strong> ' $name '<br>
    <strong>Email Address:</strong> ' 
    $email '<br>';
    if( 
    $website ) { $comments3 .= '<strong>Website:</strong> <a href="' $website '" target="_blank">' $website '</a><br>
    '
    ; }
    $comments3 .= '
    <strong>IP Address:</strong> ' 
    $ip '<br>
    <strong>ISP:</strong> ' 
    $host '<br>
    <strong>Browser Details:</strong> ' 
    $_SERVER['HTTP_USER_AGENT'] . '<br>
    <strong>Date & Time:</strong> ' 
    date"F jS Y - g:i a" ) . '<br><br>

    <strong>Comments:</strong><br>
    $comments2;
                    
    sendMail"me@mark-eriksson.com"$subject$comments3$name$email );
                    echo 
    "<div id=\"error\"><strong>Success!</strong><br>I have just recieved your email! I will try my best to respond within 72 hours (3 days).</div>";
                    
    $disablebutton true;
                }
            }
            
    ?>
            <div id="contact">
                <form method="post" name="contactfrm" id="contactfrm">
                    <table width="100%" border="0" cellspacing="0" cellpadding="5">
                        <tr>
                            <td width="30%" align="right" valign="middle"><strong>Your Name *</strong></td>
                            <td align="left" valign="middle"><input type="text" name="name" size="30" value="<?=$name;?>" maxlength="50" /></td>
                        </tr>
                        <tr>
                            <td width="30%" align="right" valign="middle"><strong>Your Email *</strong></td>
                            <td align="left" valign="middle"><input type="text" name="email" size="30" value="<?=$email;?>" maxlength="150" /></td>
                        </tr>
                        <tr>
                            <td width="30%" align="right" valign="middle"><strong>Subject *</strong></td>
                            <td align="left" valign="middle"><input type="text" name="subject" size="30" value="<?=$subject;?>" maxlength="75" /></td>
                        </tr>
                        <tr>
                            <td width="30%" align="right" valign="middle"><strong>Your Website</strong></td>
                            <td align="left" valign="middle"><input type="text" name="website" size="30" value="<?=$website;?>" maxlength="300" /></td>
                        </tr>
                        <tr>
                            <td width="30%" align="right" valign="middle"><strong>Comments *</strong></td>
                            <td align="left" valign="middle"><textarea name="comments" id="comments" style="width: 173px; height: 109px;"><?=$comments1;?></textarea></td>
                        </tr>
                        <tr>
                            <td width="30%" align="right" valign="middle">&nbsp;</td>
                            <td align="left" valign="middle"><input type="submit" value="Send Message" name="send" class="button"<?php if( $disablebutton ) { ?> disabled="yes"<?php ?> /></td>
                        </tr>
                    </table>
                </form>
            </div>
            <div id="extra_info">
                <table width="100%" border="0" cellspacing="0" cellpadding="0">
                    <tr>
                        <td align="left" valign="middle">IP Logged: <strong><?=$_SERVER['REMOTE_ADDR'];?></strong></td>
                        <td align="right" valign="middle">Created by Mark Eriksson</td>
                    </tr>
                </table>
            </div>
        </div>
        <div align="center">Mark Eriksson &copy; 2011.<br>Fields marked with a * are required.<br><a href="javascript:why();">Why does this site suck so damn bad?</a></div>
    </body>
    </html>
    global.css
    Code:
    body, td, th {
    	font-family: Tahoma, Arial, Helvetica, sans-serif;
    	font-size: 11px;
    	color: #333;
    }
    body {
    	background-color: #FFF;
    	margin: 0;
    }
    a, a:visited {
    	color: #000066;
    	text-decoration: underline;
    }
    a:hover {
    	text-decoration: none;
    }
    form {
    	margin: 0;
    	padding: 0;
    }
    input, select, textarea {
    	font-family: Tahoma, Arial, Helvetica, sans-serif;
    	font-size: 11px;
    	padding: 3px;
    }
    #contact_container {
    	color: #333;
    	background-color: #FFF;
    	text-align: left;
    	width: 330px;
    	padding: 1px;
    	margin: 20px auto 10px auto;
    	border: 1px solid #CCCCCC;
    }
    #contact_container #contact {
    	background-color: #EFEFEF;
    	text-align: left;
    	margin: 0;
    	padding: 10px;
    }
    #contact_container #error {
    	background-color: #FCF9D2;
    	text-align: center;
    	padding: 10px;
    	margin: 0 0 1px 0;
    }
    #contact_container #extra_info {
    	background-color: #CCC;
    	text-align: left;
    	padding: 10px;
    	margin: 1px 0 0 0;
    }

    Live Demo?
    Sure, but spam it and I will ban your fucking IP Address from the rest of the site.
    Code:
    http://www.mark-eriksson.com/contact/index.php

    Enjoy.
    - m0nsta.


  2. #2
    Account Upgraded | Title Enabled! Putako is offline
    MemberRank
    Mar 2011 Join Date
    CanadaLocation
    542Posts

    Re: [PHP] Simple, neat PHP contact form

    I really enjoy this. Thanks.

  3. #3
    Developer Macbeth is offline
    MemberRank
    Dec 2007 Join Date
    SwedenLocation
    392Posts

    Re: [PHP] Simple, neat PHP contact form

    Looking good! You should use css instead of tables though.

  4. #4
    Web Developer Markshall is offline
    MemberRank
    Oct 2009 Join Date
    EnglandLocation
    628Posts

    Re: [PHP] Simple, neat PHP contact form

    Thanks, and how do you mean? :P

  5. #5
    Alpha Member Justei is offline
    MemberRank
    Oct 2007 Join Date
    /f241Location
    1,904Posts

    Re: [PHP] Simple, neat PHP contact form

    Quote Originally Posted by m0nsta. View Post
    Thanks, and how do you mean? :P
    Pretty sure he means the form itself using tables, however I don't see anything wrong with that. Works fine if u ask me, usage of tables can be justified sometimes if u ask me :P.

  6. #6
    Developer Macbeth is offline
    MemberRank
    Dec 2007 Join Date
    SwedenLocation
    392Posts

    Re: [PHP] Simple, neat PHP contact form

    Yeah I ment inside the form :P Well yeah it works good, but they should only be used for tabular data, that'll say if you're really picky.



Advertisement