简体   繁体   English

联系人表格正在发送电子邮件,但提交后未回复。

[英]Contact form is sending email but not responding after submission.

I have my contact form set where after submitting it redirects back to my homepage but for some reason it just stays on the same page. 我设置了联系表单,提交后将其重定向到我的主页,但由于某种原因,它只停留在同一页面上。 I'm trying to receive confirmation after email is sent and then a redirect back to my homepage. 发送电子邮件后,我试图接收确认,然后重定向回我的主页。 I'm using php and javascript........................................... 我正在使用php和javascript ....................................................

<div class="col-sm-6 col-sm-offset-3">

    <h3>Send me a message</h3>
    <form role="form" id="contactForm" action="index2.php" method="POST">
     <div class="row">
            <div class="form-group col-sm-6">
                <label for="name" class="h4">Name</label>
                <input type="text" class="form-control" id="name" placeholder="Enter name" required>
        </div>
        <div class="form-group col-sm-6">
            <label for="email" class="h4">Email</label>
            <input type="email" class="form-control" id="email" placeholder="Enter email" required>
        </div>
    </div>
    <div class="form-group">
        <label for="message" class="h4 ">Message</label>
        <textarea id="message" class="form-control" rows="5" 
placeholder="Enter your message" required></textarea>
    </div>
    <button type="submit" id="form-submit" class="btn btn-primary btn-lg 
pull-right ">Submit</button>
<div id="msgSubmit" class="h3 text-center hidden">Message Submitted!</div>
   </form>

index2.php index2.php

 <meta http-equiv="refresh" content="0; url=http://myurl.com/" />


</header>

<?php
$name = $_POST["name"];
$email = $_POST["email"];
$message = $_POST["message"];

$EmailTo = "arash281pro@live.com";
$Subject = "New Message Received";

// prepare email body text
$Body .= "Name: ";
$Body .= $name;
$Body .= "\n";

$Body .= "Email: ";
$Body .= $email;
$Body .= "\n";

$Body .= "Message: ";
$Body .= $message;
$Body .= "\n";

// send email
$success = mail($EmailTo, $Subject, $Body, "From:".$email);

// redirect to success page
if ($success){
   echo "success";
}else{
echo "invalid";
}
?>

js js

$("#contactForm").submit(function(event){
// cancels the form submission
event.preventDefault();
submitForm();
});

function submitForm(){
// Initiate Variables With Form Content
var name = $("#name").val();
var email = $("#email").val();
var message = $("#message").val();

$.ajax({
    type: "POST",
    url: "index2.php",
    data: "name=" + name + "&email=" + email + "&message=" + message,
    success : function(text){
        if (text == "success"){

            formSuccess();
        }
    }
});
}
function formSuccess(){

$( "#msgSubmit" ).removeClass( "hidden" );
}

var confirmSubmit = true;

$('form').submit(function(e) {
  if (confirmSubmit) {
   e.stopPropagation();

if (confirm('Are you sure you want to send this form?')) {
  confirmSubmit = false;

  $('form').submit();
}else{
  alert("The form was not submitted.");
  }
  }
});

You're not actually redirecting back to your homepage. 您实际上并没有重定向回到首页。 For that you'll need to add something like this after your submit function: 为此,您需要在Submit函数之后添加以下内容:

window.location.replace("index2.php");

Rather than using <meta http-equiv="refresh" content="0; url=http://myurl.com/" /> in index2.php, just add header("Location: http://myurl.com/"); 而不是在index2.php中使用<meta http-equiv="refresh" content="0; url=http://myurl.com/" /> ,只需添加header("Location: http://myurl.com/"); <meta http-equiv="refresh" content="0; url=http://myurl.com/" /> header("Location: http://myurl.com/"); at the end of the script instead of "success" or "invalid" message. 在脚本末尾而不是“成功”或“无效”消息。 Also remove any HTML before the PHP code starts to prevent errors like "Headers already sent, output started on line ..." 在PHP代码开始之前,还要删除所有HTML,以防止出现诸如“标题已发送,输出开始于行...”之类的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 PHP联系表格将在提交后刷新页面。 - PHP contact form will just refresh the page after submission. 表单验证和提交后,禁用“提交”按钮。 PHP,JQ? - Disable “submit” button after form validation and submission. PHP, JQ? 提交ajax表单后,iScroll4高度错误。 高度是表单提交之前的原始高度 - iScroll4 height is wrong after ajax form submission. Height is orginal height before form submit 提交表单后,Backbone.Marionette + Rails应用程序将重定向。 为什么? - Backbone.Marionette + Rails app redirects after form submission. Why? 从联系表单发送电子邮件后,无法显示成功消息 - Unable to display Success message after sending an email from the contact form PHP的形式不发送联系人电子邮件 - php form not sending contact email 提交表单后,加载微调框(ng-show = loading)始终为true。 接收数据后需要将其设置为假 - Loading spinner (ng-show = loading) always true after form submission. Need to make it false after receiving data 提交后下载文件联系表格 7 Wordpress - Download File after submission Contact Form 7 Wordpress PHP的联系方式发送电子邮件,没有任何信息 - php contact form sending email with no information PHP不从联系表格发送电子邮件 - php not sending email from contact form
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM