PHP Contact form to email [closed] - php

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I used this post to make a simple ‘contact us’ form. The form collects a few pieces of information (email, name and a message) from your visitor and emails it to you.
Instead of redirecting to a thank you page, I'd prefer it to just print the thank you (or error) text in the same HTML form page.
How can this be done? Where do I need to change to be able to keep the user in the same page and show the confirmation message ("Thank you")?
Edit: Found a much simpler way to do this - with ajax.

Here is my suggestion.
1- Change your file name contact-form.html to contact-form.php
2- Added include line and edit your form action in contact-form.php:
<h1>Contact us</h1>
<?php include './contact-form-handler.php'; ?>
<form method="POST" name="contactform" action="#">
3- Edit your contact-form-handler.php, disable header and added your thank you with personal message, you can also added some layout and decoration that is left to your fantasy.
//header('Location: contact-form-thank-you.html');
echo "Thank you " . $name . "<br />";
echo "We will contact you soon";
4- Last thing I will added condition to check if the form is submitted or not
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
if (empty... etc
}
5- (Optional) To re-fetch the posted values if failed I have added following to the form inputs (3 of them, each field got one with the respective field/variable name):
value="<?php if (isset($_POST['name'])) echo $name; ?>"
But in general When all this done, I will personally will go through the form and clean it up a little bit. Like if I am check the content in JavaScript it is not necessary to check if the fields are empty, so checking submission form is enough. I have not talk about form security yet against any kind of misuse, this just to make some thoughts when you come so far.
Complete code
Here is your final results, with extra things I done:
contact-form.php
<form method="POST" name="contactform" action="#">
<p>
<label for='name'>Your Name:</label> <br>
<input type="text" name="name" value="<?php if (isset($_POST['name'])) echo $name; ?>">
</p>
<p>
<label for='email'>Email Address:</label> <br>
<input type="text" name="email" value="<?php if (isset($_POST['email'])) echo $email_address; ?>"> <br>
</p>
<p>
<label for='message'>Message:</label> <br>
<textarea name="message" value="<?php if (isset($_POST['message'])) echo $message; ?>"></textarea>
</p>
<input type="submit" value="Submit"><br>
</form>
contact-form-handler.php
<?php
$errors = '';
$myemail = 'yourname#website.com';//<-----Put Your email address here.
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
if (empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['message'])
)
{
$errors .= "\n Error: all fields are required";
}
$name = $_POST['name'];
$email_address = $_POST['email'];
$message = $_POST['message'];
if (!preg_match(
"/^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i",
$email_address)
)
{
$errors .= "\n Error: Invalid email address";
}
if (empty($errors))
{
$to = $myemail;
$email_subject = "Contact form submission: $name";
$email_body = "You have received a new message. " .
" Here are the details:\n Name: $name \n Email: $email_address \n Message \n $message";
$headers = "From: $myemail\n";
$headers .= "Reply-To: $email_address";
mail($to, $email_subject, $email_body, $headers);
//redirect to the 'thank you' page
//header('Location: contact-form-thank-you.html');
echo "Thank you " . $name . "<br />";
echo "We will contact you soon";
$name = "";
$email_address = "";
$message = "";
}
}
?>

Related

Want to echo "Email sent!" in a specific div after form is submitted [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I'm implementing a contact-form to my website. When I click the "submit" button it sends me the email then redirects user to domain.com/mail.php and echoes "Email sent!" in a blank white page. Instead I want to have the user stay in index.html and have the "Email sent!" echoed inside <div class="alert-msg"></div>. Any help is appreciated!
contact.php:
<?php
if(isset( $_POST['fname']))
$name = $_POST['fname'];
if(isset( $_POST['email']))
$email = $_POST['email'];
if(isset( $_POST['subject']))
$subject = $_POST['subject'];
if(isset( $_POST['message']))
$message = $_POST['message'];
$content="Name: $name \n Email: $email \n Subject: $subject \n Message: $message";
$recipient = "myemail#domain.com";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $content, $mailheader) or die("Error!");
echo "Email sent!";
?>
index.html
<div class="contactcontainer">
<form action="contact.php" method="POST" id="contact-form">
<label for="fname">Full Name</label>
<input type="text" id="fname" name="fname" placeholder="Your name" required>
<label for="email">E-mail</label>
<input type="text" id="email" name="email" placeholder="E-mail" required>
<label for="email">Subject</label>
<input type="text" id="subject" name="subject" placeholder="Subject" required>
<label for="subject">Message</label>
<textarea id="message" name="message" placeholder="Your message" style="height:200px" required></textarea>
<button class="btn btn-outline-light text-uppercase" onclick="document.getElementById('contact-form').submit();" type="submit" name="submit" id="submit">submit</button>
<div class="alert-msg"></div>
</form>
</div>
Page After clicking submit:
Email sent! in blank page with URL
For the alert-msg to work the PHP needs to be above the html.
There's a lot wrong with this as you have no sanitization of post data leaving you wide open to attack.
Here are some suggestions for the PHP;
<?php
// You should santize input, examples below;
// $data = strip_tags($data); - this removes tags like <a>
// $data = trim($data); - this removes whitespace
// New code
$status = ""; // First set the alert-msg to a null/empty value so it's not shown before the form is submitted.
// Listen for when the submit button is clicked
if (isset($_POST['submit'])) {
$name = $_POST['fname'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
// Now we have the post data and it has been sanitized we can check to make sure it isn't null/empty
if ($name == "" || $email == "" || $subject == "" || $message == "") {
// If any of these values are empty send the alert that email hasn't been sent
$status = "Email not sent!";
// If all values have some data, continue...
} else {
$content = "Name: $name \n Email: $email \n Subject: $subject \n Message: $message";
$recipient = "myemail#domain.com";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $content, $mailheader) or die("Error!");
// Set alert-msg value for success
$status = "Email sent!";
}
}
?>
Change the HTML form by removing the action and include your PHP script above the HTML on the same page. With the submit button, remove the onclick part as you are using PHP to send the form directly and add a php value to the alert-msg div;
New HTML
<form method="POST" id="contact-form">
<button class="btn btn-outline-light text-uppercase" type="submit" name="submit" id="submit">submit</button>
</form>
<div class="alert-msg"><?php echo $status; ?></div>
I omitted the rest of the HTML you posted as it's okay.

PHP question. I get email, but it doesn't have user input from website

The PHP code sends me an email from the server, but the user input is empty or "blank". I only receive: "From: \ Email: \ Subject: \ Message: " and that's it.
How do I fix my PHP and/or HTML code to receive user input from the form? Here is my existing HTML and PHP code that doesn't send me any "user input" from the form.
PHP
HTML
Thanks to anyone who can help!!
Consider this is your form
<form action="yourpage.php" method="post">
<input type="text" name="name" placeholder="enter name" required>
<input type="submit" name="submit" placeholder="enter name">
Make sure that <form action="" is linking to correct page and method="post" should be there.
If form is there on current page i.e. in my case its yourpage.php then leave form action blank. Also i recommend adding required in end of input field e.g.
<input type="text" name="name" placeholder="enter your name" required>
as it forces user to write some value there so you dont get a empty value
Next will be your php file, just add a code in first line
if(isset($_POST['submit'])) {
above code verifies that user clicks on submit button so overall code will look like
<?php
if(isset($_POST['submit'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phoneT'];
$call = $_POST['call'];
$website = $_POST['website'];
$priority = $_POST['priority'];
$type = $_POST[itypel];
$message = $_POSTUmessagefl;
$formcontent = " From:$name \n Phone: $phone \n CallBack: $call \n Website: $website \n Priority: $priority \n Type: $type \n Message: $message";
$recipient = "YOUREMAIL#HERE.COM";
$subject - "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!" . " -" . "<a href=iform.htmll
style='text-decoration:none;color:#ff0099;'> Return
Home</a>";
}
?>

CSS form for emailing the data not working [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am working on a website and the form in the code is not working up. What can I do to make it work and put it in action. Here is the form code....
<div class="form"; action="mail.php"; method="post">
<input class="input-text" type="text" name="name" value="Your Name *" onFocus="if(this.value==this.defaultValue)this.value='';" onBlur="if(this.value=='')this.value=this.defaultValue;">
<input class="input-text" type="text" name="email" value="Your E- mail *" onFocus="if(this.value==this.defaultValue)this.value='';" onBlur="if(this.value=='')this.value=this.defaultValue;">
<textarea class="input-text text-area" name="message" cols="0" rows="0" onFocus="if(this.value==this.defaultValue)this.value='';" onBlur="if(this.value=='')this.value=this.defaultValue;">Your Message *</textarea>
<input class="input-btn" type="submit" value="send message">
</div>
Here is the PHP code I am using to fetch data from form.
<html>
<body>
<?php
$errors = '';
$myemail = 'myemail#example.com';
if(empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['message']))
{
$errors .= "\n Error: all fields are required";
}
$name = $_POST['name'];
$email_address = $_POST['email'];
$message = $_POST['message'];
if (!preg_match(
"/ ^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i",
$email_address))
{
$errors .= "\n Error: Invalid email address";
}
if( empty($errors))
{
$to = '$myemail';
$email_subject = "Contact form submission: $name";
$email_body = "You have received a new message. ".
" Here are the details:\n Name: $name \n ".
"Email: $email_address\n Message \n $message";
$headers = "From: $myemail\n";
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
//redirect to the 'thank you' page
header('Location: contact-form-thank-you.html');
}
?>
The problem is that after entering data in the form, it is not proceeding any further.
Change the markup so that it is a <form> not a <div> and get rid of the semicolons.
<form class="form" action="mail.php" method="post">
...
</form>

what should I use to create a web contact form? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
Hi guys this is my first question on stackoverflow.
I'm an amateur web designer designing a mostly static website..I need to create a contact form for user queries.What's the best approach for this task?
1)php's mailto function?
2)form data stored in a text file or spreadsheet?
3)database-connected(not preferred)
Any help would be really appreciated!!
To create a simple contact us form in php,you dont need to create any database. You have to use mail() function in PHP
You can refer following links to built simple contact us form :-
http://www.phpeasystep.com/phptu/8.html
http://www.freecontactform.com/email_form.php
OR
page1.php
<h2>Your Title</h2>
 
<form action="receiving.php" method="POST">
 
Name:<br><input type="text" name="name" size="40" /><br><br>
 
Email:<br><input type="text" name="email" size="40" /><br><br>
 
Phone:<br><input type="text" name="phone" size="40"><br><br>
 
Message:<br><textarea name="message" rows="3" cols="31" > </textarea><br><br>
 
<input type="submit" name="submit" value="Submit" />
<br><br>
 
</form>
receiving.php
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
if(isset($_POST['submit']))
{
$from_add = "contactform#yourwebsite.com";
$to_add = "yourname#yourwebsite.com";
$subject = "Your Subject Name";
$message = "Name:$name \n Email: $email \n Phone: $phone \n
Message: $message";
$headers = "From: $from_add \r\n";
$headers .= "Reply-To: $from_add \r\n";
$headers .= "Return-Path: $from_add\r\n";
$headers .= "X-Mailer: PHP \r\n";
if(mail($to_add,$subject,$message,$headers))
{
$msg = "Mail sent";
}
}
print "<p> Thank you $name for your message,
we will be in contact shortly. Click here
to continue </p>" ;
?>
NOTE :- You cannot send mail from localhost, configure some other smtp at localhost eg : google,yahoo...
Sample code of contact form using mail function
<?php
if (isset($_POST['action'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
if ($name == "" || $email == "" || $message == "") {
echo "All fields are required, please fill the form again.";
} else {
$from = "From: $name<$email>\r\nReturn-path: $email";
$subject = "Message sent using your contact form";
mail("youremail#yoursite.com", $subject, $message, $from);
echo "Email sent!";
}
}
?>
<form action="" method="POST">
Your name:<br>
<input name="name" type="text" /><br>
Your email:<br>
<input name="email" type="text"/><br>
Your message:<br>
<textarea name="message" rows="7" cols="30"></textarea><br>
<input type="submit" name="action" value="Send email"/>
</form>
Simply build a html-form and send the data to a php-file. Database is not needed at all, unless you want to store all messages or IP.
A simple search will get you a lot of results.
In the phpfile you have two options: Use php's mail() as is, or try PHPmailer. The latter is a bit more complecated, but ends up less as spam, it sets headers and everything for you.
Especially when you mail from multiple pointer on your website, I recommend PHPmailer.
In reply to one of your comments:
All mailing happends on the server. PHP is a serverside language, all actions take place on the server, not on the users computer. For that reason, your code should always be on a server, or local with WAMP (or LAMP)

empty $_POST in PHP mail()

Some help with the following would be greatly appreciated! I've been searching the web and stackoverflow for hours. There is this problem with my php mail function.
There's a form on my website (the site uses wordpress) with two text fields, name and phone number. There's also a hidden field in the form that displays the current url, so we can see on what page the form was filled in. This information is also stored in a php session.
<?php session_start();
$_SESSION['url'] = $_SERVER['REQUEST_URI'];
?>
<form name="callback" action="callme.php" method="post" onsubmit="return(validate());">
<label>Name:</label><input type="text" name="name" size=15 />
<label>Tel:</label><input class="sky" type="text" name="tel" size=15 />
<div id="afterfive"><p>Call after five<input type="checkbox" name="afterfive" value="Call back after five"></p></div>
<input type="hidden" name="url" value="<?php echo $_SESSION['url']; ?>">
<input type="submit" class="classname" value="Call me back!" title="Call me back!" />
</form>
<script>
function validate()
{
if( document.callme.name.value == "" )
{
alert( "Please fill in your name" );
document.callme.name.focus() ;
return false;
}
if( document.callme.tel.value == "" )
{
alert( "Please fill in your phone number" );
document.callme.tel.focus() ;
return false;
}
}
</script>
The following php code is callme.php:
<?php session_start();
$name = $_POST['name'];
$tel = $_POST['tel'];
$afterfive = $_POST['afterfive'];
$url = $_POST['url'];
$to = "me#myemail.com";
$subject = "Please call back $name";
$message .= "Hi, the following person would like to be called back: \n";
$message .= "Name: $name \n";
$message .= "Phonenumber: $tel \n";
$message .= "$afterfive \n";
$message .= "This message was send from this page: $url \n";
$headers = "From: meagain#myemail.com" . "\r\n";
$headers .= "BCC: meagain#myemail.com" . "\r\n";
if(mail($to, $subject, $message, $headers)){
$_SESSION['name'] = $_POST['name'];
$_SESSION['tel'] = $_POST['tel'];
header("Location: http://www.mywebsite.thankyou");
}
?>
After submitting the form, the visitor is redirected to our thankyou page and given the opportunity the fill in additional information using a second form. The information previously stored in the php session (form fields name, tel and url) are added in hidden form fields.
This all works fine most of the time, but sometimes we receive e-mails with all or some fields empty. Of course this could be users with javascript disabled or google bots that submit blank forms, but the weird thing is that sometimes even the url field is empty (the form is not visible on our homepage). Shouldn't $_SERVER['REQUEST_URI'] always still work?
I was thinking about adding php form validation, but I'm not sure this will solve the problem. Could this have something to do with the hyper cache plug-in for wordpress? Or could it be related to the php session?
"but sometimes we receive e-mails with all or some fields empty"
You should be using a server-side method instead of JS such as
if(empty($_POST['name'])){ die("You need to enter your name.");
(JS can always be disabled by the user, one probable cause for empty emails/fields)
and that will ensure that the fields you wish to be NOT empty, be filled. In conjunction with what Andrewsi mentioned, use if(isset($_POST['submit'])){ at the top of your handler, and name your submit button to name="submit" that way the callme.php if accessed directly, won't process the information without the submit button being clicked.
For example:
Note: There are many other ways to achieve this, but this is a basic yet effective method.
Naming your submit button such as:
<input type="submit" name="submit" value="Submit">
in your case, it would be:
<input type="submit" name="submit" class="classname" value="Call me back!" title="Call me back!" />
PHP handler
<?php
session_start();
if(isset($_POST['submit'])){
if(empty($_POST['name'])){ die("You need to enter your name."); }
if(empty($_POST['tel'])){ die("You need to enter your telephone number."); }
if(empty($_POST['afterfive'])){ die("You need to fill this field."); }
if(empty($_POST['url'])){ die("You need to fill this field."); }
$name = $_POST['name'];
$tel = $_POST['tel'];
$afterfive = $_POST['afterfive'];
$url = $_POST['url'];
$to = "me#myemail.com";
$subject = "Please call back $name";
$message .= "Hi, the following person would like to be called back: \n";
$message .= "Name: $name \n";
$message .= "Phonenumber: $tel \n";
$message .= "$afterfive \n";
$message .= "This message was send from this page: $url \n";
$headers = "From: meagain#myemail.com" . "\r\n";
$headers .= "BCC: meagain#myemail.com" . "\r\n";
if(mail($to, $subject, $message, $headers)){
$_SESSION['name'] = $_POST['name'];
$_SESSION['tel'] = $_POST['tel'];
header("Location: http://www.mywebsite.thankyou");
}
}
// You could use this at end also to show a message
// if callme.php is accessed directly.
// else {echo "You cannot do that from here.";exit;}
?>
Your javascript refers to document.callme, but there is nothing in your code with that name.

Categories