2015-06-23 31 views
2

在index.php頂部的PHP代碼。每當我點擊提交它顯示發送的消息,但它會生成一個空白的電子郵件到我的電子郵件地址它不能捕獲來自後的數據。php腳本發送空白郵件無法獲取發佈數據

<?php 
    $emailSubject = 'Customer Has a Question!'; 
    $webMaster = '[email protected]'; 
    $name = $_POST['iname']; 
    $email = $_POST['iemail']; 
    $phone=$_POST['iphone']; 
    $question = $_POST['imessage']; 
    echo $name; 
    $body = <<<EOD 
    <br><hr><br> 
    Name: $name <br> 
    Email: $email <br> 
    Questions: $question <br> 
    EOD; 
    $headers = "From: $email\r\n"; 
    $headers .= "Content-type: text/html\r\n"; 
    $success = mail($webMaster, $emailSubject, $body, $headers); 
    $theResults = <<<EOD 
    EOD; 
    echo "$theResults"; 
?> 
<div class="col-lg-12"> 
    <form name="sentMessage" action="/index.php" method="post" id="contactForm" novalidate > 
     <div class="row"> 
      <div class="col-md-6"> 
       <div class="form-group"> 
        <input type="text" name="iname" class="form-control" 
         placeholder="Your Name *" 
         id="name" 
         required data-validation-required-message="Please enter your name."> 
        <?php $name = $_POST['iname']; ?> 
        <p class="help-block text-danger"></p> 
       </div> 
       <div class="form-group"> 
        <input type="email" name="iemail" class="form-control" 
         placeholder="Your Email *" id="email" 
         required data-validation-required-message="Please enter your email address."> 
         <?php $name = $_POST['iemail']; ?> 
        <p class="help-block text-danger"></p> 
       </div> 
       <div class="form-group"> 
        <input type="tel" name="iphone" 
         class="form-control" 
         placeholder="Your Phone *" id="phone" 
         required data-validation-required-message="Please enter your phone number."> 
         <?php $name = $_POST['iphone']; ?> 
        <p class="help-block text-danger"></p> 
       </div> 
      </div> 
      <div class="col-md-6"> 
       <div class="form-group"> 
        <textarea class="form-control"name="imessage" placeholder="Your Message *" 
         id="message" 
         required data-validation-required-message="Please enter a 
         message.">/textarea> 
        <?php $name = $_POST['imessage']; ?> 
        <p class="help-block text-danger"></p> 
       </div> 
      </div> 
      <div class="clearfix"></div> 
       <div class="col-lg-12 text-center"> 
        <div id="success"></div> 
        <button type="submit" name="isubmit" class="btn btn-xl">Send Message</button> 
       </div> 
      </div> 
     </form> 

這是HTML + PHP

+0

如何發佈數據? – Tushar

+2

等待讓我添加html代碼 –

+0

html代碼加入@Tushar –

回答

0

好了所有的代碼,只要看一眼,有很多錯在這裏。

  • 首先,我不是EOD的忠實粉絲。所以,我改變這只是爲了引用。
  • 您的文本需要縮進,認真,否則你不能讀取它。

讓我先給你的PHP,然後HTML。

<?php 
    print_r($_POST); 

    if (isset($_POST['isubmit'])){ // checking if the submit button variable is there from clicking on the button 
     // mail variables 

     $emailSubject = 'Customer Has a Question!'; 
     $webMaster = '[email protected]'; 
     $name = $_POST['iname']; 
     $email = $_POST['iemail']; 
     $phone=$_POST['iphone']; // please note this variable is not used anymore in the code. You either forgot it in your email, or you dont want anything with this 
     $question = $_POST['imessage']; 

     echo $name; 

     //set up mail body 
     $body = "<br><hr><br>"; 
     $body .= "Name:". $name . "<br>"; 
     $body .= "Email:". $email ."<br>"; 
     $body .= "Questions:". $question ."<br>"; 

     $headers = "From: $email\r\n"; 
     $headers .= "Content-type: text/html\r\n"; 
     $headers .= "MIME-Version: 1.0" . "\r\n"; 

     if (mail($webMaster, $emailSubject, $body, $headers)){ 
      echo "your mail has been send succesfully"; 
     } 
    } else { 
     // this is a test message. Just erase the whole else statement when this all works (for test purposes if the code is read) 
     echo "variables aren't set yet" 
    } 
?> 

如你所見,我對你的代碼做了一些修改。我確保它檢查是否設置了變量。這樣,您可以觸發包含所有變量的郵件。現在每次刷新頁面郵件功能時它都會運行。你不想那樣做。我在郵件功能上做了一個檢查,如果它已經發送了郵件,並且我添加了一個額外的郵件頭。

你應該嘗試做的是將你的輸入sanatize。現在它不會在數據庫中結束,所以它不是一個大問題,但是,如果您想將它發送到您自己的數據庫以供參考,該怎麼辦?它加入了一個名爲mysqli_real_escape_string()功能的一個簡單的問題(這裏asuming你mysqli的工作,如果你對你的網站上運行的數據庫功能,檢查出來,如果你願意。

<form name="sentMessage" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" id="contactForm" novalidate > 
    <div class="row"> 
     <div class="col-md-6"> 
      <div class="form-group"> 
       <input type="text" name="iname" class="form-control" placeholder="Your Name *" id="name" required data-validation-required-message="Please enter your name."> 
        <?php $name = $_POST['iname']; // i leave this here, but i would remove this. It has no purpose, if you want to replace the name in your field, check if variable is set, and put it in the name field of your item ?> 
       <p class="help-block text-danger"></p> 
      </div> 
      <div class="form-group"> 
       <input type="email" name="iemail" class="form-control" placeholder="Your Email *" id="email" required data-validation-required-message="Please enter your email address."> 
        <?php $name = $_POST['iemail']; // i leave this here, but i would remove this. It has no purpose, if you want to replace the name in your field, check if variable is set, and put it in the name field of your item ?> 
       <p class="help-block text-danger"></p> 
      </div> 
      <div class="form-group"> 
       <input type="tel" name="iphone" class="form-control" placeholder="Your Phone *" id="phone" required data-validation-required-message="Please enter your phone number."> 
        <?php $name = $_POST['iphone'];// i leave this here, but i would remove this. It has no purpose, if you want to replace the name in your field, check if variable is set, and put it in the name field of your item ?> 
       <p class="help-block text-danger"></p> 
      </div> 
     </div> 

     <div class="col-md-6"> 
      <div class="form-group"> 
       <textarea class="form-control"name="imessage" placeholder="Your Message *" id="message" required data-validation-required-message="Please enter a message."></textarea> <!-- issue here with wrong closing of text area in html --> 
        <?php $name = $_POST['imessage']; // i leave this here, but i would remove this. It has no purpose, if you want to replace the name in your field, check if variable is set, and put it in the name field of your item ?> 
       <p class="help-block text-danger"></p> 
      </div> 
     </div> 
     <div class="clearfix"></div> 
     <div class="col-lg-12 text-center"> 
      <div id="success"></div> 
      <button type="submit" name="isubmit" class="btn btn-xl">Send Message</button> 
     </div> 
    </div> 
</form> 

此外,我看着你的HTML。關鍵被稱爲indedent。有一些可疑的php放置位置,我沒有得到。echo的名稱。如果他們是跳棋,那很好,在最終產品中刪除它們,如果它們在那裏替換名稱,那麼您需要添加他們不同的東西,我把他們的解決方案放在其中(不確定是否需要它,所以我把解決方案放在答案之外,你需要的代碼已經在這個答案中,你只需要把它們放在一起)

編輯

我只需要添加,以及(如交互設計師),即要求PHONENUMBER不聰明,尤其是當它涉及到轉換(多久客戶將離開的消息)。你希望人們與你聯繫。在網絡上留下你的電話號碼是「個人」的事情,如果有人要求電話號碼,它總會讓人懷疑。人們不知道他們的號碼會發生什麼,所以他們退出了。說明信息被謹慎使用。

我的提示:不要讓手機領域成爲必填字段。只是電子郵件,名稱和消息。

+0

我嘗試完全按照你所說的,但它仍然沒有生成和電子郵件現在仍然消息方面變量還沒有設置,即使我點擊提交按鈕 –

+0

更新了它,我加入了PHP自我命令的行動,我意識到我忘了放在那裏。檢查它現在是否有效。既然您將它提交到同一頁面,action屬性可以省略或空白,但顯然它沒有。你現在想做的是檢查後期變量。我在上面的php代碼中添加了一行,它顯示了post方法所推送的所有變量。看看你是否得到了結果。哦,我看到我在$ _POST變量上犯了一個拼寫錯誤。這可能是你的問題。再次檢查 – Dorvalla