我在这里阅读了许多答案,并且几乎每一个都尝试过,但没有一个尝试对我有帮助.问题是我想将联系人从数据发送到PHPmailer,后者将发送邮件.我想用jQuery Ajax做到这一点.电子邮件运行得很好,但问题是我的表单重定向到了“操作”.我尝试了preventDefault();并返回false;但这并没有帮助我.
我想向用户显示成功提交表单后的消息.
这是我正在使用引导程序的表单代码
<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
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('[email protected]', '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>";
}
}
我每次提交表单时都会收到电子邮件,但它会转到操作页面并在其中回显文本.我希望它提交而无需重新加载,并向用户显示该表单已提交,我应该收到电子邮件.
我也尝试使用Submit,但是单击时没有任何帮助,请告诉我我做错了.
请帮我谢谢你.
解决方法:
<?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 = '[email protected]';
$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('[email protected]', '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>
引导程序形式与以前相同
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。