2017-02-14 99 views
0

我在我的网站上有以下联系表。但表单数据被附加到URL上,并且json没有被发送到PHP邮件处理程序。以下是我的表单代码。PHP不从我的网站发送电子邮件

HTML

<form novalidate="novalidate" style="width:60%;margin-left:auto;margin-right:auto;" onsubmit="sendMailNew()"> 
<div class="col-md-12"> 
    <div class="col-md-6" style="padding-right: 5px"> 
         <input type="text" class="form-control" name="contact_uname" id="contact_uname" placeholder="NAME" style="width:100%"> 
        </div> 
        <div class="col-md-6"> 
         <input type="text" class="form-control" name="contact_no" id="contact_no" placeholder="PHONE NUMBER" style="width:100%"> 
        </div> 

     <div class="col-md-12" style="margin-top: 3%"> 
      <input class="form-control" type="email" name="contact_email" id="contact_email" placeholder="E-MAIL ADDRESS" style="width:100%"> 
     </div> 
     <div class="col-md-12" style="margin-top: 3%"> 
      <input type="text" class="form-control" name="contact_company" id="contact_company" placeholder="COMPANY" style="width:100%"> 
     </div> 
     <div class="col-md-12" style="margin-top: 3%"> 
      <input type="text" class="form-control" name="contact_website" id="contact_website" placeholder="EXISTING WEBSITE(if any)" style="width:100%"> 
     </div> 
     <div class="col-md-12" style="margin-top: 3%"> 
      <input type="text" class="form-control" name="contact_social" id="contact_social" placeholder="WHAT WOULD YOU LIKE(website, social media etc)" style="width:100%"> 
     </div> 
     <div class="col-md-12" style="margin-top: 3%"> 
      <input type="text" class="form-control" name="contact_budget" id="contact_budget" placeholder="BUDGET(if we know approximately how much you're hoping to spend. we can (hopefully) come up with a strategy that fits your budget.)" style="width:100%"> 
     </div> 
     <div class="col-md-12" style="margin-top: 3%"> 
      <input type="text" class="form-control" name="contact_section" id="contact_section" placeholder="WHAT SECTION WOULD YOU LIKE ON THE SITE" style="width:100%"> 
     </div> 
     <div class="col-md-12" style="margin-top: 3%"> 
      <input type="text" class="form-control" name="contact_update" id="contact_update" placeholder="WHICH OF THOSE SECTIONS WILL YOU WANT TO BE ABLE TO UPDATE YOURSELF?" style="width:100%"> 
     </div> 
     <div class="col-md-12" style="margin-top: 3%"> 
      <input type="text" class="form-control" name="contact_design" id="contact_design" placeholder="ANY IDEA ON THE STYLE OT DESIGN? DO YOU HAVE REFERENCES?" style="width:100%"> 
     </div> 
     <div class="col-md-12" style="margin-top: 3%"> 
      <input type="text" class="form-control" name="contact_timeline" id="contact_timeline" placeholder="TIMELINE(when do you want to launch?)" style="width:100%"> 
     </div> 
     <div class="col-md-12" style="margin-top: 3%"> 
      <input type="text" class="form-control" name="contact_hear" id="contact_hear" placeholder="HOW DID YOU HEAR ABOUT US?" style="width:100%"> 
     </div> 
     <div class="col-md-12" style="margin-top: 3%"> 
      <input type="text" class="form-control" name="contact_comment" id="contact_comment" placeholder="ANY OTHER COMMENTS?" style="width:100%"> 
     </div> 
     <div class="col-md-12" style="margin-top:3%"> 
         <button type="submit" name="submit" value="submit" style="position:relative;left:45%;color:#22252c !important;background-color:white !important" class="btn btn-default">SUBMIT NOW</button> 
        </div> 
    </div> 
    </form> 

AJAX脚本

<script type="text/javascript"> 
       function sendMailNew() 
       { 
        $.ajax({ 
        type: 'POST', 
        url: 'contactmail.php', 
        data: { cname: $("#contact_uname").val(), cno: $().val("#contact_no"), cemail: $("#contact_email").val(), ccompany: $("#contact_company").val(), cwebsite: $("#contact_website").val(), csocial: $("#contact_social").val(), cbudget: $("#contact_budget").val(), csection: $("#contact_section").val(), cupdate: $("#contact_update").val(), cdesign: $("#contact_design").val(), ctimeline: $("#contact_timeline").val(), chear: $("#contact_hear").val(), ccomment: $("#contact_comment").val() }, 
        async: false, 
        success: function (data) { 
        $("#contact_uname").val()=data; 
        $("#contact_no").val()=""; 
        $("#contact_email").val()=""; 
        $("#contact_company").val()=""; 
        $("#contact_website").val()=""; 
        $("#contact_social").val()=""; 
        $("#contact_budget").val()=""; 
        $("#contact_section").val()=""; 
        $("#contact_update").val()=""; 
        $("#contact_design").val()=""; 
        $("#contact_timeline").val()=""; 
        $("#contact_hear").val()=""; 
        $("#contact_comment").val()=""; 
        },      
        dataType: 'json', 
        error: function (e, g, y) { 
        var msg = e; 
        } 
        }); 
        alert('Thanks for submitting the form, we will get back to you soon!'); 
       }   
      </script> 

PHP邮件处理

<?php 
    if(isset($_POST['submit'])){ 
    $to = "[email protected]"; 
    $from = $_POST['contact_email']; // this is the sender's Email address 
    $name = $_POST['contact_uname']; 
    $number = $_POST['contact_no']; 
    $company = $_POST['contact_company']; 
    $website = $_POST['contact_website']; 
    $social = $_POST['contact_social']; 
    $budget = $_POST['contact_budget']; 
    $section = $_POST['contact_section']; 
    $update = $_POST['contact_update']; 
    $design = $_POST['contact_design']; 
    $timeline = $_POST['contact_timeline']; 
    $hear = $_POST['contact_hear']; 
    $comment = $_POST['contact_comment']; 
    $subject = "Form submission"; 
    $subject2 = "Copy of your form submission"; 
    $message = "Name:" . $name . "\n Email: " . $from . "\n Number:" . $number . "\n Company:" . $company . "\n Website:" . $website . "\n Social:" . $social . "\n Budget:" . $budget . "\n Section:" . $section . "\n Update:" . $update . "\n Design:" . $design . "\n Timeline:" . $timeline . "\n Hear:" . $hear . "\n Comment:" . $comment; 
    $headers = "From:" . $from; 
    mail($to,$subject,$message,$headers); 
    } 
?> 

任何想法如何解决这一问题?

在此先感谢!

+0

如果页面重新加载,只有HTML和JS的显示代码。提出更具体的问题。 –

+0

在您的按钮上添加onclick属性。删除按钮提交的类型。做到这一点:sendMailNew(事件)。然后在你的函数中做到这一点,event.preventDefault();也尝试在你的ajax调用中做一个你的数据变量的console.log,以确保你实际上解析了一些东西 – Akintunde007

回答

0

首先在JavaScript中停止默认操作。

<script type="text/javascript"> 
function sendMailNew() 
    { 
    event.preventDefault(); 
    //Your rest of JS here 
    } 
</script> 

我不确定,但您可能需要将action=""添加到表单中。让我们知道它是否解决。

0

该选项不是type:'POST', 它是方法:'POST'。所以默认情况下它应该是一个GET请求。

在你的PHP脚本中,你从“$ _POST”中取值,所以它找不到你的参数。试试这种方式。我更愿意使用$ .post,而不是$ .ajax作为我的发布请求。为了使脚本更好,你应该防止默认事件,并可能使用url作为属性动作提交你的数据,然后以$('form')。attr('action')为你的请求。

$.ajax({ 
    method: "POST", 
    url: "some.php", 
    data: { name: "John", location: "Boston" } 
}) 

我喜欢这种方式,它看起来就像吸尘器,你不能与提交方法misstake:

function sendMailNew(e) 
    { 
     e.preventDefault(); 
     var $form = $('form'); 
     var url = $form.attr('action') || 'contactmail.php'; 

     var params = { 
      cname: $("#contact_uname").val(), 
      cno: $().val("#contact_no"), 
      cemail: $("#contact_email").val(), 
      ccompany: $("#contact_company").val(), 
      cwebsite: $("#contact_website").val(), 
      csocial: $("#contact_social").val(), 
      cbudget: $("#contact_budget").val(), 
      csection: $("#contact_section").val(), 
      cupdate: $("#contact_update").val(), 
      cdesign: $("#contact_design").val(), 
      ctimeline: $("#contact_timeline").val(), 
      chear: $("#contact_hear").val(), 
      ccomment: $("#contact_comment").val() 
     }; 

     $.post(url, params, function(data, status) { 

      $("#contact_uname").val()=data; 
      $("#contact_no").val()=""; 
      $("#contact_email").val()=""; 
      $("#contact_company").val()=""; 
      $("#contact_website").val()=""; 
      $("#contact_social").val()=""; 
      $("#contact_budget").val()=""; 
      $("#contact_section").val()=""; 
      $("#contact_update").val()=""; 
      $("#contact_design").val()=""; 
      $("#contact_timeline").val()=""; 
      $("#contact_hear").val()=""; 
      $("#contact_comment").val()=""; 

     }, 'json').fail(function() { 
      alert("error"); 
     }); 
    } 
+0

这是错误的。在进行ajax调用时,它是键入文章而不是方法文章。要解决此问题,请防止默认操作。 event.preventDefault(); – Akintunde007

+0

为什么?类型只是方法的别名!我们不知道他使用的是什么jQuery版本。也许他可以在他的调试工具中向我们展示一些网络请求的细节。 http://api.jquery.com/jquery.ajax/ >>>>类型(默认值:'GET') 类型:字符串 方法的别名。如果您使用1.9.0之前版本的jQuery,则应该使用类型。 <<<<< >>>方法(默认值:'GET') 类型:字符串 用于请求的HTTP方法(例如“POST”,“GET”,“PUT”)。 (版本添加:1.9.0)<<< – fGeyer

+0

我正在使用jquery 3.1.1。目前 –