[英]php form submission for phpmailer to send mail using jquery ajax
我已經在這里閱讀了很多答案並嘗試了幾乎每一個但非他們幫助我的問題是我想從數據發送聯系到phpmailer將發送郵件。 我想用jquery ajax來做。 電子郵件是完美的,但問題是我的表單重定向到“動作”。 我試過preventDefault(); 並返回false; 但它沒有幫助我。
我希望在成功提交表單后向用戶顯示該消息。 這是我使用bootstrap的表單代碼
<form class="form-horizontal" action="sections/contactdata.php" method="post" id="contact-us-form" enctype="multipart/form-data">
<!-- Name input-->
<div class="form-group">
<div class="col-md-12">
<input id="name" name="name" type="text" placeholder="Your Name"
class="form-control input-md">
</div>
</div>
<!-- Email input-->
<div class="form-group">
<div class="col-md-12">
<input id="email" name="email" type="text" placeholder="Your Email"
class="form-control input-md">
</div>
</div>
<!-- phone input-->
<div class="form-group">
<div class="col-md-12">
<input id="phone" name="phone" type="text" placeholder="Phone"
class="form-control input-md">
</div>
</div>
<!-- Textarea -->
<div class="form-group">
<div class="col-md-12">
<textarea class="form-control" id="message" name="message" placeholder="Message"
rows="6"></textarea>
</div>
</div>
<!-- Button -->
<div class="form-group">
<div class="col-md-4 pull-right">
<input type="submit" name="singlebutton" class="btn btn-success" value="SEND" id="singlebutton">
</div>
</div>
</form>
我的jquery ajax代碼
<script>
$(document).ready(function (){
$("#singlebutton").click(function(event)
{
/* stop form from submitting normally */
event.preventDefault();
/* get some values from elements on the page: */
var $form = $( this ),
$submit = $form.find( 'input[type="submit"]' ),
name_value = $form.find( 'input[name="name"]' ).val(),
phone_value = $form.find( 'input[name="phone"]' ).val(),
email_value = $form.find( 'input[name="email"]' ).val(),
message_value = $form.find( 'textarea[name="message"]' ).val(),
url = $form.attr('action');
/* Send the data using post */
var posting = $.post( url, {
name: name_value,
email: email_value,
phone: phone_value,
message: message_value
});
posting.done(function( data )
{
/* Put the results in a div */
$( "#contactResponse" ).html(data);
/* Change the button text. */
$submit.text('Sent, Thank you');
/* Disable the button. */
$submit.attr("disabled", true);
});
return false;
});
});
</script>
我的php郵件代碼
<?php
require_once '../assets/phpmailer/PHPMailerAutoload.php';
if (isset($_POST['name'], $_POST['email'], $_POST['phone'], $_POST['message'])) {
$fields = [
'name' => $_POST['name'],
'email' => $_POST['email'],
'phone' => $_POST['phone'],
'message' => $_POST['message']
];
foreach ($fields as $field => $data) {
}
$m = new PHPMailer;
$m->isSMTP();
$m->SMTPAuth = true;
$m->SMTPDebug = 1;
$m->Host = 'smtp.gmail.com';
$m->Username = 'myemail';
$m->Password = 'mypassword';
$m->SMTPSecure = 'ssl';
$m->Port = 465;
$m->isHTML();
$m->Subject = 'Contact Form submitted';
$m->Body = 'From:' . $fields['name'] . '(' . $fields['email'] . ')' . '<p><b>phone:</b><br/>' . $fields['phone'] . '</p>' . '<p><b>Message</b><br/>' . $fields['message'] . '</p>';
$m->FromName = 'Contact';
$m->AddAddress('rjsnh1522@gmail.com', 'Pawan');
if ($m->send()) {
// header('Location: ../index.php');
// print_r($_POST);
// echo 'message send';
echo "<h2>Thank you for your comment</h2>";
// die();
} else {
// echo 'try again later';
// print_r($_POST);
echo "<h2>Sorry, there has been an error</h2>";
}
}
我每次提交表單時都會收到電子郵件,但是會進入操作頁面,並在那里回復文本。 我希望它提交沒有重新加載並顯示用戶表單已提交,我應該收到電子郵件。
我也試過使用提交,點擊但沒有任何幫助我請告訴我,我做錯了。
請幫幫我謝謝你。
phpmailer代碼
<?php
require_once '../assets/phpmailer/PHPMailerAutoload.php';
if (isset($_POST['name'], $_POST['email'], $_POST['phone'], $_POST['message'])) {
$fields = [
'name' => $_POST['name'],
'email' => $_POST['email'],
'phone' => $_POST['phone'],
'message' => $_POST['message']
];
foreach ($fields as $field => $data) {
}
$m = new PHPMailer;
$m->isSMTP();
$m->SMTPAuth = true;
$m->SMTPDebug = false;
$m->do_debug = 0;
$m->Host = 'smtp.gmail.com';
$m->Username = 'your-password@gmail.com';
$m->Password = 'your-password';
$m->SMTPSecure = 'ssl';
$m->Port = 465;
$m->isHTML();
$m->Subject = 'Contact Form submitted';
$m->Body = 'From:' . $fields['name'] . '(' . $fields['email'] . ')' . '<p><b>phone:</b><br/>' . $fields['phone'] . '</p>' . '<p><b>Message</b><br/>' . $fields['message'] . '</p>';
$m->FromName = 'Contact';
$m->AddAddress('rjsnh1522@gmail.com', 'Pawan');
if ($m->send()) {
echo 'Thank You '.$_POST["name"].' We Will Contact You Soon franchise form';
die();
} else {
echo 'try again later';
}
}
我的jquery ajax代碼
<script type="text/javascript">
$(document).ready(function(){
$('#franchisedata').on('submit',function(){
var that=$(this),
url=that.attr('action'),
type=that.attr('method'),
data={};
that.find('[name]').each(function(index,value){
var that=$(this),
name=that.attr('name'),
value=that.val();
data[name]=value;
});
$.ajax({
url:url,
type:type,
data:data,
success:function(response){
console.log(response);
alert(response);
}
});
return false;
});
});
</script>
引導形式與以前相同
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.