send fields of form in email with php - php

I am trying to get all the fields of a html form and sent them by email when the submit button is pressed.
But it doesn't work and I don't know why.
When I test the send button, nothing display in action.php page I don't know why.
I made method="post" and action=action.php in form tag.
HTML FORM
<form action="action.php" method="POST">
<div class="p pb-3"><strong>Feel free to contact me </strong></div>
<div class="row mb-3">
<div class="col">
<div class="input-group"><span class="input-group-addon"><i class="fa fa-user-circle"></i></span>
<input class="form-control" type="text" name="name" placeholder="Name" required="required"/>
</div>
</div>
</div>
<div class="row mb-3">
<div class="col">
<div class="input-group"><span class="input-group-addon"><i class="fa fa-file-text"></i></span>
<input class="form-control" type="text" name="subject" placeholder="Subject" required="required"/>
</div>
</div>
</div>
<div class="row mb-3">
<div class="col">
<div class="input-group"><span class="input-group-addon"><i class="fa fa-envelope"></i></span>
<input class="form-control" type="email" name="mail" placeholder="E-mail" required="required"/>
</div>
</div>
</div>
<div class="row mb-3">
<div class="col">
<div class="form-group">
<textarea class="form-control" name="message" placeholder="Your Message" required="required"></textarea>
</div>
</div>
</div>
<div class="row">
<div class="col">
<button class="btn btn-primary" type="submit" name="submit" value="Send">Send</button>
</div>
</div>
</form>
```.
PHP CODE
``` code
<?php
echo 'bonjour'
if(isset($_POST['submit'])){
$name=$_POST['name'];
$email=$_POST['email'];
$subjectF=$_POST['subject'];
$msg=$_POST['message'];
$to='boussidan.raphael#gmail.com';
$subject='Form Submission';
$message="Name :".$name."\n"."Subject :".$subjectF."\n"."Wrote the following :"."\n\n".$msg;
$headers="From: ".$email;
if(mail($to, $subjectF, $message, $headers)) {
echo "<h1>Sent Successfully! Thank you"." ".$name.", I will contact you shortly!</h1>";
echo "Message :"." ".$message. , .$subjectF., .$email.
}
else{
echo "<h1>Something went wrong!</h1>";
}
}
?>

If you are making website not on local server, php mail will be probably turned off, also try https://github.com/PHPMailer/PHPMailer it is if mail does not work, cuz from your question I did not understand what is not working. Another thing if you are doing your php code into another file, not in the file where your html, you don't have to put
if(isset($_POST['submit']))
because if you are doing it in another file it means you are already pressed button to make action in that file

Related

HTML form is not sending data using POST?

Below is the PHP code I deleted some of the PHP mailer code because stack overflow refused to allow me to submit the code saying the code was too long and that I needed more explanation for the problem at hand.
<?php
if(isset($_POST['submit'])) {
$mail->addAddress($_POST['email']);
// $mail->addReplyTo($_POST['email'], $_POST['name']);
$mail->isHTML(true);
$mail->Subject = $_POST['subject'];
$mail->Body = $_POST['message'];
$mail->addAttachment($_POST['attachment']);
try {
$mail->send();
echo 'Your message was sent successfully!';
} catch (Exception $e) {
echo "Your message could not be sent! PHPMailer Error: {$mail->ErrorInfo}";
}
}
else {
echo "There is a problem with the contact.html document!";
}
?>
Below is the HTML form. I have tried submitting the form data to be processed by the PHP code but it refuses to be submitted. I have posted the PHP code above and the HTML code below.
<form method="POST" role="form" enctype="multipart/form-data">
<div class="row">
<div class=" col-md-6 mb-3">
<input type="email" class="form-control" name="email" id="email">
</div>
<div class=" col-md-6 mb-3">
<input type="text" class="form-control" name="subject" id="subject">
</div>
<div class="mb-3">
<textarea class="form-control" rows="5" name="message" id="message"></textarea>
</div>
<div class=" col-md-6 mb-3">
<input type="file" class="form-control" name="attachment" id="attachment">
</div>
</div>
</form>
<button name="submit" type="submit" class="btn btn-primary">Send mail</button>
You have to add submit button under form tag.
<form method="POST" role="form" enctype="multipart/form-data">
<div class="row">
<div class=" col-md-6 mb-3">
<input type="email" class="form-control" name="email" id="email">
</div>
<div class=" col-md-6 mb-3">
<input type="text" class="form-control" name="subject" id="subject">
</div>
<div class="mb-3">
<textarea class="form-control" rows="5" name="message" id="message"></textarea>
</div>
<div class=" col-md-6 mb-3">
<input type="file" class="form-control" name="attachment" id="attachment">
</div>
</div>
<button name="submit" type="submit" class="btn btn-primary">Send mail</button>
</form>
Thanks
Your submit button is not inside the form tag.. That is the issue Please put the submit button inside the form tag. before the closing tag
You should keep the submit button inside form tags.
<form><button name="submit" type="submit" class="btn btn-primary">Send mail</button></form>

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.

Form to Email PHP

Hi I am new to php and am not quite sure how to do this I am attempting to build a contact form that sends an email currently the HTML I Have for the form is:
<section class="mbr-section form1 cid-qL6aon4aVW" id="form1-11">
<div class="container">
<div class="row justify-content-center">
<div class="title col-12 col-lg-8">
<h2 class="mbr-section-title align-center pb-3 mbr-fonts-style display-2">
MAINTENANCE FORM
</h2>
<h3 class="mbr-section-subtitle align-center mbr-light pb-3 mbr-fonts-style display-5">
Request Maintenance for your unit.</h3>
</div>
</div>
</div>
<div class="container">
<div class="row justify-content-center">
<div class="media-container-column col-lg-8" data-form-type="formoid">
<form class="mbr-form" action="feedback.php" method="post">
<div class="row row-sm-offset">
<div class="col-md-4 multi-horizontal" data-for="name">
<div class="form-group">
<label class="form-control-label mbr-fonts-style display-7" for="name-form1-11">Full Name</label>
<input type="text" class="form-control" name="name" data-form-field="Name" required="" placeholder="Full Name" id="name-form1-11">
</div>
</div>
<div class="col-md-4 multi-horizontal" data-for="email">
<div class="form-group">
<label class="form-control-label mbr-fonts-style display-7" for="email-form1-11">Email Address</label>
<input type="email" class="form-control" name="email" data-form-field="Email" required="" placeholder="Email" id="email-form1-11">
</div>
</div>
<div class="col-md-4 multi-horizontal" data-for="phone">
<div class="form-group">
<label class="form-control-label mbr-fonts-style display-7" for="phone-form1-11">Phone Number</label>
<input type="tel" class="form-control" name="phone" data-form-field="Phone" placeholder="Phone #" id="phone-form1-11">
</div>
</div>
</div>
<div class="form-group" data-for="message">
<label class="form-control-label mbr-fonts-style display-7" for="message-form1-11">Request</label>
<textarea type="text" class="form-control" name="message" rows="7" data-form-field="Message" placeholder="Request" id="message-form1-11"></textarea>
</div>
<span class="input-group-btn">
<button href="" type="submit" class="btn btn-primary btn-form display-4">SEND FORM</button>
</span>
</form>
</div>
</div>
</div>
And the PHP I have is :
<?php
if (isset($_POST['submit'])){
$to="email#email.com";
$subject="Shovers.net Maintenance form submission";
$mail_from="from: ".$_POST['email']." \n";
$mail_from .="Content-Type: text/html; charset=utf-8 \n";
$message="<font size=\"2\" face=\"Verdana\"> Here is the message:<br />
".$_POST['comments']."<br />
".$_POST['name']."<br />
".$_POST['email']."</font>";
mail($mail_from,$to,$subject,$comments);
echo "Thanks!";
$TARGET = "/";
header("Location: $TARGET");
exit();
}else{
}
?>
I can't seem to get this to send to my email and I am not sure why on form submit it currently just goes to the feedback.php page which is blank as it is just the PHP script. Does this need to be running on the actual server for it to work or will it work with xampp and apache/mysql running? If not what do I need to do to get this working?
In addition I am also trying to get it to just reload/stay on that same page but pop up a verification message what am I doing wrong for that?
Any help appreciated!
Thanks!

How do I display message on the same page?

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'
});
});

Web page contact form in PHP not working [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 5 years ago.
So I have this HTML code for my contact form:
<section id="contact-form">
<form action="form.php" method="post" enctype="multipart/form-data">
<div class="container">
<div class="row">
<div class="col-md-6 col-sm-12">
<div class="block">
<form>
<div class="form-group">
<input type="text" class="form-control" placeholder="Ime" name="name">
</div>
<div class="form-group">
<input type="text" class="form-control" placeholder="Email Adresa" name="email">
</div>
<div class="form-group">
<input type="text" class="form-control" name="subject" placeholder="Predmet">
</div>
</form>
</div>
</div>
<div class="col-md-6 col-sm-12">
<div class="block">
<form>
<div class="form-group-2">
<textarea class="form-control" rows="3" placeholder="Poruka" name="message"></textarea>
</div>
<button class="btn btn-default" type="submit">Pošalji</button>
</form>
</div>
</div>
</div>
</div>
</form>
</section>
And my PHP script looks like this:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: Kontakt s web-stranice';
$to = 'myEmailAddress';
$subject = 'Kontakt s web-stranice ';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
?>
<?php
if ($_POST['submit']) {
if (mail ($to, $subject, $body, $from)) {
echo '<p>Thank you for your email!</p>';
} else {
echo '<p>Oops! An error occurred. Try sending your message again.</p>';
}
}
?>
And for some reason when I click on the submit button the web page refreshes and the message isn't sent to my email.
Does anyone know why?
Your HTML format is using forms within forms, so everything outside that <form> tag is not being collected. All you need to do is have the first <form> tag and wrap all your input boxes and the submit button in it.
Notice in the code below how you only need one <form>.
<section id="contact-form">
<form action="form.php" method="post" enctype="multipart/form-data">
<div class="container">
<div class="row">
<div class="col-md-6 col-sm-12">
<div class="block">
<div class="form-group">
<input type="text" class="form-control" placeholder="Ime" name="name">
</div>
<div class="form-group">
<input type="text" class="form-control" placeholder="Email Adresa" name="email">
</div>
<div class="form-group">
<input type="text" class="form-control" name="subject" placeholder="Predmet">
</div>
</div>
</div>
<div class="col-md-6 col-sm-12">
<div class="block">
<div class="form-group-2">
<textarea class="form-control" rows="3" placeholder="Poruka" name="message"></textarea>
</div>
<button class="btn btn-default" type="submit">Pošalji</button>
</div>
</div>
</div>
</div>
</form>
</section>
Your PHP code is correct from the looks of things, but that messed up form tag was the big problem.

Categories