Jump to content

contact form with logo tutorial

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
35 replies to this topic

#1
midnightrose2008

midnightrose2008

    Newbie

  • Members
  • PipPip
  • 16 posts
I am new at php I am creating my own contact form for my site. But don't know how to get started are there any tutorials for this. Thanks in advance.

#2
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
It's really simple. Just create a <form> object, and point it to the PHP script using the "action" attribute. Then, create the input controls such as text boxes within the form, assigning them unique names. Add a submit button, then the browser will automatically send the data to the PHP file, putting the data in either the $_GET[] or $POST[] array variable. Just grab the data, pop it in a mail() function (perhaps with some validation first) and you're done.

This will get you started: PHP Mail.
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums

#3
chili5

chili5

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 7,247 posts
Xav explained it really well but your gonna need a SMTP server to send the mail and validating the input would be good to learn how to do when your learning. :D learn good habits from the beginning :)

good luck :D

#4
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
Yes, I did explain it really well... ;)

You're absolutely right. You also need an email address to receive the messages... :)
And as for validating input, John did an excellent validation class at http://forum.codecal...tion-class.html.

Here's the code to save you going there:
<?php

/**
 * @author John Ciacia
 * @copyright 2008
 */

class Validator
{
    const ALPHA = 1;
    const NUMBER = 2;
    const ALPHNUM = 3;
    const UPPER = 4;
    const LOWER = 5;

    function __construct()
    {

    }

    //This function validates a string of a particular type.
    //You can either pass in a number (1-5) or its defininion
    //as defined above. If the string is of the valid type
    //this function will return true. If the string is not
    //of the valid type, it will return false.
    static public function valid_string($input, $type)
    {
        switch ($type) {
                //such a string can only contain alphabetical characters
            case ALPHA:
                if (preg_match('/^[a-z]+$/i', $input))
                    return true;
                return false;
                break;

                //such a string can only contain numbers
            case NUMBER:
                if (preg_match('/^\d+$/', $input))
                    return true;
                return false;
                break;

                //such a string can only contain numbers and alphabetical characters
            case ALPHNUM:
                if (preg_match('/^[a-zA-Z0-9]+$/i', $input))
                    return true;
                return false;
                break;

                //such a string can only contain uppercase characters
            case UPPER:
                if (preg_match('/[A-Z]/', $input) && !preg_match('/[a-z]/', $input))
                    return true;
                return false;
                break;

                //such a string can only contain lowercase characters
            case LOWER:
                if (!preg_match('/[A-Z]/', $input) && preg_match('/[a-z]/', $input))
                    return true;
                return false;
                break;

        }
    }

    //This function checks whether an email address is valid. It is
    //not perfect as it does not account for .museum email addresses
    //however this tradeoff is preferable to the other downsides
    //of allowing it.
    static public function valid_email($input)
    {
        if (!eregi("^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$", $input)) {
            return false;
        }
        return true;
    }

    //This function checks whether a string is within certain size constraints.
    //For example, the required length of a password might be 5 characters. Setting
    //$min to 5 will require the password to be larger than or equal to five
    //characters. If not, it will display an error message. If either $max
    //or $min is set to 0, length checking is ignored on that constraing. The max
    //constraint might be set on a contact form, where the text entered should
    //not be any longer than 250 characters.
    static public function valid_size($input, $min = 0, $max = 0)
    {
        //check the minimum size constraints
        //in this case: the string needs to be longer
        if (strlen($input) < $min) {
            return false;
        }

        //check the maximum size constraints
        //in this case: the string is too long
        if (($max != 0) && (strlen($input) > $max)) {
            return false;
        }

        return true;
    }

}

?> 

Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums

#5
chili5

chili5

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 7,247 posts
Thanks, that will be really helpful *goes and does some random validating* :)

I wish i saw that class a long time ago :)

I use mercury mail to send and receive my email. Kinda cool that i setup my own email address :)

#6
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
Is Mercury Mail an email client, like Gmail or Outlook?
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums

#7
chili5

chili5

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 7,247 posts
No it's a web server that sends emails to an email client like outlook. It doesn't send to things like gmail when you run it locally. Great for PHP :D

#8
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
Oh, I see. I use the SMTP server set up on my hosting package - I've never actually run a script locally, due to the amazing ability to open web sites in... yes, VWD. :)
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums

#9
chili5

chili5

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 7,247 posts
I would do that too but I don't have a place online to put a web site or a web site. :P

#10
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts

chili5 said:

Thanks, that will be really helpful *goes and does some random validating* :)

I wish i saw that class a long time ago :)

I use mercury mail to send and receive my email. Kinda cool that i setup my own email address :)

If you need any other validations (or have any I've missed) let me know. I'd like to expand this class. I think it can be really helpful.

#11
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
You mean like other regular expressions if we need to validate anything? I've never taken the time to learn all the characters.
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums

#12
chili5

chili5

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 7,247 posts
I don't know if this was in your class or not but postal codes and zip codes?

Canada format: N2J 34F
UK format: A99 9AA
US zip code: 78350

just an idea? btw i have no idea how to use the class lol