Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

bntz313

macrumors 6502
Original poster
Jul 11, 2007
399
0
Ok. I need to make an email form for on a website and I coded it into my website but now it doesn't work link Click on contacts, it doesn't seem to work.

Heres my code:
HTML:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>Cycles in the City | Contact</title>
<meta name="keywords" content="harley, davidson, harley davidson, detoit, motorcycles, biker, bikes, biker bob's, biker bobs, taylor, michigan, detroit, motown, events, hogs, HOGS, hog, HOG, HD" />
<meta name="description" content="Cycles in the City is hosted by Biker Bob's Harley Davidson Motown and Susan G. Komen for the Cure. It's in the heart of downtown Detroit." />
<link href="style_2.css" rel="stylesheet" type="text/css" media="screen" />
<?php

// get posted data into local variables
$EmailFrom = Trim(stripslashes($_POST['EmailFrom'])); 
$EmailTo = "abetancourt@bikerbobshd.com";
$Subject = "Cycles in the City info";
$Name = Trim(stripslashes($_POST['Name'])); 
$Address = Trim(stripslashes($_POST['Address'])); 
$Telephone = Trim(stripslashes($_POST['Telephone'])); 

// validation
$validationOK=true;
if (Trim($EmailFrom)=="") $validationOK=false;
if (Trim($Name)=="") $validationOK=false;
if (!$validationOK) {
  print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
  exit;
}

// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Address: ";
$Body .= $Address;
$Body .= "\n";
$Body .= "Telephone: ";
$Body .= $Telephone;
$Body .= "\n";

// send email 
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");

// redirect to success page 
if ($success){
  print "<meta http-equiv=\"refresh\" content=\"0;URL=ok.htm\">";
}
else{
  print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
}
?>
</head>
<body>
<!-- start header -->
<div id="header">
	<div id="menu">
		<ul>
			<li><a href="index.html">Home</a></li>
			<li><a href="bands.html">Music</a></li>
		  	<li><a href="sponsors.html">Sponsors</a></li>
			<li><a href="map.html">Map</a></li>
			<li class="current_page_item"><a href="contact.html">Contact</a></li>
	  </ul>
	</div>
</div>
<div id="logo">
	</div>
<!-- end header -->
<hr />
<!-- start page -->
<div id="page">
	<!-- start content -->
	<div id="sponsors">
	  <div class="post_sponsors">
	<form method="POST" action="contact.php">
Fields marked * are required

<p>Email From:* <br>
<input type="text" name="EmailFrom">
<p>Name:* <br>
<input type="text" name="Name">
<p>Address:<br>
<input type="text" name="Address">
<p>Telephone:<br>
<input type="text" name="Telephone">
<p><input type="submit" name="submit" value="Submit">
</form>
<p>

	  </div>		
	</div>
	</div>
<!-- end page -->
<hr />
<!-- start footer -->
<div id="footer">
	<p>©2009 All Rights Reserved.  •  Cycles in the City.</p>
</div>
<!-- end footer -->
</body>
</html>

This is my first time ever using php
 
First off, there is supposed to be a page named "contact.html" loaded when users click on your contact link in your menu. It does not exist and the web server redirects users to an error page. Fix that first, looks like you need to upload the below as "contact.html" in your document root. You've got plenty of other issues other folks here will discuss with you involving everything from basic security concerns to checking how to see if a POST global variable exists, etc.

For now, please fix the page and also compare you code to this method which is a great example to see how it's done with a form on a single page. See the section named "PHP Mail Form" there. Model your code after this method, using your own field names and form of course. Even though the example uses the REQUEST global variable, use POST instead as you did in your code - it's a better (more secure and scalable) choice although either may be used.

-jim
 
hey, I just figured out that I need a contact.php file, I have that in my public directory but now it won't work link

When I click submit it sends me to an error page
 
Ok, so the form shows up. but now when I go to email me in doesn't work. I get the error page. I got the php from a php generator, because I don't know any php.

This is the php:
HTML:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Contact</title>
</head>

<?php

// get posted data into local variables
$EmailFrom = Trim(stripslashes($_POST['EmailFrom'])); 
$EmailTo = "http://www.abetancourt@bikerbobshd.com";
$Subject = "Cycles in the City Info";
$Name = Trim(stripslashes($_POST['Name'])); 
$Address = Trim(stripslashes($_POST['Address'])); 
$Telephone = Trim(stripslashes($_POST['Telephone'])); 

// validation
$validationOK=true;
if (Trim($EmailFrom)=="") $validationOK=false;
if (Trim($Name)=="") $validationOK=false;
if (!$validationOK) {
  print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
  exit;
}

// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Address: ";
$Body .= $Address;
$Body .= "\n";
$Body .= "Telephone: ";
$Body .= $Telephone;
$Body .= "\n";

// send email 
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");

// redirect to success page 
if ($success){
  print "<meta http-equiv=\"refresh\" content=\"0;URL=ok.htm\">";
}
else{
  print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
}
?>

</body>
</html>

and this is the html:
HTML:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>Cycles in the City | Contact</title>
<meta name="keywords" content="harley, davidson, harley davidson, detoit, motorcycles, biker, bikes, biker bob's, biker bobs, taylor, michigan, detroit, motown, events, hogs, HOGS, hog, HOG, HD" />
<meta name="description" content="Cycles in the City is hosted by Biker Bob's Harley Davidson Motown and Susan G. Komen for the Cure. It's in the heart of downtown Detroit." />
<link href="style_2.css" rel="stylesheet" type="text/css" media="screen" />

</head>
<body>
<!-- start header -->
<div id="header">
	<div id="menu">
		<ul>
			<li><a href="cnc.html">Home</a></li>
			<li><a href="bands.html">Music</a></li>
		  	<li><a href="sponsors.html">Sponsors</a></li>
			<li><a href="map.html">Map</a></li>
			<li class="current_page_item"><a href="contact.html">Contact</a></li>
	  </ul>
	</div>
</div>
<div id="logo">
	</div>
<!-- end header -->
<hr />
<!-- start page -->
<div id="page">
	<!-- start content -->
	<div id="sponsors">
	  <div class="post_sponsors">
	<form method="POST" action="contact.php">
Fields marked * are required

<p>Email From:* <br>
<input type="text" name="EmailFrom">
<p>Name:* <br>
<input type="text" name="Name">
<p>Address:<br>
<input type="text" name="Address">
<p>Telephone:<br>
<input type="text" name="Telephone">
<p><input type="submit" name="submit" value="Submit">
</form>
<p>

	  </div>		
	</div>
	</div>
<!-- end page -->
<hr />
<!-- start footer -->
<div id="footer">
	<p>©2009 All Rights Reserved.  •  Cycles in the City.</p>
</div>
<!-- end footer -->
</body>
</html>
 
hey, I just figured out that I need a contact.php file, I have that in my public directory but now it won't work link When I click submit it sends me to an error page

Sorry but I don't even see the form on that page with my FF3 browser. Below the "cycles in the city" image comes your footer, I don't see the form in between. Bad HTML is to blame.

You have 43 errors on the HTML page - so please fix the critical ones to get your form working and then work on the PHP stuff.

I suggest switching the DTD to HTML 4.01 if you have these many errors using XHTML transitional 1.0 but still validate the page.

-jim
 
I fixed the errors but haven't uploaded it yet.

For the record, while we await you to upload:

If you don't know PHP "at all" - that's like asking a mechanic to allow you to work on the engine simply because you know how to drive. You cannot simply copy/paste code from the Internet and expect it to work. I suggest you take this basic tutorial on PHP first, then follow closely the original link I gave you line by line, and then consult php.net if you need to learn how a function works as you come across one in the code example I gave you.

If you have zero programming experience in general, i.e. you don't even know what a function is and how to pass arguments in any language, you should either hire a pro or dive in and learn. This reply is not to be taken as mean spirited or condescending in any way, but your comments and actions so far reveal you might be a bit over your head on this one.

The link I gave you, once you fix the HTML issue for the form, honestly is like giving you the answers to a test in advance. It's a great way to learn how to code in PHP, put your code next to the example I linked, compare, line by line until you understand how it works. You'll find PHP really is easy to learn, once you dive in. I could not be more helpful so far, is my point, without doing it for you. I hope you realize that as you continue on! :) :) :)

-jim
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.