我有一个可以正常工作的联系表,但是我的问题是,如果您单击提交并回显“已发送电子邮件!”,它将转到另一个页面。
如何防止此类重定向?我希望它仅在按钮下面弹出,否则该按钮消失,将被“已发送电子邮件”取代。或“错误!”?
这是我的HTML:
<div style="padding-top: 75px; padding-left: 0;" class="col-md-6 col-sm-6 col-xs-12 contact-padding hidden-xs hidden-sm" id="form_container">
<div class="row">
<div class="col-centered1 col-md-8">
<h2 style="font-family: 'yrthree_boldregular', sans-serif;" class="divider-header3">Hit us up</h2>
</div>
</div>
<form role="form" method="post" id="contact-form" action="mail.php" name="contact-form">
<div class="row">
<div class="col-centered1 col-md-8 form-group">
<input type="text" id="name" name="name" class="form-control" placeholder="NAME" required>
</div>
</div>
<div class="row">
<div class="col-centered1 col-md-8 form-group">
<input type="text" id="email" name="email" class="form-control" placeholder="EMAIL" required>
</div>
</div>
<div class="row">
<div class="col-centered1 col-md-8 form-group">
<textarea style="resize: none; border: 0; color: #07002c;" class="form-control" type="text" name="message" id="message" placeholder="MESSAGE" maxlength="6000" rows="7"></textarea>
</div>
</div>
<div class="row">
<div class="col-centered1 col-md-8 form-group">
<a class="btn btn-primary pull-left send-button1" onclick="document.getElementById('contact-form').submit();">
<img draggable="false" src="/images/send-button1.jpg" alt="" onmouseover="this.src='/images/send-button2.jpg';" onmouseout="this.src='/images/send-button1.jpg';">
</a>
</div>
</div>
</form>
</div>
这是我的PHP代码:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$content="From: $name \n Email: $email \n Message: $message";
$recipient = "MY@EMAIL.com";
$mailheader = "From: $email \r\n";
mail($recipient, $content, $mailheader) or die("Error!");
echo "Email sent!";
?>
谢谢! :)
答案 0 :(得分:1)
您可以使用ajax发送而不重定向到另一个页面。我在this上阅读了此答案。您可以这样编写代码:
<script type="text/javascript">
function sendEnquiryform(){
var name=$('#name').val();
var email=$('#email').val();
var message=$('#message').val();
$.post("send_mail.php",'name='+name+'&email='+email'&message='+message,function(result,status,xhr) {
if( status.toLowerCase()=="error".toLowerCase() )
{ alert("An Error Occurred.."); }
else {
//alert(result);
$('#sucessMessage').html(result);
}
})
.fail(function(){ alert("something went wrong. Please try again") });
}
HTML:
<form method="post" name="FrmEnquiry" id="FrmEnquiry" action="" onsubmit="sendEnquiryform();">
<input name="name" id="name" required="required" placeholder="Your Name">
<input name="email" id="email" type="email" required="required" placeholder="Your Email">
<div class="clearfix"> </div>
<textarea name="message" id="message" cols="20" rows="5" required="required" placeholder="Message"></textarea>
<div class="submit">
<input id="submit" name="submit" type="submit" value="Submit">
</div>
</form>
<span id="sucessMessage"> </span>
发送mail.php
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: agriindiaexp.com';
$to = 'shridhar.kagi@gmail.com';
$subject = 'Email Inquiry';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if ($_POST['submit']) {
if (mail ($to, $subject, $body, $from)) {
$success = "Message successfully sent";
} else {
$success = "Message Sending Failed, try again";
}
}
?>
这对我有用。感谢author的回答。
答案 1 :(得分:0)
表单动作应为空白 并添加php代码应在同一页面和文件ext上。应该是.php