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'
});
});
Related
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.
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
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>
PHP form on my website isn't responding well. It would be appreciated if someone could help me.
This is the HTML Code of the form
<form action="contactform.php" method="post" name="form" class="p-5 bg-white">
<div class="row form-group">
<div class="col-md-6 mb-3 mb-md-0">
<label class="text-black" for="fname">First Name</label>
<input type="text" id="fname" class="form-control">
</div>
<div class="col-md-6">
<label class="text-black" for="lname">Last Name</label>
<input type="text" id="lname" class="form-control">
</div>
</div>
<div class="row form-group">
<div class="col-md-12">
<label class="text-black" for="email">Email</label>
<input type="email" id="email" class="form-control">
</div>
</div>
<div class="row form-group">
<div class="col-md-12">
<label class="text-black" for="subject">Subject</label>
<input type="subject" id="subject" class="form-control">
</div>
</div>
<div class="row form-group">
<div class="col-md-12">
<label class="text-black" for="mssg">Message</label>
<textarea name="mssg" id="mssg" cols="30" rows="7" class="form-control" placeholder="Write your notes or questions here..."></textarea>
</div>
</div>
<div class="row form-group">
<div class="col-md-12">
<input type="submit" value="Send Message" class="btn btn-primary py-2 px-4 text-white">
</div>
</div>
</form>
This is the PHP Code
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$to = "alyyashar#gmail.com";
$subject = "New email from your site!";
$fname = $_POST['fname'];
$email = $_POST['email'];
$message = $_POST['message'];
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
$title = '<h3>Hello! You have received a new mail from your website!</h3>';
$body = "$title
<br/>
<b>From:</b> $fname
<br/>
<b>E-Mail:</b> $email
<br/>
<b>Message:</b>\n$message
<br/>
<br/>";
if (mail($to, $subject, $body, $headers)){
echo "<h1>Sent Successfully! Thank you"." ".$fname.", We will contact you shortly!</h1>";
} else {
echo "Something went wrong!";
}
}
?>
<br>
Back to Homepage
This is the email I receive
Screenshot of the email
When I enter information into the form and click send message, I do receive the email but there is no content in it.
The form elements have no name attributes, which is what the browser uses to send their name/value pairs to the server. So while it's posting the form, none of the values are being included.
For example, this:
<input type="text" id="fname" class="form-control">
Should be this:
<input type="text" id="fname" class="form-control" name="fname">
The same fix would need to be repeated for the remaining form elements.
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.