I have created a contact form using HTML and PHP.
Right now when a user submit the contact form he redirect to a new page with the echo message, instead, I want this message to appears in a small popup window.
HTML Code:
<form action="/assets/bootstrap/php/emailscript.php" method="post">
<input type="text" placeholder="Name" name="Name" oninvalid="setCustomValidity('Please enter your name')"
onchange="try{setCustomValidity('')}catch(e){}" required >
<input type="text" placeholder="Company" name="Company" oninvalid="setCustomValidity('Please enter your Company name')"
onchange="try{setCustomValidity('')}catch(e){}" required >
<input type="text" placeholder="Email" name="Email" required pattern="[a-z0-9._%+-]+#[a-z0-9.-]+\.[a-z]{2,3}$" oninvalid="setCustomValidity('Please enter a valid e-mail address')"
onchange="try{setCustomValidity('')}catch(e){}" >
<input type="text" placeholder="Subject" name="Subject" oninvalid="setCustomValidity('Please enter a Subject')"
onchange="try{setCustomValidity('')}catch(e){}" required >
<textarea rows="5" placeholder="Message" name="Message" oninvalid="setCustomValidity('Please Write your Message')"
onchange="try{setCustomValidity('')}catch(e){}" required ></textarea>
<button type="submit" class="btn btn-primary"><i class="fa fa-paper-plane"></i> Send</button>
</form>
</div>
</div>
</div>
PHP Code:
<?php
if(isset($_POST['Name']))
{
$name=$_POST['Name'] ;
$company=$_POST['Company'] ;
$from=$_POST['Email'] ;
$message=$_POST['Message'] ;
$to="contact#website.com" ;
$subject="Website contact form";
$headers .= 'From: '.$name.' | '.$company.' | '.$from.' ' . "\r\n";
mail ($to, $subject, $message, $headers ) ;
echo "Your Email has bean sent.";
}
?>
Thanks
Related
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 5 years ago.
HTML Codes
<h3>online consultation</h3>
<h5>We have provided contact information down below. You can contact us either by mobile, via email or via contact form. We will at once contact you to discuss your problems! If you have special requirments a consultation can be decided.</h5>
</div>
<div class="col-md-7 col-lg-offset-3 contact-grid-right">
<form method="post" action="mail.php">
<input type="text" placeholder="Your Name" name="name" id="name" required="required">
<input type="text" placeholder="Email" name="Email" id="Email" required="required">
<input type="text" placeholder="Contact No" name="contact" id="contact" required="required">
<input type="text" placeholder="Subject" name="subject" id="subject" required="required">
<textarea rows="2" cols="70" type="text" placeholder="MESSAGE" name="message" id="message" required="required">
</textarea>
<button type="submit" class="slide-btn"> Send your message <i class="fa fa-paper-plane" aria-hidden="true"></i> <br></button>
</form>
</div>
PHP Script
<?php
$n=$_POST['name'];
$mail=$_POST['email'];
$ph=$_POST['contact'];
$ln=$_POST['subject'];
$msg=$_POST['message'];
$to="email#yahoo.com";
$sub="message";
$body="
<html>
<body>
<p><b></b></p><h4></h4>
<table border='0'>
<tr>
<b>Name : </b> $n <br><br>
<b>Email : </b> $mail <br><br>
<b>Contact :</b> $ph <br><br>
<b>Subject :</b> $ln <br><br>
<hr>
<b>Contact Form Message:</b> <br><br>$msg
</tr>
</table>
</body>
</html>";
$headers = "MIME-Version: 1.0". "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8". "\r\n";
$headers .= 'From:'.$mail . "\r\n";
//$headers .= 'Cc: email#yahoo.com.au'; "\r\n";
//$headers .= 'Bcc: '; "\r\n";
mail($to,$sub,$body,$headers);
//echo "<script>alert('Message Send Sucessfully')</script>";
echo "<script>window.location.href='index.html'</script>";
?>
here the solution change the $mail=$_POST['email']; in your php script to $mail=$_POST['Email'];
For keeping your code standard replace the email html input for:
<input type="text" placeholder="Email" name="email" id="email" required="required">
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 5 years ago.
I haven't written code in years, so I am going off old code from a project that is over 5 years old, so I am not surprised that it doesn't work; I would like some pointers on how to make it work, please.
Here is what I have in my HTML email form --
<form action="fydcontact.php" method="post">
<div class="form-group">
<!--<label for="contact_name">Name:</label>-->
<input type="text" id="contact_name" class="form-control" placeholder="Name" />
</div>
<div class="form-group">
<!--<label for="contact_email">Email:</label>-->
<input type="text" id="contact_email" class="form-control" placeholder="Email Address" />
</div>
<div class="form-group">
<!--<label for="contact_message">Message:</label>-->
<textarea id="contact_message" class="form-control" rows="9" placeholder="Write a message"></textarea>
</div>
<button type="submit" class="btn btn-primary">Send</button>
</form>
and here is what my PHP looks like --
<?php
if(isset($_POST['send'])) {
// Prepare the email
$to = ''foryourdayformals#gmail.com ';
$name = $_POST['contact_name'];
$mail_from = $_POST['contact_email'];
$subject = 'Message sent from website';
$message = $_POST['contact_message'];
$header = "From: $name <$mail_from>";
// Send it
$sent = mail($to, $subject, $message, $header);
if($sent) {
echo 'Your message has been sent successfully! Return to For Your Day, LLC Website';
} else {
echo 'Sorry, your message could not send. Please use direct email link on Contact Us page (below the map). Return to For Your Day, LLC Website';
}
}
?>
Any help is GREATLY appreciated! Thanks!
update
$to = ''foryourdayformals#gmail.com ';
to
$to = 'foryourdayformals#gmail.com';
and need to add name attribute for form . This update your form
<form action="fydcontact.php" method="post">
<div class="form-group">
<!--<label for="contact_name">Name:</label>-->
<input type="text" id="contact_name" name="contact_name" class="form-control" placeholder="Name" />
</div>
<div class="form-group">
<!--<label for="contact_email">Email:</label>-->
<input type="text" id="contact_email" name="contact_email" class="form-control" placeholder="Email Address" />
</div>
<div class="form-group">
<!--<label for="contact_message">Message:</label>-->
<textarea id="contact_message" name="contact_message" class="form-control" rows="9" placeholder="Write a message"></textarea>
</div>
<button type="submit" class="btn btn-primary" name ="send">Send</button>
</form>
As stated in the title, my webform is not returning the user information in the email, although the field labels are being returned.
I think this is an issue where the HTML is not structured to relate back to the php field name, but I am not sure how to format that.
Can anyone set me on the path?
This is the HTML:
<form id="main-contact-form" class="contact-form" name="contact-form" method="post" action="sendemail.php">
<div class="row-fluid">
<div class="span5">
<label>First Name</label>
<input type="text" class="input-block-level" required="required" placeholder="Your First Name">
<label>Last Name</label>
<input type="text" class="input-block-level" required="required" placeholder="Your Last Name">
<label>Email Address</label>
<input type="text" class="input-block-level" required="required" placeholder="Your email address">
</div>
<div class="span7">
<label>Message</label>
<textarea name="message" id="message" required class="input-block-level" rows="8"></textarea>
</div>
</div>
<button type="submit" class="btn btn-primary btn-large pull-right">Send Message</button>
</form>
And this is the php:
<?php
header('Content-type: application/json');
$status = array(
'type'=>'success',
'message'=>'Email sent!'
);
$name = #trim(stripslashes($_POST['name']));
$email = #trim(stripslashes($_POST['email']));
$subject = #trim(stripslashes($_POST['subject']));
$message = #trim(stripslashes($_POST['message']));
$email_from = $email;
$email_to = 'Example#email.com';
$body = 'Name: ' . $name . "\n\n" . 'Email: ' . $email . "\n\n" . 'Subject: ' . $subject . "\n\n" . 'Message: ' . $message;
$success = #mail($email_to, $subject, $body, 'From: <'.$email_from.'>');
echo json_encode($status);
die;
Firstly, your inputs does not have name attributes:
<input type="text" class="input-block-level" required="required" name="first_name" placeholder="Your First Name">
<input type="text" class="input-block-level" required="required" name="last_name" placeholder="Your Last Name">
<input type="text" class="input-block-level" required="required" name="email" placeholder="Your email address">
Secondly, you need to use <input> instead of <button>. Otherwise, your form will not be submitted.
<input type="submit" class="btn btn-primary btn-large pull-right">Send Message</input>
Thirdly, you need to check if the form is submitted by using isset() function:
// will return true if the form is submitted
if(isset($_POST['submit']){
header('Content-type: application/json');
// other parts of your code
}
Lastly, you shouldn't suppress the error messages by removing the # as they are very useful in helping you to debug your errors, especially when you have a syntax error.
You need to give your inputs a name attribute, such as:
<input type="text" name="name" class="input-block-level" required="required" placeholder="Your First Name" />
Then you will be able to access the $_POST data as you currently are. Check out the name attribute docs
I'm not very good at PHP (more of a front-end developer) and I'm doing a website for a client. I got everything working up to now except for one thing. When the user fills out the contact form and submits it successfully i want to display a message that tells them the email was sent. I already have the div set up I just don't know who to execute it with PHP.
<div class="sent">Email has been sent.</div>
<form action="" method="POST" class="contact-form">
<img class="close" src="images/close-icon.png">
<div class="top-title">Contact Us</div>
<input type="hidden" name="action" value="submit">
<div class="input-title">Name*</div>
<input class="input" name="name" type="text" value="" size="30" required><br>
<div class="input-title">Phone Number*</div>
<input class="input" name="phone" type="text" value="" size="30" required><br>
<div class="input-title">Email Address*</div>
<input class="input" name="email" type="text" value="" size="30" required><br>
<div class="input-title">How did you hear about us?</div>
<input class="input" name="company" type="text" value="" size="30"/><br>
<div class="input-title">Let us know how we can help you</div>
<textarea class="textarea" name="message" rows="7" cols="30"></textarea><br>
<div class="required">*Fill is required</div>
<input class="submit-button" type="submit" value="Send Email"/>
</form>
<?php
{
$name=$_POST['name'];
$email=$_POST['email'];
$phone=$_POST['phone'];
$company=$_POST['company'];
$message= "\r\nName: " . $_POST['name'] ."\r\nPhone Number: ". $_POST['phone']."\r\nEmail: ". $_POST['email'] ."\r\nHow you know the company: ".$_POST['company'] ."\r\nReason for message: ".$_POST['message'];
$from="From: $name<$email>\r\nReturn-path: $email";
$subject="GS Website Message";
mail("name#email.com", $subject, $message, $from);
}
?>
Change: (and using a conditional statement)
mail("name#email.com", $subject, $message, $from);
to:
if(mail("name#email.com", $subject, $message, $from)){
$sent = "Message sent";
}
Then modify:
<div class="sent">Email has been sent.</div>
to read as:
<div class="sent"><?php if(!empty($sent)) { echo $sent; } ?></div>
Seeing you're new to PHP/forms, it's best to also check if your inputs are empty or not, otherwise you risk in either not getting mail, or receiving empty data.
http://php.net/manual/en/function.empty.php
Set inside a conditional statement also.
I.e.: using NOT empty: ! is the NOT operator in PHP.
if(!empty($_POST['var'])){...}
Edit:
The logic needs to be reversed.
First, place your PHP above your HTML form.
I also added a submit name attribute to your submit button, with a conditional statement and a ternary operator in the div.
<?php
if(isset($_POST['submit'])){
$name=$_POST['name'];
$email=$_POST['email'];
$phone=$_POST['phone'];
$company=$_POST['company'];
$message= "\r\nName: " . $_POST['name'] ."\r\nPhone Number: ". $_POST['phone']."\r\nEmail: ". $_POST['email'] ."\r\nHow you know the company: ".$_POST['company'] ."\r\nReason for message: ".$_POST['message'];
$from="From: $name<$email>\r\nReturn-path: $email";
$subject="GS Website Message";
if(mail("email#example.com", $subject, $message, $from)){
$sent = "Message sent";
}
}
?>
<div class="sent"><?php echo isset($sent) ? $sent : ''; ?></div>
<form action="" method="POST" class="contact-form">
<img class="close" src="images/close-icon.png">
<div class="top-title">Contact Us</div>
<input type="hidden" name="action" value="submit">
<div class="input-title">Name*</div>
<input class="input" name="name" type="text" value="" size="30" required><br>
<div class="input-title">Phone Number*</div>
<input class="input" name="phone" type="text" value="" size="30" required><br>
<div class="input-title">Email Address*</div>
<input class="input" name="email" type="text" value="" size="30" required><br>
<div class="input-title">How did you hear about us?</div>
<input class="input" name="company" type="text" value="" size="30"/><br>
<div class="input-title">Let us know how we can help you</div>
<textarea class="textarea" name="message" rows="7" cols="30"></textarea><br>
<div class="required">*Fill is required</div>
<input class="submit-button" type="submit" value="Send Email" name="submit"/>
</form>
$password=md5($_POST ['password']);
$password3 =md5($_POST ['password2']);
if($password != $password3)
{
$message = "Passwords do not match";
}
etc.
you need add
<form>
<div class="form-group">
<input type="password" name="password" id="password" class="form-control" placeholder="Password *" value="" required/>
</div>
<div class="form-group">
<div class="message"><?php if(!empty($message)) { echo $message; } ?></div>
<input type="password" name="password2" class="form-control" id="password2" placeholder="Confirm Password *" value="" required/>
</div>
</form>
i'm getting blank field returns in my inbox on only a portion on my form fields
like so:
From: whatevername
Email: fgsdfg#fakeysite.com
Message:
phone:
here is my code its probably something really dumb but its bugging me, so here we go.
HTML
<div class="row-item col-1_4">
<h3>Contact Form</h3>
<h4>Please fill out the form to get your free CD Replacement Kit</h4>
<!-- Success Message -->
<div class="form-message"></div>
<!-- Form -->
<form class="b-form b-contact-form" action="blast.php">
<div class="input-wrap m-full-width">
<i class="icon-user"></i>
Name
<input class="field-name" type="text" placeholder="Name (required)">
</div>
<div class="input-wrap m-full-width">
<i class="icon-phone"></i>
Phone
<input class="field-phone" type="text" placeholder="Phone">
</div>
<div class="input-wrap m-full-width">
<i class="icon-envelope"></i>
Email
<input class="field-email" type="text" placeholder="E-mail (required)">
</div>
<div class="input-wrap m-full-width">
<i class="icon-pencil"></i>
Message
<input class="field-comments" type="text" placeholder="Message">
</div>
<input class="btn-submit btn colored" type="submit" value="Send">
</form>
PHP
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$phone = $_POST['phone'];
$formcontent=" From: $name \n Email: $email \n Phone: $phone \n Message: $message";
$recipient = "csmith#legacybrokerage.com";
$subject = "UNLIMITED ANNUITY LEADS CD BLAST";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!";
try this, define name= 'something' to the input field
<input class="field-name" type="text" placeholder="Name (required)" name="name">
<input class="field-phone" type="text" placeholder="Phone" name="phone">
<input class="field-email" type="text" placeholder="E-mail (required)" name="email">
your input's dose not name attribute!
<input class="field-name" type="text" placeholder="Name (required)">
correct :
<input name="from" class="field-name" type="text" placeholder="Name (required)">