PHP/CSS integrated email form

Okay, I looked at many PHP email form examples on stackoverflow and other sites, and however insightful I still cannot find a solution for my specific situation. I want to make a simple email form in HTML and embed it. I made the php file needed for it, but I still cannot get it to work. Mind you, I am still new to PHP. I made sure the form tag references the submit.php file, but whenever I click the “submit” button in the browser, it just gives me the php text.

Here is my code, what am I doing wrong? (If it’s stupid simple, then dock me, I gotta learn how to this soon and if loosing points is the route, fine.)

This is the HTML


<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>small Groups Email Form</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
</header>


<html>
<body>
<font face=Arial size=2>
<form method="post" action="submit.php">

<table align=left>
<option value="gschudel@mannachurch.org"></option>
<tr>
<td><font color=red>*</font> Name:</td>
<td>
  <input size=25 name="Name"></td></tr>  <tr><td>
      <font color=red>*</font> Email:</td><td>
      <input size=25 name="badrobot"></td></tr>
      <tr><td>Are you human?</td><td><br>
      <input type="radio" name="list" value="No"> No
      <input type="radio" name="list" value="Yes" checked> Yes <br></td></tr>
      <tr><td colspan=2>Message:</td></tr>  <tr>
      <td colspan=2 align=left>
      <textarea name="Message" rows=5 cols=35></textarea></td></tr>
      <tr><td colspan=2 align=left>
      <input type=submit name="send" value="Submit"></td></tr>
      <tr><td colspan=2 align=left>
      <small>A <font color=red>*</font> indicates a field is required</small></td>
      </tr>
  </table>

</form>
</body>
</html>

And here is my PHP (submit.php) file:


<?php

$to = $_REQUEST['sendto'] ;
$from = $_REQUEST['Email'] ;
$name = $_REQUEST['Name'] ;
$headers = "From: $from";
$subject = "Web Contact Data";
$fields = array();  $fields{"Name"} = "Name";
$fields{"Email"} = "Email";
$fields{"badrobot"} = "badrobot";
$fields{"Message"} = "Message";
$body = "We have received the following information:\
\
";

foreach($fields as $a => $b){ $body .= sprintf("%20s: %s\
",$b,$_REQUEST[$a]); }

$headers2 = "From: noreply@yourcompany.com";  $subject2 = "Thank you for contacting us";

$autoreply = "Thank you for contacting us. Somebody will get back to you as
soon as possible, usualy within 48 hours. If you have any more questions,
please consult our website at www.oursite.com";

if($from == '') {print "You have not entered an email, please go back and try again";}
else {  if($name == '') {print "You have not entered a name, please go back and
try again";}
else {  $send = mail($toexample@example.com, $subject, $body, $headers);
$send2 = mail($from, $subject2, $autoreply, $headers2);

if($send)  {print ( "Thank you for filling out our form! We will be contact with
you soon." );}

else  {print "We encountered an error sending your mail, please
notify webmaster@YourCompany.com"; }  } }

?>

Try changing $headers to this.


$headers = "From: $from\\r\
";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\\r\
";

I have no

$headers = "From: $from\\r\
";  

Do you mean this?

$headers = "From: $from"; 

How are you testing your code? That is, are you uploading it to a web server, or running a server with PHP support on your local machine, something like WAMP or MAMP?

When you say it just gives you the PHP text, it makes me think that whatever is handling your form doesn’t know that the php file you’re linking to should be run as a php program on your server, not just displayed. Sorry if I’ve misunderstood the problem.

I’m testing it in a browser. The files are just sitting on my desktop. Do I need a server for this to work? I guess that would make sense since PHP is used on the backend, right?

Yes, you need a server to process the PHP. Without that you will just see what you’re seeing as there’s nothing around to actually process the PHP language.

So could I test with with MAMP, and then perhaps use it on a Joomla site, provided I place the php file in the right spot of the Joomla directory?

No idea about Joomla sorry, but yes, I believe MAMP would do it. I use WAMP here but I don’t think that handles the mail side.

Does that require any special configuration on MAMP side? I know you use WAMP, but just curious. Do I just simply throw it on the server, fire up MAMP and click the “submit” button?

I tried substituting

$headers = "From: $from\\r\
"; 

for

$headers .= "Content-Type: text/html; charset=ISO-8859-1\\r\
";

It still doesn’t work. I’ve even tested in a MAMP server. When I click the submit button it just goes to the submit.php file and shows the code in the browser. Do I have to configure the MAMP server to utilize a SMTP somehow?

Well there are issues with the form, in that <option> is not in a select group and the input field for email is missing. You also have the badrobot input shown and I assume it should be hidden. Here’s my quick edit of your code on one page.

<?php
//echo "<pre>";
//print_r($_POST);
//echo "<pre>";
if(isset($_POST['send'])){
	$to = "example@example.com" ;
	$from = $_POST['Email'] ;
	$name = $_POST['Name'] ;
	$headers = "From: $from\\r\
";
	$headers .= "Content-Type: text/html; charset=ISO-8859-1\\r\
";
	$subject = "Web Contact Data";
	$fields = array();
	$fields{"Name"} = "Name";
	$fields{"Email"} = "Email";
	$fields{"badrobot"} = "badrobot";
	$fields{"Message"} = "Message";
	
	$body = "We have received the following information:\
\
";
	
	foreach($fields as $a => $b){
		$body .= sprintf("%20s: %s\
",$b,$_POST[$a]);
	}
	//echo $body;
	$headers2 = "From: noreply@yourcompany.com";
	$subject2 = "Thank you for contacting us";
	
	$autoreply = "Thank you for contacting us. Somebody will get back to you as
	soon as possible, usualy within 48 hours. If you have any more questions,
	please consult our website at www.oursite.com";
	
	if(empty($from)){
		$message = "You have not entered an email, please go back and try again";
	}elseif(empty($name)){
		$message = "You have not entered a name, please go back and try again";
	}else{
		$send = mail($to, $subject, $body, $headers);
		$send2 = mail($from, $subject2, $autoreply, $headers2);
	
		if($send){
			$message = "Thank you for filling out our form! We will be contact with you soon.";
		}else{
			$message = "We encountered an error sending your mail, please notify webmaster@YourCompany.com";
		}
	}
}
?>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>small Groups Email Form</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style type="text/css">
.left{
float:left;
}
.red{
color:red;
}
.w175{
width:175px;
}
.text-left{
text-align:left;
}
</style>
</head>
<body>
<?php
if(isset($message)){ echo $message;}
?>
<form method="post" action="">

	<table class="left">
		<tr>
			<td><span class="red">*</span> Name:</td>
			<td><input type="text" name="Name" class="w175" /></td>
		</tr>
		<tr>
			<td><span class="red">*</span> Email:</td>
			<td>
				<input type="text" name="Email" value="gschudel@mannachurch.org" class="w175" />
				<input type="hidden" name="badrobot" />
			</td>
		</tr>
		<tr>
			<td>Are you human?</td>
			<td>
				<input type="radio" name="human" value="No" /> No
				<input type="radio" name="human" value="Yes" checked="checked" /> Yes
			</td>
		</tr>
		<tr>
			<td colspan="2">Message:</td>
		</tr>
		<tr>
			<td colspan="2" class="text-left">
				<textarea name="Message" rows=5 cols=35></textarea>
			</td>
		</tr>
		<tr>
			<td colspan="2" class="text-left">
				<input type="submit" name="send" value="Submit">
			</td>
		</tr>
		<tr>
			<td colspan="2" class="text-left">
				<small>A <span class="red">*</span> indicates a field is required</small>
			</td>
		</tr>
	</table>

</form>
</body>
</html>
1 Like

I didn’t fix those curly brackets in the last version for the $fields array.

<?php
//echo "<pre>";
//print_r($_POST);
//echo "<pre>";
if(isset($_POST['send'])){
	$to = "example@example.com" ;
	$from = $_POST['Email'] ;
	$name = $_POST['Name'] ;
	$headers = "From: $from\\r\
";
	$headers .= "Content-Type: text/html; charset=ISO-8859-1\\r\
";
	$subject = "Web Contact Data";
	$fields = array();
	$fields['Name'] = "Name";
	$fields['Email'] = "Email";
	$fields['badrobot'] = "badrobot";
	$fields['Message'] = "Message";
	
	$body = "We have received the following information:\
\
";
	
	foreach($fields as $a => $b){
		$body .= sprintf("%20s: %s\
",$b,$_POST[$a]);
	}
	//echo $body;
	$headers2 = "From: noreply@yourcompany.com";
	$subject2 = "Thank you for contacting us";
	
	$autoreply = "Thank you for contacting us. Somebody will get back to you as
	soon as possible, usualy within 48 hours. If you have any more questions,
	please consult our website at www.oursite.com";
	
	if(empty($from)){
		$message = "You have not entered an email, please go back and try again";
	}elseif(empty($name)){
		$message = "You have not entered a name, please go back and try again";
	}else{
		$send = mail($to, $subject, $body, $headers);
		$send2 = mail($from, $subject2, $autoreply, $headers2);
	
		if($send){
			$message = "Thank you for filling out our form! We will be contact with you soon.";
		}else{
			$message = "We encountered an error sending your mail, please notify webmaster@YourCompany.com";
		}
	}
}
?>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>small Groups Email Form</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style type="text/css">
.left{
float:left;
}
.red{
color:red;
}
.w175{
width:175px;
}
.text-left{
text-align:left;
}
</style>
</head>
<body>
<?php
if(isset($message)){ echo $message;}
?>
<form method="post" action="">

	<table class="left">
		<tr>
			<td><span class="red">*</span> Name:</td>
			<td><input type="text" name="Name" class="w175" /></td>
		</tr>
		<tr>
			<td><span class="red">*</span> Email:</td>
			<td>
				<input type="text" name="Email" value="gschudel@mannachurch.org" class="w175" />
				<input type="hidden" name="badrobot" />
			</td>
		</tr>
		<tr>
			<td>Are you human?</td>
			<td>
				<input type="radio" name="human" value="No" /> No
				<input type="radio" name="human" value="Yes" checked="checked" /> Yes
			</td>
		</tr>
		<tr>
			<td colspan="2">Message:</td>
		</tr>
		<tr>
			<td colspan="2" class="text-left">
				<textarea name="Message" rows=5 cols=35></textarea>
			</td>
		</tr>
		<tr>
			<td colspan="2" class="text-left">
				<input type="submit" name="send" value="Submit">
			</td>
		</tr>
		<tr>
			<td colspan="2" class="text-left">
				<small>A <span class="red">*</span> indicates a field is required</small>
			</td>
		</tr>
	</table>

</form>
</body>
</html>

You are probably already doing this, but I wanted to make sure. Even with MAMP or WAMP, you can’t double-click on the file to make it work. You have to open to localhost (which is the /www root folder location) and navigate from there to the file of your page. So your page URL should begin with “localhost”.

1 Like