PHP Tutorial: Easy Form Mailer

If you need help with something which doesn't fit in to another forum section, then post here.

PHP Tutorial: Easy Form Mailer

Postby egbakaet on Wed Dec 14, 2005 6:14 pm

In followup of Wildross's html tutorial.

What is PHP?
PHP is a widely-used general-purpose scripting language that is especially suited for Web development and can be embedded into HTML.

Today's Tutorial: Easy Contact Form
In today's tutorial, I'll show you how to make an easy contact form. In PHP, there are objects called Operators and Variables. Operators allow us to compare strings, for example:

Code: Select all
<?php
$Num1 = 1;
$Num2 = 2;

if ($Num1 < $Num2) {
echo "$Num is less than $Num2";
}

?>


Here the less than operator "<" is used. There are numerous other operators, as seen here:
  • == is equal to
  • != is not equal
  • > is greater than
  • < is less than
  • >= is greater than or equal
  • <= is less than or equal to
In any contact form in php, the mail(); function must be used. Here's an example:

Code: Select all
<?php mail('someone@example.com', 'My Subject', 'woot you suck lol'); ?>


This code sends an email to someone@example.com with the subject 'My Subject' and the message 'woot you suck lol'.

Now that we have some basic knowledge of operators, I'll move on. Using the $_POST method, we can extract the information typed into a box after the submit button has been pressed. For this tutorial, create a new file called email.html. In there, start with this:

Code: Select all
<form action="contact.php" method="post">
Your Name:<br> <input type="text" size="30" name="name"><br>
Your Email:<br> <input type="text" size="30" name="email"><br>
Subject:<br> <input type="text" size="30" name="subject"><br>
Your Message:<br> <textarea name="message" cols="30" rows="10"><br>
<input type="submit" value="Submit">
</form>


Basically, this is a form in which the name, email, subject, and message boxes are displayed for the user to enter input to email to someone. Now onto the contact.php file. Start with this:

Code: Select all
<?php

$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];


This basically just gets the information the user inputted into the boxes. The $_POST method allows us to do this. Now onto error checking. To make sure a person has entered text into a textbox, we can use the empty(); function. In order for us to show a message if something is empty, we can use if...else statements like we did in example 1 to see if num1 was less than num2. For our form, it would look something like this (continued in the same file):

Code: Select all
If(empty($name)) {
echo "Name was empty.<br>"; }

If(empty($email)) {
echo "Email was empty.<br>"; }

If(empty($subject)) {
echo "Subject was empty.<br>"; }

If(empty($message)) {
echo "Message was empty.<br>"; }


Here, the if statements check to see if $name, $email, $subject, and $message are empty, if true, display an error describing what's missing. Not too hard eh? Now onto the mail(); part. For this part, you'll need logical operators:
  • && denotes in english 'and'
  • || denotes in english 'or'
  • ! denotes in english 'not'
If you understand that, lets continue. This part of the code checks to see that if everything contains a value, and if so, it continues on.

Code: Select all
if (!empty($name) && !empty($email) && !empty($message) && !empty($subject)){


Notice the && and the !'s? They can be really helpful in if...else statements and in php in general. Now to the fun part:

Code: Select all
$messageproper = "$message. This message was sent from $email";

mail('YOUR EMAIL', $subject, $messageproper);

echo "Thanks. Your email has been sent!";

}
?>


Make sense? Basically in steps, this is what we did:

  1. Made a form with the name, email, subject, and message boxes.
  2. Made a php file in which we (1) Got the information from the textboxes, (2) checked to see if anything was empty and (3), sent an email to the specified email address if nothing was empty.

Here's the entire code for your reference:

contact.php
Code: Select all
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];

If(empty($name)) {
   echo "Name was empty.<br>";
}

If(empty($email)) {
   echo "Email was empty.<br>";
}

If(empty($subject)) {
   echo "Subject was empty.<br>";
}

If(empty($message)) {
   echo "Message was empty.<br>";
}

if (!empty($name) && !empty($email) && !empty($message) && !empty($subject)){

   $messageproper = "$message. This message was sent from $email";

   mail('YOUR EMAIL', $subject, $messageproper);

   echo "Thanks. Your email has been sent!";

}
?>


Hope you enjoyed this tutorial. More to come.


egbakaet
Regular Member
 
0:1:696545
 
Posts: 133
Joined: Sun May 15, 2005 9:35 pm

Postby DLSS on Wed Dec 14, 2005 6:36 pm

woth reading for new ppl i guess :P nice post :P :P
Image
Seany wrote:btw i'm calling you Derek when you're here
saying Dirk makes me feel like we're in a 80s porn film
User avatar
DLSS
Senior Member
 
Player: DLSS
STEAM_0:0:6262865
 
Posts: 2845
Joined: Wed Aug 03, 2005 2:57 pm
Location: 127.0.0.1



Image

Postby wildross on Wed Dec 14, 2005 6:38 pm

i amde him do this for me, been pestering him on msn to help me learn php :D thanks buddy
Sorry to the ppl i let down :-(
User avatar
wildross
UKCS Sponsor
 
Player: Ross (WR)
STEAM_0:0:6979202
 
Posts: 2942
Joined: Thu Jun 09, 2005 3:48 pm
Location: plymouth, Southwest, uk

Postby Xhiea on Wed Dec 14, 2005 6:39 pm

nice tut :D
Image

Need help with signature text?

>>watch my video tutorial for help!<<
User avatar
Xhiea
UKCS Sponsor
 
Player: Xhiea is crap at css
STEAM_0:0:5403780
 
Posts: 8956
Joined: Tue Dec 07, 2004 4:48 pm
Location: York, North Yorkshire



Image

Postby egbakaet on Wed Dec 14, 2005 6:48 pm

Thanks and you're welcome. I plan to do more depending on what you guys think.


egbakaet
Regular Member
 
0:1:696545
 
Posts: 133
Joined: Sun May 15, 2005 9:35 pm

Postby TuPac on Wed Dec 14, 2005 7:19 pm

Elliot cheats! :shock:

HTML I can deal with but PHP just sends me loopy :?

Nice tutorial though :P
Image
User avatar
TuPac
UKCS Life Sponsor
 
Player: MisterOCD
STEAM_0:0:33510428
 
Posts: 5620
Joined: Sat Apr 02, 2005 12:32 am
Location: England

Postby pzych0 on Wed Dec 14, 2005 7:20 pm

Nice guide tbh. I need to stick a form in my AS ICT Project. I'm currently debating whether to use a form mailer or a mysql database.
Image
User avatar
pzych0
UKCS Sponsor
 
Player: dns
STEAM_0:0:4292787
 
Posts: 4078
Joined: Mon May 09, 2005 6:49 pm
Location: Univ. Sheffield

Postby wildross on Wed Dec 14, 2005 7:22 pm

pzych0 wrote:Nice guide tbh. I need to stick a form in my AS ICT Project. I'm currently debating whether to use a form mailer or a mysql database.


go mysql DB, would be better
Sorry to the ppl i let down :-(
User avatar
wildross
UKCS Sponsor
 
Player: Ross (WR)
STEAM_0:0:6979202
 
Posts: 2942
Joined: Thu Jun 09, 2005 3:48 pm
Location: plymouth, Southwest, uk

Postby TuPac on Wed Dec 14, 2005 7:22 pm

I'd love to be able to make a site :cry:

It's far too confusing for me tbh.
Image
User avatar
TuPac
UKCS Life Sponsor
 
Player: MisterOCD
STEAM_0:0:33510428
 
Posts: 5620
Joined: Sat Apr 02, 2005 12:32 am
Location: England

Postby Psychic Duck on Wed Dec 14, 2005 7:25 pm

TuPac wrote:I'd love to be able to make a site :cry:

It's far too confusing for me tbh.


more than happy to walk you through it :)

if you want just add me :)
Image
User avatar
Psychic Duck
UKCS Sponsor
 
Player: PD
STEAM_0:0:5473368
 
Posts: 3294
Joined: Sat Jan 29, 2005 7:33 pm
Location: Berkshire, UK

Postby pzych0 on Wed Dec 14, 2005 7:27 pm

For a basic site, your best bet is to make the whole thing as an image in Adobe Fireworks and export it to Adobe Dreamweaver in slices.

(ZOMG can't believe I brought myself to admit its not macromedia anymore :( )

Then you just have to stick it all in a table for spacing etc and add editable regions in the places you wanna put text. It's easy when you practice a bit :)
Image
User avatar
pzych0
UKCS Sponsor
 
Player: dns
STEAM_0:0:4292787
 
Posts: 4078
Joined: Mon May 09, 2005 6:49 pm
Location: Univ. Sheffield

Postby TuPac on Wed Dec 14, 2005 7:28 pm

Can you stick this with Ross' tutorial or at least sticky it for the nabs among us :roll:

hehe :P
Image
User avatar
TuPac
UKCS Life Sponsor
 
Player: MisterOCD
STEAM_0:0:33510428
 
Posts: 5620
Joined: Sat Apr 02, 2005 12:32 am
Location: England

Postby Little_Devil on Wed Dec 14, 2005 11:28 pm

TuPac wrote:Can you stick this with Ross' tutorial or at least sticky it for the nabs among us :roll:

hehe :P


Done :D


Little_Devil
Exec. Admin (Retired)
 
Player: Aurora
STEAM_0:1:6431437
 
Posts: 18895
Joined: Mon Feb 07, 2005 1:47 pm

Postby egbakaet on Thu Dec 15, 2005 12:25 am

I'll be sure to make a mysql sorting script for all of you who found this too easy lol. :lol:


egbakaet
Regular Member
 
0:1:696545
 
Posts: 133
Joined: Sun May 15, 2005 9:35 pm

Postby TuPac on Thu Dec 15, 2005 1:48 am

egbakaet wrote:I'll be sure to make a mysql sorting script for all of you who found this too easy lol. :lol:


Yeah that was very easy 8)

Bring on the C++ (I have no idea what that is, Ross said he was learning it) :P
Image
User avatar
TuPac
UKCS Life Sponsor
 
Player: MisterOCD
STEAM_0:0:33510428
 
Posts: 5620
Joined: Sat Apr 02, 2005 12:32 am
Location: England

Postby egbakaet on Thu Dec 15, 2005 2:17 am

C++ is more of a non-web based programming language. I'm going to learn ASP and CGI soon enough.


egbakaet
Regular Member
 
0:1:696545
 
Posts: 133
Joined: Sun May 15, 2005 9:35 pm

Postby Little_Devil on Thu Dec 15, 2005 7:43 am

egbakaet wrote:C++ is more of a non-web based programming language. I'm going to learn ASP and CGI soon enough.


Stay on topic please, I stickied this as a tutorial thread.


Little_Devil
Exec. Admin (Retired)
 
Player: Aurora
STEAM_0:1:6431437
 
Posts: 18895
Joined: Mon Feb 07, 2005 1:47 pm

OOP Email Form

Postby egbakaet on Thu Dec 15, 2005 9:27 pm

For those of you advanced php users:

Code: Select all
<?php

class MailClass
{
var $subject;
var $name;
var $email;
var $message;

function mailto( )
{

function valid_email($var)
{
if(!empty($var)) {
if (ereg("^[a-zA-Z0-9_\.\-]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$", $var))
return true;
else
return false;
}
}

if(isset($_POST["send"])){
$this->new_string = $_SESSION['new_string'];

If(empty($this->name)) {
echo "<img src=\"images/arrow.gif\"> Name was empty.<br>"; }

If(empty($this->email)) {
echo "<img src=\"images/arrow.gif\"> Email was empty.<br>"; }

If(!empty($this->email) && !valid_email($this->email)) {
echo "<img src=\"images/arrow.gif\"> Email was invalid.<br>"; }

If(empty($this->subject)) {
echo "<img src=\"images/arrow.gif\"> No subject specified.<br>"; }

If(empty($this->message)) {
echo "<img src=\"images/arrow.gif\"> Message was empty.<br>"; }

if(empty($this->name) || empty($this->subject) || empty($this->message) || (empty($this->email) || !valid_email($this->email))) { echo "<br>"; }


if (!empty($this->name) && valid_email($this->email) && !empty($this->email) && !empty($this->subject)){
 
      $headers  = "MIME-Version: 1.0";
      $headers .= "Content-type: text/html; charset=iso-8859-1";
      $headers .= "From: ".$this->email."";

$messageproper =

"This message was sent from:<br>\n" .
   "$this->email<br>\n" .
   "------------------------- $this->subject --------------------<br>\n\n" .
                "<b>Name:</b> $this->name<br>\n" .
                "<b>Email:</b> $this->email<br>\n" .
                "<b>Subject:</b> $this->subject<br>\n" .
                "<b>Message:</b> $this->message<br>\n" .
               
   "\n\n------------------------------------------------------------\n" ;

mail("YOUR EMAIL", $this->subject, $messageproper, "From: \"$this->name\" <$this->email>\nReply-To: \"$this->name\" <$this->email>\nX-Mailer: chfeedback.php 2.02\r\nContent-Type: text/html; charset=\"iso-8859-1\"" );
echo("Thank you. Your message has been sent.<br><br>");

}
}
}

}

if(isset($_POST["send"])){
$this = new MailClass;
$this->subject = $_POST['subject'];
$this->name = $_POST['name'];
$this->email = $_POST['email'];
$this->message = $_POST['message'];
$out = $this->mailto();
$out;
}

?>
<form action="<?PHP $_SERVER['PHP_SELF']; ?>" method="post">
<table width="350" border="0" cellpadding="0" cellspacing="0">
<tr>

<td width="50%" valign="top">
Name:<font color="#CC0000">*</font>
<td width="50%" valign="top">
<input type="text" size="30" name="name" class="inputbox" value="<?php $name = (!empty($_POST['name']) ? $_POST['name'] : ""); echo $name; ?>" tabindex="1">
</tr>

<tr>
<td width="50%" valign="top">
Email:<font color="#CC0000">*</font>
<td width="50%" valign="top">
<input type="text" size="30" name="email" class="inputbox" value="<?PHP $email = (!empty($_POST['email']) ? $_POST['email'] : ""); echo $email; ?>" tabindex="2">
</tr>

<tr>
<td width="50%" valign="top">
Subject:<font color="#CC0000">*</font>
<td width="50%" valign="top">
<input type="text" size="30" name="subject" class="inputbox" value="<?PHP $subject = (!empty($_POST['subject']) ? $_POST['subject'] : ""); echo $subject; ?>" tabindex="3">
</tr>
<tr>
<td width="50%" valign="top">
Message:<font color="#CC0000">*</font>

<td width="50%" valign="top">
<textarea name="message" cols="62" rows="10" class="inputbox" tabindex="4"><?PHP $message = (!empty($_POST['message']) ? $_POST['message'] : ""); echo $message; ?></textarea>

</tr>
<tr>
<td width="50%" align="left">
<td width="50%" align="left">
<input type="submit" name="send" value="Submit" class="inputbox" style="width:50px"> <input type="reset" name="Reset" value="Reset" class="inputbox" style="width:50px">
</tr>
</table>
</form>


egbakaet
Regular Member
 
0:1:696545
 
Posts: 133
Joined: Sun May 15, 2005 9:35 pm

Postby wildross on Thu Dec 15, 2005 11:23 pm

This works, i made a form on wildmedia with the help of egbakeat and its works,

thanks mate
Sorry to the ppl i let down :-(
User avatar
wildross
UKCS Sponsor
 
Player: Ross (WR)
STEAM_0:0:6979202
 
Posts: 2942
Joined: Thu Jun 09, 2005 3:48 pm
Location: plymouth, Southwest, uk

Postby egbakaet on Thu Dec 15, 2005 11:24 pm

Cheers mate. Glad you like it :)


egbakaet
Regular Member
 
0:1:696545
 
Posts: 133
Joined: Sun May 15, 2005 9:35 pm

Next

Return to I need help with...

Who is online

Users browsing this forum: CommonCrawl [Bot] and 0 guests