I am working on a site, and having never done something like this, I am unsure how to proceed with PHP. I am working with bootstrap and looking to send an email based on a modal. My HTML looks like
<div class="modal fade" id="contact" role="dialoge">
<div class="modal-dialog">
<div class="modal-content">
<form class="form-horizontal">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4>Plan your Party!</h4>
</div>
<form class="name" name="contact">
<div class="modal-body">
<div class="form-group">
<label for="name" class="col-lg-2 control-label">Name:</label>
<div class="col-lg-10">
<input type="text" class="form-control" name="name" required="required" placeholder="Full Name">
</div>l
</div>
<div class="form-group">
<label for="email" class="col-lg-2 control-label">Email:</label>
<div class="col-lg-10">
<input type="email" class="form-control" name="email" required="required" placeholder="Email">
</div>
</div>
<div class="form-group">
<label for="message" class="col-lg-2 control-label">Message:</label>
<div class="col-lg-10">
<textarea class="form-control" rows="8" style="resize: vertical;" required="required" placeholder="Message" name="message"></textarea>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-info" data-dismiss="modal"><i class="glyphicon glyphicon-remove"></i> Close</button>
<button class="btn btn-success" type="submit" id="submit"><i class="glyphicon glyphicon-inbox"></i> Submit</button>
</div>
</div>
</div>
</div>
So I am just wondering if anyone could give me some pointers to make this form actually send the email. I am looking to learn and willing to try anything. Thanks in advance, help and constructful criticism appreciated. Also, how would I approach adding a confirmation that the message has been sent?
Thanks, John.
<script>
$(document).ready(function () {
$("input#submit").click(function(){
$.ajax({
type: "POST",
url: "process.php", //
data: $('form.contact').serialize(),
success: function(msg){
$("#thanks").html(msg)
$("#form-content").modal('hide');
},
error: function(){
alert("failure");
}
});
});
});
</script>
This is the script I tried to add to call the php:
<?php
$myemail = 'myemail#myemail.ca';
if (isset($_POST['name'])) {
$name = strip_tags($_POST['name']);
$email = strip_tags($_POST['email']);
$message = strip_tags($_POST['message']);
echo "<span class=\"alert alert-success\" >Your message has been received. Thanks! Here is what you submitted:</span><br><br>";
echo "<stong>Name:</strong> ".$name."<br>";
echo "<stong>Email:</strong> ".$email."<br>";
echo "<stong>Message:</strong> ".$message."<br>";
$to = $myemail;
$email_subject = "Contact form submission: $name";
$email_body = "You have received a new message. ".
" Here are the details:\n Name: $name \n ".
"Email: $email\n Message \n $message";
$headers = "From: $myemail\n";
$headers .= "Reply-To: $email";
mail($to,$email_subject,$email_body,$headers);
}
?>
And I mean I am sure there is something I am missing, or some simple fix, this is all just so new to me.
Following are the edits for you:
1) You have two forms in the modal and none of them having closing form tag. Remove <form class="form-horizontal"> form and change your other form to <form class="contact">
2) Give closing form tag.
3) Change submit button to <button class="btn btn-success" id="submit"><i class="glyphicon glyphicon-inbox"></i> Submit</button>
4) Also check updated AJAX code and PHP code.
Hope this should help you..
HTML
<div id="thanks"> </div>
<p>
<a data-toggle="modal" href="#contact"
class="btn btn-primary">Contact us</a>
</p>
<!-- model content -->
<div class="modal hide fade in" style="display: none; " id="contact" >
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4>Plan your Party!</h4>
</div>
<form class="contact">
<div class="modal-body">
<div class="form-group">
<label for="name" class="col-lg-2 control-label">Name:</label>
<div class="col-lg-10">
<input type="text" class="form-control" name="name" required="required" placeholder="Full Name">
</div>
</div>
<div class="form-group">
<label for="email" class="col-lg-2 control-label">Email:</label>
<div class="col-lg-10">
<input type="email" class="form-control" name="email" required="required" placeholder="Email">
</div>
</div>
<div class="form-group">
<label for="message" class="col-lg-2 control-label">Message:</label>
<div class="col-lg-10">
<textarea class="form-control" rows="8" style="resize: vertical;" required="required" placeholder="Message" name="message"></textarea>
</div>
</div>
</div>
</form>
<div class="modal-footer">
<button type="button" class="btn btn-info" data-dismiss="modal"><i class="glyphicon glyphicon-remove"></i> Close</button>
<button class="btn btn-success" id="submit"><i class="glyphicon glyphicon-inbox"></i> Submit</button>
</div>
</div>
</div>
</div>
AJAX
<script>
$(function() {
//twitter bootstrap script
$("button#submit").click(function(){
$.ajax({
type: "POST",
url: "process.php",
data: $('form.contact').serialize(),
success: function(msg){
$("#thanks").html(msg);
$("#contact").modal('hide');
},
error: function(){
alert("failure");
}
});
});
});
</script>
process.php
<?php
$myemail = 'myemail#myemail.ca';
if (isset($_POST['name']))
{
$name = strip_tags($_POST['name']);
$email = strip_tags($_POST['email']);
$message = strip_tags($_POST['message']);
$msg_success = "";
$msg_fail = "Error sending mail.";
$msg_success .= "<span class=\"alert alert-success\" >Your message has been received. Thanks! Here is what you submitted:</span><br><br>";
$msg_success .= "<stong>Name:</strong> ".$name."<br>";
$msg_success .= "<stong>Email:</strong> ".$email."<br>";
$msg_success .= "<stong>Message:</strong> ".$message."<br>";
$to = $myemail;
$email_subject = "Contact form submission: $name";
$email_body = "You have received a new message. ".
" Here are the details:\n Name: $name \n ".
"Email: $email\n Message \n $message";
$headers = "From: $myemail\n";
$headers .= "Reply-To: $email";
if(mail($to,$email_subject,$email_body,$headers))
echo $msg_success;
else
echo $msg_fail;
}
else
echo "Input parameters missing";
?>
In form tag include action attribute to point any PHP page, from there you can send emails...
Related
<div class="col-lg-10 col-lg-offset-1 text-center">
<form method="POST" action="index.php" accept="text/html" formenctype="multipart/form-data" class="contact-form row">
<div class="col-md-4">
<label></label>
<input name="name" type="text" class="form-control" placeholder="Name">
</div>
<div class="col-md-4">
<label></label>
<input name="email" type="text" class="form-control" placeholder="Email">
</div>
<div class="col-md-4">
<label></label>
<input name="subject" type="text" class="form-control" placeholder="Subject">
</div>
<div class="col-md-12">
<label></label>
<textarea name="message" class="form-control" rows="9" placeholder="Your message here.."></textarea>
</div>
<div class="col-md-4 col-md-offset-4">
<label></label>
<button id="submit" name="submit" type="sumbit" data-toggle="modal" data-target="#alertModal" class="btn btn-primary btn-block btn-lg">Send <i class="ion-android-arrow-forward"></i></button>
</div>
</form>
</div>
</div>
</div>
</section>
<div id="alertModal" class="modal fade" tabindex="-1" role="dialog" aria-hidden="true">
<div class="modal-dialog modal-sm">
<div class="modal-content">
<div class="modal-body">
<h2 class="text-center">Submitted sucessfully!</h2>
<p class="text-center">You clicked the button</p>
<br/>
<button class="btn btn-primary btn-lg center-block" data-dismiss="modal" aria-hidden="true">OK <i class="ion-android-close"></i></button>
</div>
</div>
</div>
</div>
<?php
$headers = ''; // added this line
$headers. = "MIME-Version: 1.0\r\n";
$headers. = "Content-type: text/html\r\n";
$headers = 'From: $name'.
"\r\n". // added .= instead of =
'X-Mailer: PHP/'.phpversion();
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: '.$email;
$to = 'random#gmail.com';
$subject = $_POST['subject'];
$body = " From: $name\n E-mail: $email\n Subject: $subject\n Message:\n $message";
if (isset($_POST['submit'])) {
/* Anything that goes in here is only performed if the form is submitted */
if (mail('random#gmail.com', $subject, $body, $from)) {
echo "<script type='text/javascript'>$(document).ready(function(){ $('#alertModal').modal('show');});</script>";
} else {
echo "something went wrong";
}
} ?>
I have been trying to call my modal when submitting my form, but cannot seem to get it to work.
I have been looking at various different questions but nothing seems to work.
I do not have JS for this and my PHP is incorporated on a separate page.
Please help, I want the modal to open on the same page as the form when someone submits the info.
Thanks
See , it is working
function showModal(){
$('#alertModal').modal('show');
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<div class="col-lg-10 col-lg-offset-1 text-center">
<form method="POST" action="index.php" accept="text/html" formenctype="multipart/form-data" class="contact-form row">
<div class="col-md-4">
<label></label>
<input name="name" type="text" class="form-control" placeholder="Name">
</div>
<div class="col-md-4">
<label></label>
<input name="email" type="text" class="form-control" placeholder="Email">
</div>
<div class="col-md-4">
<label></label>
<input name="subject" type="text" class="form-control" placeholder="Subject">
</div>
<div class="col-md-12">
<label></label>
<textarea name="message" class="form-control" rows="9" placeholder="Your message here.."></textarea>
</div>
<div class="col-md-4 col-md-offset-4">
<label></label>
<button id="submit" name="submit" type="sumbit" data-toggle="modal" data-target="#alertModal" class="btn btn-primary btn-block btn-lg">Send <i class="ion-android-arrow-forward"></i></button>
</div>
</form>
</div>
</div>
</div>
</section>
<div id="alertModal" class="modal fade" tabindex="-1" role="dialog" aria-hidden="true">
<div class="modal-dialog modal-sm">
<div class="modal-content">
<div class="modal-body">
<h2 class="text-center">Submitted sucessfully!</h2>
<p class="text-center">You clicked the button</p>
<br/>
<button class="btn btn-primary btn-lg center-block" data-dismiss="modal" aria-hidden="true">OK <i class="ion-android-close"></i></button>
</div>
</div>
</div>
</div>
<?php
$headers = ''; // added this line
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html\r\n";
$headers = 'From: $name' . "\r\n" . // added .= instead of =
'X-Mailer: PHP/' . phpversion();
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: ' . $email;
$to = 'random#gmail.com';
$subject = $_POST['subject'];
$body = " From: $name\n E-mail: $email\n Subject: $subject\n Message:\n $message";
if (isset($_POST['submit']))
{
/* Anything that goes in here is only performed if the form is submitted */
if (mail ('random#gmail.com', $subject, $body, $from ))
{
echo "<script type='text/javascript'>showModal();</script>";
} else
{
echo "something went wrong";
}
}
?>
you have two choice
change button type:
<button type="button" class="btn btn-primary btn-lg center-block" data-dismiss="modal" aria-hidden="true">OK <i class="ion-android-close"></i></button>
call function:
<form method="POST" action="index.php" accept="text/html" formenctype="multipart/form-data" class="contact-form row" onSubmit="show_modal();return;">
and the function:
function show_modal(){
$('#myModal').modal('show');
}
UPDATE (requested in comments)
Set timer for submit form after delay:
Change button type to button: type="button"
add this code to your page:
$("#submit").click(function(){
setTimeout(function() {
$("form").submit();
}, 5000); // 5 sec
$("#myModal").show(); //Find your modal id and replace with myModal
});
Note: don't forget to include jquery lib
My modal is not showing. the page is reloading and then i receive an email.
Below you can find my code. Can somebody help me out please... I think the problem is that my page is reloading after clicking the submit button.
Below my form that i use :
<form id="form" method="post" name="form" autocomplete="on">
<div class="row">
<div class="col-sm-6 nopadding-right">
<div class="input-group">
<div class="input-group-addon"><span class="fa fa-user"></span></div>
<input type="text" name="vname" placeholder="Name" class="form-control requiredField name" value="" />
</div>
<div class="input-group">
<div class="input-group-addon"><span class="fa fa-phone"></span></div>
<input type="text" name="vphone" placeholder="Phone" class="form-control requiredField phone" value="" />
</div>
<div class="input-group">
<div class="input-group-addon"><span class="fa fa-at"></span></div>
<input type="email" name="vmail" placeholder="Enter email" class="form-control requiredField email" value="" />
</div>
</div>
<div class="col-sm-6">
<div class="input-group mail-block">
<div class="input-group-addon"><span class="fa fa-pencil"></span></div><textarea class="form-control message" name="vtext" placeholder="Message" rows="4"></textarea><input class="btn btn-primary inverse-btn" id="send" name="confirm" type="submit" value="send">
</div>
</div>
</div>
</form>
The modal :
<div class="modal fade" id="Modal" tabindex="-1" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title" id="myModalLabel">Thank you</h4>
</div>
<div class="modal-body">
<p>Thanks for getting in touch!</p>
</div>
</div>
</div></div>
My php code :
<?php if(isset($_POST["confirm"])){ if($_POST["vname"]==""||$_POST["vmail"]==""||$_POST["vphone"]==""||$_POST["vtext"]==""){
echo "error";}else{
$email=$_POST['vmail'];
$email =filter_var($email, FILTER_SANITIZE_EMAIL);
$email= filter_var($email, FILTER_VALIDATE_EMAIL);
if (!$email){
echo "error";
}
else{
$subject = "mail " . $_POST['vname'];
$message = $_POST['vtext'] . "\r\n" . ' phone ' . $_POST['vphone'] ."\r\n" . $_POST['vmail'];
$headers = 'From:'. "xxx#email.com" . "\r\n"; // Sender's Email
$headers .= 'Cc:'. "xxx#email.com" . "\r\n"; // Carbon copy to Sender
$message = wordwrap($message, 70);
mail("xxx.yy#xxx.com", $subject, $message, $headers);
echo "<script type='text/javascript'>
$(document).ready(function(){
$('#Modal').modal('show');
});
</script>";
}
}
}?>
The remedy is to use AJAX call instead of form submission. It allows you to do better error handling as well.
Ok so I have the modal toggling, that is easy with bootstrap. But now, whenever I click submit the form gets redirected to php/contact-process.php. It looks like the php is processed but then just stays at a white screen, and not being redirected and no email is received to my main email(i put example#gmail.com for stackexchange question). Please help me.
<!--Modals for Contact-->
<div class="modal fade" id="contact" role = "dialog">
<div class="modal-dialog">
<div class="modal-content">
<form class="form-horizontal contact" name="contact" action="php/contact-process.php">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
<h4>Contact Us</h4>
</div>
<div class="modal-body">
<div class="form-group">
<label for="contact-name" class="col-lg-2 control-label">Name:</label>
<div class="col-lg-10">
<input type="text" name="name" class="form-control" id="contact-name" placeholder="Full Name">
</div>
</div>
<div class="form-group">
<label for="contact-email" class="col-lg-2 control-label">Email:</label>
<div class="col-lg-10">
<input type="email" name="email" class="form-control" id="contact-email" placeholder="you#example.com">
</div>
</div>
<div class="form-group">
<label for="contact-message" class="col-lg-2 control-label">Message:</label>
<div class="col-lg-10">
<textarea name="usertext" name="message" id="" rows="8" class="form-control" style="resize:none;"></textarea>
</div>
</div>
</div>
<div class="modal-footer">
Close
<button class="btn btn-primary" type="submit" value="Send!">Send</button>
</div>
</form>
</div>
</div>
</div>
CONTACT-PROCESS.PHP:
<?php
//add the recipient's address here
$myemail = 'example#gmail.com';
//grab named inputs from html then post to #thanks
if (isset($_POST['name'])) {
$name = strip_tags($_POST['name']);
$email = strip_tags($_POST['email']);
$message = strip_tags($_POST['message']);
//generate email and send!
$to = $myemail;
$email_subject = "Contact form submission: $name";
$email_body = "You have received a new message. ".
" Here are the details:\n Name: $name \n ".
"Email: $email\n Message \n $message";
$headers = "From: $myemail\n";
$headers .= "Reply-To: $email";
mail($to,$email_subject,$email_body,$headers);
//redirect
header("Location: index.php");
exit();
}
?>
I am trying send an email through my contact form, here is my php code:
if(isset($_POST['send'])) {
$fn = $_POST['firstname'];
$ln = $_POST['lastname'];
$name = $fn . ' ' . $ln;
$email = $_POST['email'];
$message = $_POST['comment'];
$subject = $_POST['subject'];
$to = "iesteghlal#gmail.com";
$header = "From: $email \r\n";
$send_contact = mail($to, $subject, $message, $header);
if($send_contact){
echo "We've recived your contact information";
echo "Your email has been sent. Stay in touch with us.";
echo $name;
echo $email;
echo $message;
echo $subject;
echo $mailheader;
echo $body;
echo "</br><a href='index.html'> Go Back. </a>";
}
else {
echo "something not working";
}
}
else {
echo "Something wrong.";
}
when I press send, it goes to the next page and it gives me "something not working" error. I am not quite sure what is going wrong here.
this is my contact form:
<form role="form" action="send.php" method="post" accept-charset="utf-8">
<div class="modal-body" style="padding: 5px; background-color:#D7D1D1;">
<div class="row">
<div class="col-lg-6 col-md-6 col-sm-6" style="padding-bottom: 10px;">
<input class="form-control" id="firstname" name="firstname" placeholder="Firstname" required="" autofocus="" type="text">
</div>
<div class="col-lg-6 col-md-6 col-sm-6" style="padding-bottom: 10px;">
<input class="form-control" id="lastname" name="lastname" placeholder="Lastname" required="" type="text">
</div>
</div>
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12" style="padding-bottom: 10px;">
<input class="form-control" id="email" name="email" placeholder="E-mail" required="" type="text">
</div>
</div>
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12" style="padding-bottom: 10px;">
<input class="form-control" id="subject" name="subject" placeholder="Subject" required="" type="text">
</div>
</div>
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12">
<textarea style="resize:vertical;" class="form-control" placeholder="Message..." rows="6" id="comment" name="comment" required=""></textarea>
</div>
</div>
</div>
<div class="panel-footer">
<input class="btn btn-success" value="Send" type="submit" id="submit" name="send">
<!--<span class="glyphicon glyphicon-ok"></span>-->
<input class="btn btn-danger" value="Clear" type="reset">
<!--<span class="glyphicon glyphicon-remove"></span>-->
<button style="float: right;" type="button" class="btn btn-default btn-close" data-dismiss="modal">Close</button>
</div>
</form>
what is going wrong?
Are you using a live test server for this or are you using the likes of an offline server such as Apache? If you are using an offline server the email will not send unless you have setup utilities to do so.
This is not an answer, but due to a lack of 50 rep I cannot comment.
EDIT
Suggestion of an auto-redirect after the post has been sent, rather than a manual link.
<script type="text/javascript">
setTimeout('Redirect()', 5000)
function Redirect() {
location.href='contact.php'
}
</script>
Something like this can work, by adding to your existing code.
I have a modal on my page, with a contact form in it. Anyway, the fields like mail should be required but it just sends it right away, even when the fields aren't filled in... How is that possible?
The Modal:
<div class="modal fade" id="form-content" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header" style="border-bottom: none;">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">Contact</h4>
</div>
<div class="modal-body">
<form class="contact" name="contact">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-user"></i></span>
<input type="text" name="name" class="form-control" placeholder="Name" required="required">
</div>
<br/>
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-envelope"></i></span>
<input type="email" name="email" class="form-control" placeholder="Mail">
</div>
<br/>
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-globe"></i></span>
<input type="url" name="website" class="form-control" placeholder="Website">
</div>
<br/>
<textarea rows="6" name="message" class="form-control" placeholder="Message"></textarea>
</form>
</div>
<div class="modal-footer" style="border-top: none;">
<input class="btn btn-success" type="submit" value="Send!" id="submit">
</div>
</div>
</div>
</div>
PHP script:
<?php
$myemail = 'mail#mail.nl';
if (isset($_POST['name'])) {
$name = strip_tags($_POST['name']);
$email = strip_tags($_POST['email']);
$message = strip_tags($_POST['message']);
$website = strip_tags($_POST['website']);
echo "<br><span style=\"display:table; margin:0 auto;\" class=\"alert alert-success\" id=\"alert-message\" data-alert=\"alert\">Your message has been received. Thank you!</span>";
$to = $myemail;
$email_subject = "Contact formulier van: $name";
$email_body = "Je hebt een nieuw bericht ontvangen via het contactformulier. ".
"Hier zijn de de details:\n Naam: $name \n ".
"Email: $email\n Website: $website \n Bericht: \n $message";
$headers = "From: $email\n";
$headers .= "Reply-To: $email";
mail($to,$email_subject,$email_body,$headers);
}?>
EDIT:
I also hava a main.js, to show the people the form has been sended:
$(document).ready(function () {
$("input#submit").click(function(){
$.ajax({
type: "POST",
url: "process.php", //
data: $('form.contact').serialize(),
success: function(msg){
$("#thanks").html(msg)
$("#form-content").modal('hide');
$('#thanks').delay(3000).fadeOut()
},
error: function(){
alert("failure");
}
});
});
});
You missed the action and method in your form: action="yourpage.php" method="POST"
Where action refers to your php page and method to the way you send data, in this case POST as in your PHP code you have $_POST['name'] (etc).
Also the Submit should be inside the Form tag, not outside.
Something like this:
<div class="modal fade" id="form-content" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header" style="border-bottom: none;">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">Contact</h4>
</div>
<div class="modal-body">
<form class="contact" name="contact">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-user"></i></span>
<input type="text" name="name" class="form-control" placeholder="Name" required="required">
</div>
<br/>
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-envelope"></i></span>
<input type="email" name="email" class="form-control" placeholder="Mail">
</div>
<br/>
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-globe"></i></span>
<input type="url" name="website" class="form-control" placeholder="Website">
</div>
<br/>
<textarea rows="6" name="message" class="form-control" placeholder="Message"></textarea>
<div class="modal-footer" style="border-top: none;">
<input class="btn btn-success" type="submit" value="Send!" id="submit">
</div>
</form>
</div>
</div>
</div>
Ref: http://www.w3schools.com/tags/tag_form.asp
You have to mention form method and action in form open tag. After then check the output.
for example, Form structure should be like,
<form name="input" action="demo_form_action.asp" method="get">
Username: <input type="text" name="user">
<input type="submit" value="Submit">
</form>