Copy link to clipboard
Copied
Hi,
I am trying to add an auto responder to one of my existing forms I made in Dreamweaver. If it has to be php, please give me detailed instructions on how to set up the form, and more importantly, how to set up a php file. Also, where does that php file go? Where on the server?
Thanks so much!
Greg
<?php
if ($_POST){
$name = substr(strip_tags(html_entity_decode($_POST['name'])),0,50);
$company = substr(strip_tags(html_entity_decode($_POST['company'])),0,50);
$phone = substr(strip_tags(html_entity_decode($_POST['phone'])),0,15);
$subject = substr(strip_tags(html_entity_decode($_POST['subject'])),0,50);
$message = substr(strip_tags(html_entity_decode($_POST['message'])),0,200);
$email = filter_input(INPUT_POST, 'email', F
...Copy link to clipboard
Copied
Every time I add these, I get an error that the message could not be sent
But you have not shown us the code the gives you the error, so I can't see what's wrong
However, before you start creating forms with PHP processing, you should really have a solid understanding of HTML and some knowledge of PHP. If you had that knowledge, you would be able to look at the example I provided and know how to add more form fields and edit your email message.
Copy link to clipboard
Copied
I just wanted to add fields for
Subject, Phone number, Message, Company, and get those to show up on the receiving email.
Copy link to clipboard
Copied
ok, maybe this will help:
<?php
if ($_POST){
$name = substr(strip_tags(html_entity_decode($_POST['name'])),0,50);
$email = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);
if ($name && $email){
$email_from = $email;//<== update the email address
$email_subject = "New Form submission";
$email_body = "You have received a new message from the user $name.\n".
"Here is the message:\n $message";
$to = "webmaster@voipccg.com";//<== update the email address
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $email \r\n";
if (mail($to,$email_subject,$email_body,$headers)){
mail($email,$email_subject,"Your email has been received.",$headers);
$message="The message has been emailed.";
} else {
$message = "good email and name, but could not send.";
}} else {
$message = "Bad email, or bad name, or bad day!";
}
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Form Example</title>
</head>
<body>
<?php echo $message; ?>
<form action='' method='post'>
<p>Email: <input name='email' type='email' required/></p>
<p>Name: <input name='name' type='text' required/></p>
<p>Company: <input name='company' type= 'text' required/></p>
<p>Phone: <input name='phone' type= 'text' required/></p>
<p>Subject: <input name='subject' type= 'text' required/></p>
<p>Message: <input name='message' type= 'text' required/></p>
<p><input name='submit' type='submit'/></p>
</form>
</body>
</html>
As you can see, I added some form fields. How do I get that information from the fields to show in the email. I've been trying different things, and I can''t get it.
Copy link to clipboard
Copied
You added the new fields to the form correctly, but you are not doing anything with them in the PHP processing script.
First you need to sanitize the fields and get them out of the POST array and into a simple variable. Follow the example for name, below. Note that the 50 at the end is the number of characters to allow. You don't want someone to dump the encyclopedia Britannica into your form field.
$name = substr(strip_tags(html_entity_decode($_POST['name'])),0,50);
Now the variable name can be used in various places, such as in the email body.
Copy link to clipboard
Copied
I'm sorry, where do I add that name tag?
And what about the others?
Copy link to clipboard
Copied
ok, I'm getting it.
Copy link to clipboard
Copied
ok, I followed your instructions for the name tag, and it works. So I did all the same stuff for my other form fields, but they don't work. This is what I have:
<?php
if ($_POST){
$name = substr(strip_tags(html_entity_decode($_POST['name'])),0,50);
$company = substr(strip_tags(html_entity_decode($_POST['company'])),0,50);
$phone = substr(strip_tags(html_entity_decode($_POST['phone'])),0,50);
$subject = substr(strip_tags(html_entity_decode($_POST['subject'])),0,50);
$message = substr(strip_tags(html_entity_decode($_POST['message'])),0,50);
$email = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);
if ($name && $email){
$email_from = $email;//<== update the email address
$email_subject = "New Form submission";
$email_body = "You have received a new message from the user $name.\n".
$name = substr(strip_tags(html_entity_decode($_POST['name'])),0,50);
$company = substr(strip_tags(html_entity_decode($_POST['company'])),0,50);
$phone = substr(strip_tags(html_entity_decode($_POST['phone'])),0,50);
$subject = substr(strip_tags(html_entity_decode($_POST['subject'])),0,50);
$message = substr(strip_tags(html_entity_decode($_POST['message'])),0,50);
"Here is the message:\n $message";
$to = "webmaster@voipccg.com";//<== update the email address
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $email \r\n";
if (mail($to,$email_subject,$email_body,$headers)){
mail($email,$email_subject,"Your email has been received.",$headers);
$message="The message has been emailed.";
} else {
$message = "good email and name, but could not send.";
}} else {
$message = "Bad email, or bad name, or bad day!";
}
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Form Example</title>
</head>
<body>
<?php echo $message; ?>
<form action='' method='post'>
<p>Email: <input name='email' type='email' required/></p>
<p>Name: <input name='name' type='text' required/></p>
<p>Company: <input name='company' type= 'text' required/></p>
<p>Phone: <input name='phone' type= 'text' required/></p>
<p>Subject: <input name='subject' type= 'text' required/></p>
<p>Message: <input name='message' type= 'text' required/></p>
<p><input name='submit' type='submit'/></p>
</form>
</body>
</html>
Copy link to clipboard
Copied
This is my final post on this thread. As I said before, you need to learn a little bit about HTML and PHP. Your code example above demonstrates that you don't understand what a "variable" is. That is a term you would learn in the first fifteen minutes of any lesson on PHP.
There are many introductory tutorials on the web on PHP and HTML. Lynda.com has a good collection of tutorials.
We have spent two days going back and forth because you have not yet spent a single hour learning PHP.
Stop what you are doing and go learn a little bit about this stuff. Then come back here when you get stuck.
Copy link to clipboard
Copied
All I was missing was the
Copy link to clipboard
Copied
webmaster698 wrote:
All I was missing was the
You should mark some of Robs posts as 'helpful' if you can as most of the solution was provided by him. I can only take credit for finishing it off.
Copy link to clipboard
Copied
<?php
if ($_POST){
$name = substr(strip_tags(html_entity_decode($_POST['name'])),0,50);
$company = substr(strip_tags(html_entity_decode($_POST['company'])),0,50);
$phone = substr(strip_tags(html_entity_decode($_POST['phone'])),0,15);
$subject = substr(strip_tags(html_entity_decode($_POST['subject'])),0,50);
$message = substr(strip_tags(html_entity_decode($_POST['message'])),0,200);
$email = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);
if ($name && $email && $company && $phone && $message ){
$email_from = 'youremail@domain.com';//<== update the email address
$email_subject = "New Form submission";
$email_body = "You have received a new message from the user $name.\n";
$email_body .= "Here is the message:\n";
$email_body .= "Name: $name\n";
$email_body .= "Company: $company\n";
$email_body .= "Phone Number: $phone\n";
$email_body .= "Subject: $subject\n";
$email_body .= "Message: $message\n";
$to = "webmaster@voipccg.com";//<== update the email address
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $email \r\n";
mail($to,$email_subject,$email_body,$headers);
mail($email,$email_subject,"Your email has been received.",$headers);
$message="The message has been emailed.";
}
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Form Example</title>
</head>
<body>
<?php echo $message; ?>
<form action='' method='post'>
<p>Email: <input name='email' type='email' required/></p>
<p>Name: <input name='name' type='text' required/></p>
<p>Company: <input name='company' type= 'text' required/></p>
<p>Phone: <input name='phone' type= 'text' required/></p>
<p>Subject: <input name='subject' type= 'text' required/></p>
<p>Message: <input name='message' type= 'text' required/></p>
<p><input name='submit' type='submit'/></p>
</form>
</body>
</html>
Copy link to clipboard
Copied
Thanks! I tried your script, and it works great. One more thing, I want it so when a user at whatever their email address is, submits the form, I want the guy receiving the form to get that email from the From Address, up top.
It's hard to explain, but this is what I thought it was:
$headers = "From: $email_from \r\n";
Copy link to clipboard
Copied
At the risk of repeating myself, the email comes from your server. It does not come from the user's email account. Thus hitting the reply button won't work.
Nancy
Copy link to clipboard
Copied
Hitting the "Reply" button does work. II have done it. If a user submits from <mycompany@domain.com>, all the person who receives that email has to do is hit "Reply". A new email is opened, and the "To" address is, <mycompany@domain.com>
Copy link to clipboard
Copied
Hey,
I'm sorry, but I don't understand something. For the message field, I changed it from a textbox to a text area. Now te form won't work, but I don't see where else in the code it needs to be changed. Can you help?
Copy link to clipboard
Copied
You should only need to change the <input> form field to a <textarea></textarea> field:
<p>Message: <textarea name='message' required/></textarea></p>
Copy link to clipboard
Copied
Thanks for the reply. I got it to work. I was just doing something stupid. Thanks though
Copy link to clipboard
Copied
Hey gang, are you seeing why I bowed out of this discussion at post 32? It's at post 44 and the OP is still asking questions. That's because this thread is a private tutorial in basic PHP.
Copy link to clipboard
Copied
Hey Rob,
What does OP stand for?
Copy link to clipboard
Copied
What does OP stand for?
That's forum speak for "original poster"
Copy link to clipboard
Copied
If the OP had taken time to read the 3-part tutorial I posted, he would have a working form that auto generates error & success messages.
Nancy
Copy link to clipboard
Copied
If the OP had taken time to read the 3-part tutorial I posted, he would have a working form that auto generates error & success messages.
Exactly.
I have one client in particular that I must handhold, but at the end of the month I send an invoice.
This forum is very indulgent. And that's a good thing. But I don't like doing someone's work for them. I'd rather point them in the right direction.
Copy link to clipboard
Copied
Nancy OShea wrote:
If the OP had taken time to read the 3-part tutorial I posted, he would have a working form that auto generates error & success messages.
I was wondering if there is any point to error messages since the html 5 'required' attribute generates them, can they be switched off and how likely is that if they can?
I have constructed numerous forms in different guises which generate error messages when form fields are not correctly filled out but should I be bothering any longer?
The only area I can see when some kind of error generation might be necessary is when the 'required' field cant really be used to reflect the correct message, perhaps like a spam question.
Copy link to clipboard
Copied
For clients, I build bullet proof forms.
I use a combination of HTML5 required, plus jQuery Validate for the really tricky fields like phone numbers & spam questions, plus server-side validation if JS is disabled, plus server-side success / error reporting when the form is submitted. This gives the best overall user experience no matter which browser they happen to be using. I have made a cookie cutter template for my forms/scripts, so it's not a major production to pull one together in a hurry.
Nancy
Copy link to clipboard
Copied
Nancy OShea wrote:
For clients, I build bullet proof forms.
I use a combination of HTML5 required, plus jQuery Validate for the really tricky fields like phone numbers & spam questions, plus server-side validation if JS is disabled, plus server-side success / error reporting when the form is submitted. This gives the best overall user experience no matter which browser they happen to be using. I have made a cookie cutter template for my forms/scripts, so it's not a major production to pull one together in a hurry.
Nancy
I only use jQuery as a first line of defence and it looks prettier than error messages generated in php or 'required' but I'm no longer sure if such php error measures are needed if 'required' is used. Server side spambot checker is the one I would not neglect and any field which 'required' can't deal with...........I'm thinking about this one.