Jump to content

E-mail forms in php... HELP!


yankeefan238

Recommended Posts

Ok, well i've been trying to make a simple PHP code that will send the forms from an HTML document to my e-mail address. I looked this up online and i've been trying to get it to work but...i can't.

 

Html code:

 

<html>
<head>
<title>Test PHP Script</title>
</head>
<body>
<h1>Enter You Request Below...</h1>
<form action="contact.php" method="post">
First Name: <input type="text" name="fname" />
Last Name: <input type="text" name="lname" />
E-mail: <input type="text" name="email" />
Site type: <input type="text" name="type" />
Site description: <input type="text" name="description" value="Site Features" />
Other: <input type="text" name="other" value="Other Information" />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>

 

PHP Code:

 

<?php
$to = "myemail@email.com" ;
$subject = "Website Design Request" ;
$fname = $_REQUEST['fname'] ;
Slname = $_REQUEST['lname'] ;
$email = $_REQUEST['email'] ;
$stype = $_REQUEST['type'] ;
$sdescription = $_REQUEST['description'] ;
Sother = $_REQUEST['other'] ;
$message = "$fname, $lname, $email, $stype, $sdescription, $other" ;
$headers = "From: $email" ;
$sent = mail($to, $subject, $message, $headers) ;
if ($sent)
{echo "Your request was sent successfully" ; }
else
{echo "We encountered an error sending your request, please try again later" ; }
?>

 

What did I do wrong!?!?!?! Any help would be great!

Thank You!

 

 

Link to comment
Share on other sites

I'm not saying what you have wont work I am still a rookie at this stuff.  But here is the coding I use to get mine to work.  This is the form in PHP coding so it can post to itself.

 

$top_form = "<form method=\"post\" action=\"$_SERVER[php_SELF]\"> <br>
<table width=\"45%\" border=\"2\" cellspacing=\"2\" cellpadding=\"2\" bordercolor=\"#000000\" bgcolor=\"#CCCCCC\">
  <tr>
    <td colspan=\"2\">";

$form_block = "</td>
  </tr>
   <tr class=\"cellbg1\">
    <td colspan=\"2\"><strong>If you are a current THAC0 member you do not need to fill out this application.  Just re-register at the FORUMS link above.</strong></td>
  </tr>
  <tr class=\"cellbg1\">
    <td colspan=\"2\"><strong>Please register on the forums before filling out this application.</strong></td>
  </tr>
  <tr class=\"cellbg1\">
    <td><strong>Character Name:</strong></td>
    <td><input type=\"text\" name=\"Cname\" value=\"$_POST[Cname]\" size=30></td>
  </tr>
  <tr class=\"cellbg1\">
    <td><strong>Who recomended you:</strong></td>
    <td><input type=\"text\" name=\"recomend\" value=\"$_POST[recomend]\" size=30></td>
  </tr>
  <tr class=\"cellbg1\">
    <td><strong>Email Address:</strong></td>
    <td><input type=\"text\" name=\"email\" value=\"$_POST[email]\" size=30></td>
  </tr>
  <tr class=\"cellbg1\">
    <td><strong>Email Address Confirm:</strong></td>
    <td><input type=\"text\" name=\"email1\" value=\"$_POST[email1]\" size=30></td>
  </tr>
  <tr class=\"cellbg1\">
    <td><strong>Why do you want to join THACO:</strong></td>   
    <td><textarea name=\"wjoin\" value=\"$_POST[wjoin]\" cols=60 rows=5></textarea></td>
    </tr>
  <tr class=\"cellbg1\">
    <td><strong>Forum user name:</strong></td>
    <td><input type=\"text\" name=\"login\" value=\"$_POST[login]\" size=30></td>
  </tr>
  <tr align=\"center\" valign=\"middle\" class=\"cellbg1\">
    <td colspan=\"2\">
      <input type=\"submit\" name=\"submit\" value=\"Send this form\">
      <input type=\"hidden\" name=\"op\" value=\"ds\">
      <input type=\"reset\" name=\"reset\" value=\"Reset\">
    </td>
  </tr>
</table>
</form>"; 

 

 

 

Next I check the data to ensure it has been inputed and not left blank.

 

//The first time it loads it will enter into this first part of the if because $_POST[op] has nothing in it.  So the form will be shown.
if ($_POST[op] != "ds") 
{ 
	// they need to see the form 
	echo "$top_form"."$form_block"; 
} 
else if ($_POST[op] == "ds") 
	{ 

//********************************Check to see if the form has been filled out not blank and if the fields match.***************************		

		if (($_POST['email']) != ($_POST['email1']))
			{
				$result = "$result"." <font color=red>Your emails do not match.</font><br>";
				$complete = "no";
			}

		if(empty($_POST['Cname']))
			{
				$result = "$result"." <font color=red>You must enter a character name so we can find you in game.</font><br>";
				$complete = "no";
			}

		if(empty($_POST['recomend']))
			{
				$result = "$result"." <font color=red>You must enter who recomended you to apply or NA</font><br>";
				$complete = "no";
			}

		if(empty($_POST['email']))
			{
				$result = "$result"." <font color=red>You left your email address blank.</font><br>";
				$complete = "no";
			}

		if(empty($_POST['wjoin']))
			{
				$result = "$result"." <font color=red>Please enter why you would like to join THACO</font><br>";
				$complete = "no";
			}

		if ($complete != "no") 
			{ 


 

 

Next is the e-mail part.

 

$additional_headers = "From: newapplicant@thaczero.com\n";
$toaddress = $_POST['email'];
$subject = "Your Thaco application at thaczero.com";
$mailcontent ="Here is the information that you provided our new application officer\n\r" 
."Character Name: " . $_POST['Cname'] . "\n\r"
."Who recomended you: " . $_POST['recomend'] . "\n\r"
."Email address: " . $_POST['email'] . "\n\r"
."Why do you want to join THACO: " . $_POST['wjoin'] . "\n\r"
."Forum user name: " . $_POST['login'] . "\n\r"
. "\n\r"
."Helpfull Tips.\n\r"
."1.  Go to http://thaczero.com/forums/  and make sure you are registered.\n"
. "\n\r"
. "\n\r"
."Thank you for applying to Thac0.  We take all our applications seriously.\n"	
."Be patient as or new applicant processor has real life to deal with as well.  He or she will get in contact with you with in 72 hours.\n"
."Thank you for applying at Thaczero.com Please do not reply to this email as it is an email bot.\n"; 

//The above code formats the email and the one liner below sends the email

mail($toaddress, $subject, $mailcontent, $additional_headers);

 

 

If you wanted to have multiple resipients I am sure you could put it into a loop to make it work.  In this instance I only needed to send the application to 4 total people.  The person applying and to me and 2 other individuals.  So I just coppied and pasted the above code 3 times and changed the email address and hard coded it.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.