Simple PHP Contact Us Form

I’ve noticed a simple contact us form is something that a lot of people frequently look for online, however many of the current solutions/examples out there are quite complex, ‘bloated’, and can also be, in some cases, complete overkill.

So here’s a simple PHP-based contact us form which makes use of one single PHP file to handle both the output of the form, the output of the thank you message, and the email processing.

You are more than welcome to copy and use this code both in a commercial and non-commercial sense.

  1. <?php
  2. if($_POST['contactus']) { // if user submitted the form, then
  3. extract($_POST); // extract form contents and assign them to variables
  4. $name = htmlentities($name);
  5. $subject = htmlentities($subject);
  6. $msg = htmlentities($msg);
  7. $msg .= “Name: {$name}”;
  8. mail(‘recipient-email@domain.com’,$subject,$msg);
  9. }
  10. ?>
  11. <html>
  12. <head>
  13. </head>
  14. <body>
  15. <form name=”contactus” action=”<?php $_SERVER['PHP_SELF']; ?>” method=”POST”>
  16. Name: <input type=”text” name=”name” /><br />
  17. Subject: <inpout type=”text” name=”subject” /><br />
  18. Message: <input type=”textarea” name=”msg” /><br />
  19. <input type=”submit” name=”contact” value=”Contact us” /></form>
  20. </body>
  21. </html>

It is crucial to consider that the automatically created variables, which were extracted from the $_POST array, are the same as the name of each input element.
For example:
<input type="text" name="xyz" id="xyz_input" />

Would result in a variable $xyz being created, which would be assigned the value of xyz_input.value

The htmlentities() functions are used to convert any HTML tags within the input boxes to the HTML character code equivalents, to help prevent unsanitized data from being processed. Note: Typically, using htmlentities alone is not enough – it’s strongly suggested that you use Javascript or PHP to validate the input fields prior to processing. In Web Development, always consider that someone out there may enter incorrect and/or potentially malicious data into your forms (google SQL injection, etc and you’ll see what I mean).

Obviously, if your web host doesn’t support the mail() function, you might have to consider sending your mail via an SMTP provider, using an SMTP class. There are plenty of these on the internet, however this SMTP class seems quite promising. (I haven’t used this, however – so any feedback would be greatly appreciated.)

Darkening Page and set focus on popup window – Javascript

Ever wanted to make a page darken once you click on a popup? I always have… Until I found the answer today, with the help from a friend of mine, Kelwynsa8.

Here’s the 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>
    <title>Untitled Page</title>

<script language="javascript" type="text/javascript">
// <!CDATA[

function SignUpForNewsletter()
{
    DarkenPage();
    ShowNewsletterPanel();
}

function ShowNewsletterPanel()
{
    var newsletter_panel = document.getElementById('newsletter_panel');

    // w is a width of the newsletter panel
    w = 300;
    // h is a height of the newsletter panel
    h = 300;

    // get the x and y coordinates to center the newsletter panel
    xc = Math.round((document.body.clientWidth/2)-(w/2))
    yc = Math.round((document.body.clientHeight/2)-(h/2))

    // show the newsletter panel
    newsletter_panel.style.left = xc + "px";
    newsletter_panel.style.top  = yc + "px";
    newsletter_panel.style.display = 'block';
}

function SignUp()
{
    // hide the newsletter panel
    var newsletter_panel = document.getElementById('newsletter_panel');
    newsletter_panel.style.display = 'none';
    // lighten the page again
    LightenPage();
}

// this function puts the dark screen over the entire page
function DarkenPage()
{
    var page_screen = document.getElementById('page_screen');
    page_screen.style.height = document.body.parentNode.scrollHeight + 'px';
    page_screen.style.display = 'block';
}

// this function removes the dark screen and the page is light again
function LightenPage()
{
    var page_screen = document.getElementById('page_screen');
    page_screen.style.display = 'none';
}

// ]]>
</script>

<style type="text/css">

body
{
 padding:0px;
 margin:0px;
}

#page_screen
{
   background-color:#000000;
   filter:alpha(opacity=80);
   opacity: 0.8;
   position:absolute;
   top:0px;
   left:0px;
   width:100%;
   display:none;
}

#newsletter_panel
{
   width:300px;
   height:300px;
   background-color:#FFFFFF;
   border:1px solid #000000;
   position:absolute;
   top:0px;
   left:0px;
}
</style>

</head>
<body>
    <h2>
        <span style="font-family: Trebuchet MS">Blur test&nbsp;
            <input id="Button1" type="button" value="Sign up for newsletter!" onclick="SignUpForNewsletter();" /></span></h2>
    <div id="lipsum">
        <p>
            <span style="font-size: 10pt; font-family: Trebuchet MS"></span>
        </p>
        <p>
            <span style="font-size: 10pt; font-family: Trebuchet MS">
                <img src="image_1.jpg" style="float: left; margin-right: 10px" width="400" />

            </span>
        </p>
        <p>
            <span style="font-size: 10pt; font-family: Trebuchet MS">
                </span>
        </p>
        <p>
            <span style="font-size: 10pt; font-family: Trebuchet MS"> </span>
        </p>
        <p>
            <span style="font-size: 10pt; font-family: Trebuchet MS"></span>
        </p>
    </div>

    <div id="page_screen">
        &nbsp;
    </div>
    <div id="newsletter_panel" style="display:none;">
        Sign up for our
        <h3 style="color:#FF6600;">NEWSLETTER</h3>

        your email address :
        <input type="text" />
        <br />

        <input type="button" value="Sign up!" onclick="SignUp();" />
    </div>

</body>
</html>

You can use the above as a template. :)

Enjoy!

Increase PHP memory limit

This comes in very handy, so i should archive it :)
It is archived from here:

http://www.ducea.com/2008/02/14/increase-php-memory-limit/


If you have seen an error like “Fatal Error: PHP Allowed Memory Size Exhausted” in apache logs or in your browser, this means that PHP has exhausted the maximum memory limit. This post will show 3 different ways on how you can increase the php memory limit and also explain when you should use them.

First, let’s see where is this limit coming from. Normally you will see from the error message what is the actual limit, as this will look like:
PHP Fatal error: Allowed memory size of X bytes exhausted (tried to allocate Y) in whatever.php
The default value might differ depending on what php version and linux distribution you are running, but normally this will be set to either 8M or 16M. For example on my debian etch, running on php 5.2 this is set by default at 16M.

In order to identify the current value on your system, look inside your php.ini and search for memory_limit:
memory_limit = 16M ; Maximum amount of memory a script may consume (16MB)

There are three ways to change this value, the obvious way – changing the global value from php.ini, but also an individual method to change it just for a script, or folder.

1. Changing memory_limit globally from php.ini

This is the simplest and most obvious method. You just edit your php.ini and change the memory_limit to whatever you need. For ex:
memory_limit = 32M

You will require access to make changes to php.ini on the system. This change is global and will be used by all php scripts running on the system. Once you change this value, you will need to restart the web server in order for it to become active.

Keep in mind that this limit has its logic and don’t increase it artificially, as poorly written php scripts might overkill your system without proper limits.
Note: if you know what you are doing and want to remove the memory limit, you would set this value to -1.

2. Changing memory_limit using .htaccess for a single folder/vhost

Changing the global memory_limit might not be a good idea, and you might be better changing this only inside one folder (normally one application or virtual host) that needs this value changed for its functionality. To do this you have to add to the respective location .htaccess something like:
php_value memory_limit 64M

This change will be local only, and can be useful for webmasters that don’t have control on the system php.ini. This change would not require a reload and will become active immediately.

3. Changing memory_limit inside a single php script.

For even more control you can set this directive inside a single php script. To do so you would use in your code:
ini_set('memory_limit', '64M');

The advantage of this method is that you have more control and set this value just where you know it is really needed. Also it can be done without having access to the system php.ini, and will become active immediately.

Note: in order to be able to use these PHP resource limits, your PHP version must have been compiled with the –enable-memory-limit configure option. Normally most packed versions will have this, but just in case if this doesn’t work for you as expected, check on how php was compiled first.