2017-02-14 97 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。目前 –