Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

contact form with logo tutorial


  • Please log in to reply
35 replies to this topic

#1 midnightrose2008

midnightrose2008

    CC Newcomer

  • Just Joined
  • PipPip
  • 16 posts

Posted 25 June 2008 - 09:16 AM

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.
  • 0

#2 Xav

Xav

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 8356 posts

Posted 25 June 2008 - 10:44 AM

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.
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#3 chili5

chili5

    CC Mentor

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3038 posts
  • Programming Language:Java, C#, PHP, JavaScript, Ruby, Transact-SQL
  • Learning:C, Java, C++, C#, PHP, JavaScript, Ruby, Transact-SQL, Assembly, Scheme, Haskell, Others

Posted 25 June 2008 - 11:34 AM

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
  • 0

#4 Xav

Xav

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 8356 posts

Posted 25 June 2008 - 11:43 AM

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;
}

}

?>

  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#5 chili5

chili5

    CC Mentor

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3038 posts
  • Programming Language:Java, C#, PHP, JavaScript, Ruby, Transact-SQL
  • Learning:C, Java, C++, C#, PHP, JavaScript, Ruby, Transact-SQL, Assembly, Scheme, Haskell, Others

Posted 25 June 2008 - 12:05 PM

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 :)
  • 0

#6 Xav

Xav

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 8356 posts

Posted 25 June 2008 - 12:11 PM

Is Mercury Mail an email client, like Gmail or Outlook?
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#7 chili5

chili5

    CC Mentor

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3038 posts
  • Programming Language:Java, C#, PHP, JavaScript, Ruby, Transact-SQL
  • Learning:C, Java, C++, C#, PHP, JavaScript, Ruby, Transact-SQL, Assembly, Scheme, Haskell, Others

Posted 25 June 2008 - 12:35 PM

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
  • 0

#8 Xav

Xav

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 8356 posts

Posted 25 June 2008 - 12:56 PM

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. :)
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#9 chili5

chili5

    CC Mentor

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3038 posts
  • Programming Language:Java, C#, PHP, JavaScript, Ruby, Transact-SQL
  • Learning:C, Java, C++, C#, PHP, JavaScript, Ruby, Transact-SQL, Assembly, Scheme, Haskell, Others

Posted 25 June 2008 - 01:02 PM

I would do that too but I don't have a place online to put a web site or a web site. :P
  • 0

#10 John

John

    CC Mentor

  • Moderator
  • 4450 posts
  • Location:New York, NY

Posted 25 June 2008 - 01:24 PM

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.
  • 0

#11 Xav

Xav

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 8356 posts

Posted 26 June 2008 - 08:10 AM

You mean like other regular expressions if we need to validate anything? I've never taken the time to learn all the characters.
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#12 chili5

chili5

    CC Mentor

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3038 posts
  • Programming Language:Java, C#, PHP, JavaScript, Ruby, Transact-SQL
  • Learning:C, Java, C++, C#, PHP, JavaScript, Ruby, Transact-SQL, Assembly, Scheme, Haskell, Others

Posted 26 June 2008 - 08:52 AM

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
  • 0




Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download