Making an email form with php and html?

Can someone here help me out please. All I need to do is make an email form in a html site I am making. I have done it with flash a few times and had no problems. Ok firstly look at my enclosed pic and you will see how i want my form to look - its very simple just 4 input fields and a send and clear button, notice how everything lines up nicely which I need. Ok so now i guess I would need a php script so this can actually work but dont know how. hope i’m making sense so far :slight_smile:

The email again is not essential but would be nice.

My server supports php.

when the mail has been sent, i would like a new page to open up saying thank you for emailing us. Simple as that (well simple for you anyway, I hope) :slight_smile:

I will love you forever if you can help me with this.

Thank you.

http://www.kirupa.com/developer/actionscript/flash_php_email.htm

I want to make it in html not flash, thats my problem. I can do it in flash already

Thanks

Cant believe I’ve been coding on a Sunday :sleep: Only did a real basic version of the script. I’ve made the script self contained, so it does error checking and the two different page outputs in the same script. Commented it as much as I could, but I’m heading out to a party soon so I didnt get to do all of the fancy stuff. If this script is good enough, look up CSS form input styles for the borders etc.

<html>
<head>
<title>PHP Mailer</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<?php
$failNum = 0;
/* First of all, the form checks to see if the user has missed any of the fields on the form.
It can be done in Javascript, but I'm not great at Javascript, so ask anyone else who can do it :) */
if(!empty($_POST['submitted'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$email2 = $_POST['email2'];
$message = $_POST['message'];
 
//checks for a message
if (strlen($message) < 1) {
$message = "";
$failMess = "Please submit a message";
$failNum = 4;
}
 
//checks for the second email address
if (strlen($email2) > 0) {
// checks that the second email address is the same as the first
if ($email2 != $_POST['email']) {
$failMess = "These email addresses do not match. Please check the addresses and try again.";
$failNum = 3;
}
// this checks that the email address contains regular characters used in email addresses, and that you put a domain after the address
else if (!ereg("^([a-zA-Z0-9_\.\-])+@(([a-zA-Z0-9\-])+.)+([a-zA-Z0-9]{2,4})+$", $email2)) {
$failMess = "This email address is not valid. Please check the address and try again.";
$failNum = 3;
}
} else {
$failMess = "Please submit your email address a second time.";
$failNum = 3;
}
 
//check for the first email address by seeing if the POST string is not empty, and that it's length is greater than 0
if (strlen($email) > 0) {
// this checks that the email address contains regular characters used in email addresses, and that you put a domain after the address
if (!ereg("^([a-zA-Z0-9_\.\-])+@(([a-zA-Z0-9\-])+.)+([a-zA-Z0-9]{2,4})+$", $email)) {
$failMess = "This email address is not valid. Please check the address and try again.";
$failNum = 2;
}
} else {
$failMess = "Please submit an email address";
$failNum = 2;
}
 
// Checks they submitted a name by seeing if the POST string is not empty, and that it's length is greater than 0
if (strlen($name) < 1) {
$failMess = "Please submit your name";
$failNum = 1;
}
 
/* if the form has been submitted (this script checks if the hidden input "submitted" is not empty) and it hasnt failed any checks,
a thank you message is output to the screen and the script stops running */
if ($failNum == 0) {
$sendTo = "your.address@you.com"; // Your Email Address
$subject = "My site reply"; // Your Subject
$headers = "From: $name <$email>
"; // From, Reply to, etc
//$headers .= "Reply-To: $email"; 
$message = $message;
mail($sendTo, $subject, $message, $headers);
echo "Thank you for submitting the form :)";
exit;
}
}
// Otherwise the form wasn't submitted, so we have the blank fields
else {
$name = "";
$email = "";
$email2 = "";
$message = "";
}
?> 
<table border="0" cellspacing="5" cellpadding="0">
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="hidden" name="submitted" value="1">
<tr>
<td <?php if ($failNum == 1) echo " style=\"color:#B20C24; font-weight:bold;\" "?>>Your Name</td>
<td><input style="font-size:8pt;" type="text" name="name" value="<?php echo $name?>" style="width:150px;"></td>
</tr>
<tr>
<td<?php if ($failNum == 2) echo " style=\"color:#B20C24; font-weight:bold;\" "?>>Your Email</td>
<td><input style="font-size:8pt;" type="text" name="email" value="<?php echo $email?>" style="width:150px;"></td>
</tr> 
<tr>
<td<?php if ($failNum == 3) echo " style=\"color:#B20C24; font-weight:bold;\" "?>>Email Again</td>
<td><input style="font-size:8pt;" type="text" name="email2" value="<?php echo $email2?>" style="width:150px;"></td>
</tr>
<tr>
<td<?php if ($failNum == 4) echo " style=\"color:#B20C24; font-weight:bold;\" "?>>Message</td>
<td><textarea name="message" style="width:150px;"><?php echo $message?></textarea></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input type="submit" value="Send">&nbsp;&nbsp;<input type="reset" value="Clear"></td>
</tr>
<?php
if ($failNum != 0) {
echo "<tr>
<td style=\"color:#B20C24; font-weight:bold;\" colspan=\"2\">$failMess</td>
</tr>";
}
?>
</form>
</table>
<?php
echo "$name $email $email2 $message";
?>
</body>
</html>

Thank you for taking the time to do that.

My problem is i dont even know how to make the html page with the input fields and send buttons etc, well i sort of do in a way but cant line things up as i would wish and I certainly would not know how to connect it to the page of code you have kindly supplied. I am lost with it really - if anyone has time to knock up a quick page in html and with thie input fields etc that would be most helpful.

I tried lining the words and input boxes up using tables but it doesn’t want to work that way.

Thank you once again for your time. I am pretty confident that I will get this sorted with the help from this forum.

If you look at the first line of code, you will see that the page is actually outputting the HTML. It is just a self contained PHP script, seeing as your server accepts PHP scripts.

Its now got some CSS in it.

<html>
<head>
<title>PHP Mailer</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style>
td {
 font:  10px Arial, Helvetica, sans-serif;
 font-style:italic;
 font-weight:bold;
 color:#000000;
}
input, textarea {
 font:  10px Arial, Helvetica, sans-serif;
 font-style:italic;
 font-weight:bold;
 border-width:1px;
 border-style:solid;
 border-color:#000000;
 vertical-align:middle;
 background-color:#FFFFFF;
 color:#000000;
}
</style>
</head>
<body>
<?php
$failNum = 0;
/* First of all, the form checks to see if the user has missed any of the fields on the form.
   It can be done in Javascript, but I'm not great at Javascript, so ask anyone else who can do it :) */
if(!empty($_POST['submitted'])) {
 $name = $_POST['name'];
 $email = $_POST['email'];
 $email2 = $_POST['email2'];
 $message = $_POST['message'];
 
 //checks for a message
 if (strlen($message) < 1) {
  $message = "";
  $failMess = "Please submit a message";
  $failNum = 4;
 }
 
 //checks for the second email address
 if (strlen($email2) > 0) {
  // checks that the second email address is the same as the first
  if ($email2 != $_POST['email']) {
   $failMess = "These email addresses do not match.  Please check the addresses and try again.";
   $failNum = 3;
  }
  // this checks that the email address contains regular characters used in email addresses, and that you put a domain after the address
  else if (!ereg("^([a-zA-Z0-9_\.\-])+@(([a-zA-Z0-9\-])+.)+([a-zA-Z0-9]{2,4})+$", $email2)) {
   $failMess = "This email address is not valid.  Please check the address and try again.";
   $failNum = 3;
  }
 } else {
  $failMess = "Please submit your email address a second time.";
  $failNum = 3;
 }
 
 //check for the first email address by seeing if the POST string is not empty, and that it's length is greater than 0
 if (strlen($email) > 0) {
  // this checks that the email address contains regular characters used in email addresses, and that you put a domain after the address
  if (!ereg("^([a-zA-Z0-9_\.\-])+@(([a-zA-Z0-9\-])+.)+([a-zA-Z0-9]{2,4})+$", $email)) {
   $failMess = "This email address is not valid.  Please check the address and try again.";
   $failNum = 2;
  }
 } else {
  $failMess = "Please submit an email address";
  $failNum = 2;
 }
 
 // Checks they submitted a name by seeing if the POST string is not empty, and that it's length is greater than 0
 if (strlen($name) < 1) {
  $failMess = "Please submit your name";
  $failNum = 1;
 }
 
 /* if the form has been submitted (this script checks if the hidden input "submitted" is not empty) and it hasnt failed any checks,
   a thank you message is output to the screen and the script stops running */
 if ($failNum == 0) {
  $sendTo = "your.address@you.com"; // Your Email Address
  $subject = "My site reply"; // Your Subject
  $headers = "From: $name <$email>
"; // From, Reply to, etc
  //$headers .= "Reply-To: $email"; 
  $message = $message;
  mail($sendTo, $subject, $message, $headers);
  echo "Thank you for submitting the form :)";
  exit;
 }
}
// Otherwise the form wasn't submitted, so we have the blank fields
else {
 $name = "";
 $email = "";
 $email2 = "";
 $message = "";
}
?> 
<table border="0" cellspacing="10" cellpadding="0">
 <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
 <input type="hidden" name="submitted" value="1">
 <tr>
  <td <?php if ($failNum == 1) echo " style=\"color:#B20C24; font-weight:bold;\" "?> align="right" valign="top">Your name</td>
  <td><input type="text" name="name" value="<?php echo $name?>" style="width:150px; height:17px;"></td>
 </tr>
 <tr>
  <td<?php if ($failNum == 2) echo " style=\"color:#B20C24; font-weight:bold;\" "?> align="right" valign="top">Your email</td>
  <td><input type="text" name="email" value="<?php echo $email?>" style="width:150px; height:17px;"></td>
 </tr> 
 <tr>
  <td<?php if ($failNum == 3) echo " style=\"color:#B20C24; font-weight:bold;\" "?> align="right" valign="top">Email again</td>
  <td><input type="text" name="email2" value="<?php echo $email2?>" style="width:150px; height:17px;"></td>
 </tr>
 <tr>
  <td<?php if ($failNum == 4) echo " style=\"color:#B20C24; font-weight:bold;\" "?> align="right" valign="top">Message</td>
  <td><textarea name="message" style="width:150px; height:150px;"><?php echo $message?></textarea></td>
 </tr>
 <tr>
  <td>&nbsp;</td>
  <td><input type="submit" value="Send" style="width:60px;">&nbsp;&nbsp;<input type="reset" value="Clear" style="width:60px;"></td>
 </tr>
 <?php
 if ($failNum != 0) {
 echo "<tr>
  <td style=\"color:#B20C24; font-weight:bold;\" colspan=\"2\">$failMess</td>
 </tr>";
 }
 ?>
 </form>
</table>
<?php
echo "$name $email $email2 $message";
?>
</body>
</html>

Thanks for that man. I changed the email bit to my own email but i dont get an email when I try it :frowning:

Ah wait - i DID GET IT !!!

wow its ok, i got it thanks man so much.

you genious

:thumb:

hey kaiser, I got it working fine on my page:

http:www.galaxymusic.co.uk/contactpagethree.php

Just one little thing I’d like to ask, Is there a way to do it so that when the mail has been sent, it sends it to a page I have called thankyou.htm instead of the white page with the thank you message it sends to at the moment? It would look more professional if I could do this so I could keep my banner image etc.

Thank you once again for helping me with this. I really do appreciate it :slight_smile:

Look for the line where it echos the thank you message, and swap it for a Javascript redirect

Kaiser was that whole thing pretty much writen in Php? I thought Php was a lot different or looked harder. Hmm might have a look into php. Good job dude that was real nice of you to make that code for him:thumb:

Erm don’t know what one is but i’ll ask around. Thanks

a javascript redirect is not the way to do it apparently. but no one seems to know how to direct this to a different page - sorry

<?php
$failNum = 0;
/* First of all, the form checks to see if the user has missed any of the fields on the form.
   It can be done in Javascript, but I'm not great at Javascript, so ask anyone else who can do it :) */
if(!empty($_POST['submitted'])) {
 $name = $_POST['name'];
 $email = $_POST['email'];
 $email2 = $_POST['email2'];
 $message = $_POST['message'];
 
 //checks for a message
 if (strlen($message) < 1) {
   $message = "";
   $failMess = "Please submit a message";
   $failNum = 4;
 }
 
 //checks for the second email address
 if (strlen($email2) > 0) {
   // checks that the second email address is the same as the first
   if ($email2 != $_POST['email']) {
	$failMess = "These email addresses do not match.  Please check the addresses and try again.";
	$failNum = 3;
   }
   // this checks that the email address contains regular characters used in email addresses, and that you put a domain after the address
   else if (!ereg("^([a-zA-Z0-9_\.\-])+@(([a-zA-Z0-9\-])+.)+([a-zA-Z0-9]{2,4})+$", $email2)) {
	$failMess = "This email address is not valid.  Please check the address and try again.";
	$failNum = 3;
   }
 } else {
   $failMess = "Please submit your email address a second time.";
   $failNum = 3;
 }
 
 //check for the first email address by seeing if the POST string is not empty, and that it's length is greater than 0
 if (strlen($email) > 0) {
   // this checks that the email address contains regular characters used in email addresses, and that you put a domain after the address
   if (!ereg("^([a-zA-Z0-9_\.\-])+@(([a-zA-Z0-9\-])+.)+([a-zA-Z0-9]{2,4})+$", $email)) {
	$failMess = "This email address is not valid.  Please check the address and try again.";
	$failNum = 2;
   }
 } else {
   $failMess = "Please submit an email address";
   $failNum = 2;
 }
 
 // Checks they submitted a name by seeing if the POST string is not empty, and that it's length is greater than 0
 if (strlen($name) < 1) {
   $failMess = "Please submit your name";
   $failNum = 1;
 }
 
 /* if the form has been submitted (this script checks if the hidden input "submitted" is not empty) and it hasnt failed any checks,
	a thank you message is output to the screen and the script stops running */
 if ($failNum == 0) {
   $sendTo = "your.address@you.com"; // Your Email Address
   $subject = "My site reply"; // Your Subject
   $headers = "From: $name <$email>
"; // From, Reply to, etc
   //$headers .= "Reply-To: $email"; 
   $message = $message;
   mail($sendTo, $subject, $message, $headers);
   header('Location: http://www.galaxymusic.co.uk/thankYou.php');
   exit;
 }
}
// Otherwise the form wasn't submitted, so we have the blank fields
else {
 $name = "";
 $email = "";
 $email2 = "";
 $message = "";
}
?>
<html>
<head>
<title>PHP Mailer</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style>
td {
font:  10px Arial, Helvetica, sans-serif;
font-style:italic;
font-weight:bold;
color:#000000;
}
input, textarea {
font:  10px Arial, Helvetica, sans-serif;
font-style:italic;
font-weight:bold;
border-width:1px;
border-style:solid;
border-color:#000000;
vertical-align:middle;
background-color:#FFFFFF;
color:#000000;
}
</style>
</head>
<body>
<table border="0" cellspacing="10" cellpadding="0">
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="hidden" name="submitted" value="1">
<tr>
  <td <?php if ($failNum == 1) echo " style=\"color:#B20C24; font-weight:bold;\" "?> align="right" valign="top">Your name</td>
  <td><input type="text" name="name" value="<?php echo $name?>" style="width:150px; height:17px;"></td>
</tr>
<tr>
  <td<?php if ($failNum == 2) echo " style=\"color:#B20C24; font-weight:bold;\" "?> align="right" valign="top">Your email</td>
  <td><input type="text" name="email" value="<?php echo $email?>" style="width:150px; height:17px;"></td>
</tr> 
<tr>
  <td<?php if ($failNum == 3) echo " style=\"color:#B20C24; font-weight:bold;\" "?> align="right" valign="top">Email again</td>
  <td><input type="text" name="email2" value="<?php echo $email2?>" style="width:150px; height:17px;"></td>
</tr>
<tr>
  <td<?php if ($failNum == 4) echo " style=\"color:#B20C24; font-weight:bold;\" "?> align="right" valign="top">Message</td>
  <td><textarea name="message" style="width:150px; height:150px;"><?php echo $message?></textarea></td>
</tr>
<tr>
  <td>&nbsp;</td>
  <td><input type="submit" value="Send" style="width:60px;">&nbsp;&nbsp;<input type="reset" value="Clear" style="width:60px;"></td>
</tr>
<?php
if ($failNum != 0) {
echo "<tr>
  <td style=\"color:#B20C24; font-weight:bold;\" colspan=\"2\">$failMess</td>
</tr>";
}
?>
</form>
</table>
</body>
</html>

On line 62, there is an address like http://www.galaxymusic.co.uk/thankYou.php, change that to whatever you want the redirected page to be