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.
Related
I have two contacts forms on my website and have set up some php to send the data to a specified email address. I want to use the same php on both forms which are on different web pages. It is working on one page but not the other. I haven't used php before so am wondering if I am missing something glaring obvious, any help much appreciated. Here is my php:
<?php
if(isset($_POST['submit'])){
$name=$_POST['name'];
$email=$_POST['emailaddress'];
$phone=$_POST['phone'];
$msg=$_POST['message'];
$to='example#hotmail.com';
$subject='Inquiry from website';
$message= "Name: ".$name."\n".
"Phone number: ".$phone."\n".
"Email address: ".$email."\n".
"The following inquiry has been made :"."\n\n".$msg;
if(mail($to, $subject, $message)){
echo "<h1>Message sent successfully. Thank you"." ".$name.", we will contact you as soon as possible.</h1>";
}
else{
echo "Something went wrong!";
}
}
?>
I have added my html into the post:
<form action="mail.php" method="POST">
<div class="form-group">
<input type="text" name="name" required class="form-control" placeholder="Name">
</div>
<div class="form-group">
<input type="email" name="emailaddress" class="form-control" placeholder="Email">
</div>
<div class="form-group">
<input type="text" name="phone" required class="form-control" placeholder="Phone number">
</div>
<div class="form-group">
<textarea name="message" id="message" required class="form-control" placeholder="Message"
cols="30" rows="5"></textarea>
</div>
<div class="form-group">
<input type="submit" name="submit" class="btn btn-primary px-5" value="Send Message">
</div>
</form>
where is your html
please send your html form
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";
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;
I have a quick question about my form. After submitting the form you will be directed to a page followed by the text that I put in. The problem is that I want this text to be displayed on the same page as where the form is on (named contact.html) I use two files, one is the Mail_handler.php and the other contact.html. I've tried multiple things to fix it, but for some reason, I have no success. I hope that you guys can help me out! Below you can find the HTML and PHP.
<form method="POST" action="mail_handler.php">
<div class="col-sm-7 slideanim">
<div class="row">
<div class="col-sm-6 form-group">
<input class="form-control" id="name" name="name" placeholder="Naam" type="text" required>
</div>
<div class="col-sm-6 form-group">
<input class="form-control" id="phone" name="phone" placeholder="Telefoonnummer" type="text" required>
</div>
<div class="col-sm-12 form-group">
<input class="form-control" id="email" name="email" placeholder="Email" type="email" required>
</div>
</div>
<textarea class="form-control" id="msg" name="msg" placeholder="Bericht" rows="5"></textarea><br>
<div class="row">
<div class="col-sm-12 form-group">
<button class="btn btn-default pull-right" id="submit" name="submit" type="submit">Verstuur</button>
</div>
</div>
</div>
</div>
</div>
</div>
</form>
<?php
if(isset($_POST['submit'])){
$name=$_POST['name'];
$email=$_POST['email'];
$phone=$_POST['phone'];
$msg=$_POST['msg'];
$to='infosyncdevelopment#gmail.com'; // Receiver Email ID, Replace with your email ID
$subject='Form Submission';
$message="Name :".$name."\n"."Phone :".$phone."\n"."Wrote the following :"."\n\n".$msg;
$headers="From: ".$email;
if(mail($to, $subject, $message, $headers)){
echo "<h1>Bedankt voor uw bericht!"." ".$name.", Wij nemen zo snel mogelijk contact met u op.</h1>";
}
else{
echo "Something went wrong!";
}
}
?>
the best way is to use ajax, but if you may not do that, you can do this little trick:
change contact.html into php script (maybe contact.php)
<?php
if(isset($_GET['msg'])) echo $_GET['msg'];
?>
<form method="POST" action="mail_handler.php">
<div class="col-sm-7 slideanim">
<div class="row">
<div class="col-sm-6 form-group">
<input class="form-control" id="name" name="name" placeholder="Naam" type="text" required>
</div>
<div class="col-sm-6 form-group">
<input class="form-control" id="phone" name="phone" placeholder="Telefoonnummer" type="text" required>
</div>
<div class="col-sm-12 form-group">
<input class="form-control" id="email" name="email" placeholder="Email" type="email" required>
</div>
</div>
<textarea class="form-control" id="msg" name="msg" placeholder="Bericht" rows="5"></textarea><br>
<div class="row">
<div class="col-sm-12 form-group">
<button class="btn btn-default pull-right" id="submit" name="submit" type="submit">Verstuur</button>
</div>
</div>
</div>
</div>
</div>
</div>
</form>
in the mail_handler.php, change echo to $msg
<?php
if(isset($_POST['submit'])){
$name=$_POST['name'];
$email=$_POST['email'];
$phone=$_POST['phone'];
$msg=$_POST['msg'];
$to='infosyncdevelopment#gmail.com'; // Receiver Email ID, Replace with your email ID
$subject='Form Submission';
$message="Name :".$name."\n"."Phone :".$phone."\n"."Wrote the following :"."\n\n".$msg;
$headers="From: ".$email;
if(mail($to, $subject, $message, $headers)){
$msg = "<h1>Bedankt voor uw bericht!"." ".$name.", Wij nemen zo snel mogelijk contact met u op.</h1>";
}
else{
$msg = "Something went wrong!";
}
header("Location: contact.php?$msg");
} ?>
Use AJAX
$("form").submit(function(e) {
e.preventDefault();
var name = $('#name').val();
// do this for all other input tags
$.post("file.php" , {x:name , .....} , function(data){
// do stuff with data , here data is the things echoed/printed by 'file.php'
});
});
I have a problem with my contact form. When I click the send button it just refreshes the page and doesn't send the mail. I tried so many things but couldn't find a solution. I hope somebody can help me.
Here is the php and contact part:
<?php
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$subject = $_POST['subject'];
$body ="From: $name\n E-Mail: $email\n Message:\n $message";
$mail = mail('mymail#gmail.com', $subject, $body);
if ($mail==true)
{
header ("Location: index.php?success=1#contact");
}
else
{
header ("Location: index.php?success=2#contact" );
}
}
?>
....
<form id="contact-form">
<div class="row">
<div class="col-md-6">
<form class="form-horizontal" method="post" role="form" action="index.php">
<div class="form-group">
<label for="name">Name</label>
<input type="text" class="form-control" id="name" name="name" placeholder="Enter name" required="required" />
</div>
<div class="form-group">
<label for="email">
Email Address</label>
<div class="input-group">
<span class="input-group-addon"><span class="glyphicon glyphicon-envelope"></span>
</span>
<input type="email" class="form-control" id="email" name="email" placeholder="Enter email" required="required" />
</div>
</div>
<div class="form-group">
<label for="subject">
Subject</label>
<select id="subject" name="subject" class="form-control" required="required">
<option value="na" selected="">Choose One:</option>
<option value="service">General Question</option>
<option value="suggestions">Suggestions</option>
</select>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="name">
Message</label>
<textarea id="message" name="message" class="form-control" rows="9" cols="25" required="required" placeholder="CONTACT FORM UNDER CONSTRUCTION! PLEASE USE THE ADRESS AT THE RIGHT/DOWN BELOW."></textarea>
</div>
</div>
<div class="col-md-12">
<input type="submit" name="submit" value="SEND" class="btn btn-skin pull-right" id="btnContactUs">
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<?php if($_GET['success'] == 1)
{
echo "Thank you. Your message was sent!";
}
elseif ($_GET['success'] == 2)
{
echo "Sorry, but there were error(s) found with the form you submitted. <br/>Please try again.";
}
?>
</div>
</div>
</form>
</div>
</div>
</body>
</html>
The problem is that you have a form tag inside another form tag.
Try to change the form tag what have the id contact-form to a div tag.