Send contact form data to email - php

My goal is to make a contact form, where the user would type their name, email and message. The moment they press "Send", me, as the admin, would receive an email with all the information that was typed in before. Right now the email is sent but its empty, for example:
Reply to:
User name:
User email
User message
Also, when I click on "Send message" it gives me the same form but without CSS, there must be a code mistake which I don't know what it is.
// Form contactos.php
<div class="col-md-7 mb-5 site-animate">
<form action="index.php?cmd=contform" method="post">
<div class="form-group">
<label for="name" class="sr-only">Name</label>
<input type="text" class="form-control" id="NomeM" placeholder="Nome">
</div>
<div class="form-group">
<label for="email" class="sr-only">Email</label>
<input type="text" class="form-control" id="EmailMen" placeholder="Email">
</div>
<div class="form-group">
<label for="message" class="sr-only">Messagem</label>
<textarea name="message" id="Mensagem" cols="30" rows="10" class="form-control" placeholder="Escreva a sua mensagem"></textarea>
</div>
<div class="form-group">
<input type="submit" name="submit" class="btn btn-primary btn-lg" value="Enviar Mensagem">
</div>
</form>
</div>
//File contform.php
<?php
$sql="select * from Mensagem ";
$res=$lig->query($sql);
$lin = $res->fetch_array();
$NomeM = $_POST['NomeM'];
$Email = $_POST['EmailMen'];
$Mensagem = $_POST['Mensagem'];
$email_from = 'admin03#happygreen.pt';
$email_subject = "New Form Submission";
$email_body = "User Name:" .$lin['NomeM']."\n".
"User Email:".$lin['EmailMen']."\n".
"User Message:".$lin['Mensagem']."\n";
$to = "filipajoao1933#gmail.com";
$headers = "De: $email_from \r\n";
$headers .= "Responda a: ".$lin['EmailMen']."\r\n";
mail($to,$email_subject,$email_body,$headers);
header("Location: Contactos/contactos.php");
?>

Form fields (input, select, textarea) need a name attribute you can reference in PHP, yours only have ids. Also, the for attribute in labels reference the id of a form field:
// Form contactos.php
<div class="col-md-7 mb-5 site-animate">
<form action="index.php?cmd=contform" method="post">
<div class="form-group">
<label for="NomeM" class="sr-only">Name</label>
<input type="text" class="form-control" name="NomeM" id="NomeM" placeholder="Nome">
</div>
<div class="form-group">
<label for="EmailMen" class="sr-only">Email</label>
<input type="text" class="form-control" id="EmailMen" name="EmailMen" placeholder="Email">
</div>
<div class="form-group">
<label for="Mensagem" class="sr-only">Messagem</label>
<textarea name="Mensagem" id="Mensagem" cols="30" rows="10" class="form-control" placeholder="Escreva a sua mensagem"></textarea>
</div>
<div class="form-group">
<input type="submit" name="submit" class="btn btn-primary btn-lg" value="Enviar Mensagem">
</div>
</form>
</div>
And your $email_body should be:
$email_body = "User Name:" .$NomeM."\n".
"User Email:".$EmailMen."\n".
"User Message:".$Mensagem."\n";

Related

Send Email submission from Multistep Form

I'm trying to create a multistep form that sends an email to me every time the form has been submitted. The form fully works except it won't send me the email. The code I used does work though for another test I did where the form is not a multistep form.
Here's the code I use:
<form <?php
error_reporting(E_ALL);
if(isset($_POST['submit'])){
$name=$_POST['name'];
$email=$_POST['email'];
$phone=$_POST['phone'];
$message=$_POST['message'];
$to='email#email.com'; // Receiver Email ID, Replace with your email ID
$subject='Form Submission';
$message="Name :".$name."\n"."Email :".$email."\n"."Phone :".$phone."\n"."Wrote the following :"."\n\n".$message;
$headers="From: ".$email;
if(mail($to, $subject, $message, $headers)){
echo "<h1>Sent Successfully! Thank you"." ".$name.", We will contact you shortly!</h1>";
}
else{
echo "Something went wrong!";
}
}
?> method="post" name="form" class="form-box">
<div class="step step-1 active">
<div class="form-group">
<label for="name">Name</label>
<input type="text" id="name" name="name">
</div><br>
<button type="button" class="next-btn">Next</button>
</div>
<div class="step step-2">
<div class="form-group">
<label for="email">Email</label>
<input type="email" id="email" name="email">
</div><br>
<div class="form-group">
<label for="phone">Phone Number</label>
<input type="number" id="phone" name="phone">
</div>
<button type="button" class="prev-btn">Previous</button>
<button type="button" class="next-btn">Next</button>
</div>
<div class="step step-3">
<div class="form-group">
<label for="message">Message</label>
<textarea name="message" class="message-box" placeholder="Enter Your Message Here..."></textarea>
<!-- <input type="text" id="message" name="message">-->
</div>
<button type="button" class="prev-btn">Previous</button>
<button type="submit" name="submit" value="Send" class="submit-btn">Submit</button>
</div>
</form>
Thanks in advance and I hope someone can help me figure it out.

contact form not getting submitted

On clicking the submit button it is showing submit is not set after the if condition. I guess this message is showing because the form is not getting submitted. I don't know where I'm going wrong kindly help me fix this issue. I'm herewith attaching a part of my contact.php code and mail.php code. Thank you
contact.php
<form id="contact-form" method="POST" action="mail.php">
<div class="row">
<div class="col-md-4">
<div class="form-group">
<label>Name</label>
<input class="form-control" name="name" id="name" placeholder="" type="text" required>
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label>Email</label>
<input class="form-control" name="email" id="email" placeholder="" type="email" required>
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label>Subject</label>
<input class="form-control" name="subject" id="subject" placeholder="" required>
</div>
</div>
</div>
<div class="form-group">
<label>Message</label>
<textarea class="form-control" name="message" id="message" placeholder="" rows="10" required></textarea>
</div>
<div class="text-right"><br>
<input class="btn btn-primary solid blank button" id="btn" type="submit" value="submit" name="submit">
</div>
</form>
mail.php
<?php
if (isset($_POST['email'])){
echo "submit is set to {$_POST['submit']} and now we send the email<br>";
$to = "abc#email.com";
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$message .="\r\n from: $email";
if(mail ($to, $subject, $name, $message)){
echo "Enquiry sent successfully!<br>";
}
else
{
echo "Mail was not sent. Please try again later<br>";
}
} else{
echo"submit is not set<br>";
}
echo "after the if condition";
header('Location: https://imatrixautomation.com/contact.php');
exit;
?>
nothing wrong with your code, i guess you doing a direct request to mail.php
try this code, maybe this help your problem
<?php
if($_POST){ // add condition if http request is POST then process your POST request
if (isset($_POST['email'])){
echo "submit is set to {$_POST['submit']} and now we send the email<br>";
$to = "abc#email.com";
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$message .="\r\n from: $email";
if(mail ($to, $subject, $name, $message)){
echo "Enquiry sent successfully!<br>";
}
else
{
echo "Mail was not sent. Please try again later<br>";
}
} else{
echo"submit is not set<br>";
}
echo "after the if condition";
exit; // if POST process has done, script will stop, and code above will not showing
}
?>
<form id="contact-form" method="POST" action="">
<div class="row">
<div class="col-md-4">
<div class="form-group">
<label>Name</label>
<input class="form-control" name="name" id="name" placeholder="" type="text" required>
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label>Email</label>
<input class="form-control" name="email" id="email" placeholder="" type="email" required>
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label>Subject</label>
<input class="form-control" name="subject" id="subject" placeholder="" required>
</div>
</div>
</div>
<div class="form-group">
<label>Message</label>
<textarea class="form-control" name="message" id="message" placeholder="" rows="10" required></textarea>
</div>
<div class="text-right"><br>
<input class="btn btn-primary solid blank button" id="btn" type="submit" value="submit" name="submit">
</div>
</form>

contact form submit not responding

I am having a problem with the contact form and when I am trying to submit the form, it's not responding and please help me to solve this issue.below is the code
HTML:
<form action="contact-form-handler.php" method="post"
class="contactForm">
<div class="row">
<div class="span4 form-group field">
<input type="text" name="name" placeholder="Your Name" data-
rule="minlen:4" data-msg="Please enter at least 4 chars" />
<div class="validation"></div>
</div>
<div class="span4 form-group">
<input type="email" name="email" placeholder="Your Email"
data-rule="email" data-msg="Please enter a valid email" />
<div class="validation"></div>
</div>
<div class="span8 form-group">
<textarea name="message" rows="5" data-rule="required" data-
msg="Please write something for us" placeholder="Message">
</textarea>
<div class="validation"></div>
<div class="text-center">
<button class="btn btn-success" type="submit"
value="sendmessage">Send message</button>
Please find the below php code for the submit form and please give me the solution on how i can solve this issue. Thanks in advance.
if (isset($_POST['submit'])) {
$name=$_POST['name'];
$email=$_POST['email'];
// $subject=$_POST['subject'];
$msg=$_POST['msg'];
}
$to='contact#****.my';
$subject='Contact Form';
$message="Name: " .$name."\n"."email: " .$email."\n"."Subject: "
.$subject."\n". "Wrote the following: "."\n\n".$msg;
$headers="From:".$email;
if (mail($to, $subject, $message, $headers)) {
echo "<h1>Your message has been sent. Thank you!"."".$name.", We Will
Contact you shortly</h1>";
}
else {
echo "Something went wrong!";
}
You forgot to close your form and didn't gave your submit button a name so your php can't work
<form action="contact-form-handler.php" method="post"
class="contactForm">
<div class="row">
<div class="span4 form-group field">
<input type="text" name="name" placeholder="Your Name" data-
rule="minlen:4" data-msg="Please enter at least 4 chars" />
<div class="validation"></div>
</div>
<div class="span4 form-group">
<input type="email" name="email" placeholder="Your Email"
data-rule="email" data-msg="Please enter a valid email" />
<div class="validation"></div>
</div>
<div class="span8 form-group">
<textarea name="message" rows="5" data-rule="required" data-
msg="Please write something for us" placeholder="Message">
</textarea>
<div class="validation"></div>
<div class="text-center">
<button class="btn btn-success" type="submit" name="submit"
value="sendmessage">Send message</button>
</form>
and you forgot to close this sentece change it from
$message="Name: " .$name."\n"."email: " .$email."\n"."Subject: "
to
$message="Name: " .$name."\n"."email: " .$email."\n"."Subject: " ;
$msg=$_POST['msg'];
must be
$msg=$_POST['message'];
and
.$subject."\n". "Wrote the following: "."\n\n".$msg;
must be
$subject."\n". "Wrote the following: "."\n\n".$msg;

PHP contact form not working right

I've been working on a contact form. I have added used the POST method to send it to a set email address. It doesn't seem to be working though. It just runs and stops as if the code is broken. The HTML and PHP are below.
<form action="contact-form.php" method="post" id="contact-form" name="contact-form">
<div class="form-group">
<label for="name">Your name</label> <input class=
"form-control" id="name" name="name" type="text">
</div>
<div class="form-group">
<label for="email">Email address</label>
<input class="form-control" id="email" name="email"
type="email">
</div>
<div class="form-group">
<label for="phone">Phone</label> <input class=
"form-control" id="phone" name="phone" type="text">
</div>
<div class="form-group">
<label for="message">Your message</label>
<textarea class="form-control" id="message" name=
"message" rows="6">
</textarea>
</div>
<div class="submit">
<input class="button button-small" type="submit"
value="Send">
</div>
</form>
<?php
if(isset($_POST['submit'])) {
$to = "gfrazer#hotmail.co.uk";
$from = $_POST['email'];
$name = $_POST['name'];
$phone = $_POST['phone'];
$message = $name . " " . " wrote the following: " . "\n\n" . $_POST['message'];
$headers = "From:" . $from;
$headers2 = "From:" . $to;
mail($to,$subject,$message,$headers);
mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender
header('Location: http://www.google.co.uk');
}
?>
You need to add a name to your submit button. From your snippit, you have no $_POST['submit']:
<form action="contact-form.php" method="post" id="contact-form" name="contact-form">
<div class="form-group">
<label for="email">Email address</label>
<input class="form-control" id="email" name="email" type="email">
</div>
<div class="form-group">
<label for="phone">Phone</label> <input class="form-control" id="phone" name="phone" type="text">
</div>
<div class="form-group">
<label for="message">Your message</label>
<textarea class="form-control" id="message" name="message" rows="6"></textarea>
</div>
<div class="submit">
<!-- ADD name="submit" -->
<input name="submit" class="button button-small" type="submit" value="Send" />
</div>
</form>

php form not sending submission data. sending email and processing form still however

I Have two forms on my site. One works fine and the other sends email with email_from: etc but doesn't capture any of the form data.
Wondering what it may be. I can post the form that is working along with it's html too if that would help debug. Very much a novice and built form using stack overflow/other sites.
Coffee
<div class="form-group">
<label class="col-sm-3 control-label">Quantity</label>
<div class="col-sm-4">
<input type="text" name="quantity" class="form-control" placeholder="Quantity : " required>
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">Name</label>
<div class="col-sm-6">
<input type="text" name="name" class="form-control" placeholder="Name : " required>
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">Email address</label>
<div class="col-sm-6">
<input type="email" name="email" class="form-control" placeholder="Email address : " required>
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">Shipping Address</label>
<div class="col-sm-6">
<textarea class="form-control" name="shipping_address" rows="8" placeholder="Shipping Address : " required></textarea>
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">Payment Method</label>
<div class="col-sm-6">
<select class="form-control" required>
<option value="Paypal">Paypal</option>
</select>
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">Notes</label>
<div class="col-sm-6">
<textarea class="form-control" name="notes" rows="8" placeholder="Notes : "></textarea>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-3 col-sm-10">
<button type="submit" class="btn btn-black">Order Now</button></a>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
And here is the php
<?php
if(isset($_POST ['submit']))
{
$coffee = ($_POST['coffee']));
$quantity = ($_POST['quantity']));
$name = ($_POST['name']));
$email = ($_POST['email']));
$shipping_address = ($_POST['shipping_address']));
$notes = ($_POST['notes']));
}
$email_from ='paradigmcoffee#gmail.com';
$email_subject="New Order Submission";
$email_body ="You have received a new message from user $name.\n".
"Email_address:$email\n".
"Coffee: $coffee\n".
"Quantity: $quantity\n".
"Shipping_Address: $shipping_address\n".
"Notes: $notes\n".
$to ="paradigmcoffee#gmail.com";
$headers = "From: $email \r\n";
mail($to,$email_from,$email_subject,$email_body,$headers);
header("Location: http://www.paradigmcoffee.co/order_thanks.html");
?>
From what you have supplied, likely the reason you can not get data is because you are not sending anything called submit. Try naming your button:
<!-- name="submit" added -->
<button name="submit" type="submit" class="btn btn-black">Order Now</button>
You can do this or make a hidden field:
<input type="hidden" name="submit" value="1" />

Categories