2017-03-16 17 views
1

我對PHP很新,我正在嘗試提交電子郵件提交(簡報)表單。 我有以下的HTML表單:HTML/PHP簡報格式

<form class="form-inline" action="mail-handler.php" method="post"> 
     <div class="col-sm-5 col-sm-offset-2 col-md-4 col-md-offset-3">      
      <label class="sr-only" for="email">Email</label> 
       <input type="text" id="email" placeholder="Your email address.." /> 
     </div> 
     <div class="col-sm-3 col-md-2"> 
       <input type="submit" name="submit" class="btn btn-submit" value="Submit"> 
     </div> 
</form> 

而對於它,我創建了一個郵件handler.php文件與下面的代碼:

<?php 
$to = "[email protected]"; // this is your Email address 
$from = $_POST['email']; // this is the sender's Email address 
$subject = "Form submission"; 
$subject2 = "You have just subscribed to the Plantsnap newsletter!"; 
$message = $from . " just subscribed for your Newsletter" ; 
$message2 = "Here is a copy of your message " . $first_name . "\n\n" . $_POST['message']; 

//Headers 
$headers .= "From: <".$from. ">"; 
$headers2 = "From:" . $to; 
if(isset($_POST['submit'])){  
mail($to,$subject,$message,$headers); 
mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender 
echo "You have subscribed to our newsletter. Thank you, we will contact you shortly."; 
// You can also use header('Location: thank_you.php'); to redirect to another page. 
// You cannot use header and echo together. It's one or the other. 
} ?> 

問題: 當我按下提交按鈕,它會發送一封郵件到我的地址,但我收到了以下電子郵件:

來源: 到:[email protected] DAT E:星期四,2017年3月16日在下午12時23分 主題:表單提交 加密:標準(TLS)瞭解更多

正如你所看到的,來自:字段爲空,這意味着,$ from = $ _POST ['email']無論我在輸入字段中輸入什麼,都會返回NULL。

我在做什麼錯?

另外,我有一個問題:

我怎樣才能使消息從表單提交出現的形式下,一個小提示,而不是重定向我鏈接/郵件handler.php和回聲「您已訂閱我們的通訊,謝謝,我們會盡快與您聯繫。「

在此先感謝!

+1

您還沒有命名的輸入字段,你需要'name =「」'和'不是ID =「」' - 打開錯誤報告和你」 d得到「未定義索引」的錯誤 – Epodax

+0

[HTML表單POST爲空]的可能重複(http://stackoverflow.com/questions/21450748/html-form-post-is-null) – ConquerorsHaki

回答

3

的inputfield需要name屬性爲它與您的形式

<input type="text" name="email" id="email" placeholder="Your email address.." />

1

您沒有設置name屬性來發送。試試這個:

<input type="text" name="email" placeholder="Your email address.." />

而只是一個小建議:如果你只發送了自己的一些地址可能會丟失的電子郵件地址。插入電子郵件到emails數據庫表是這樣的:

$email = $_POST['email']; 
mysqli_query($conn, "INSERT INTO emails (email, time) VALUES ($email, CURRENT TIME())");