簡體   English   中英

HTML / PHP新聞稿表格

[英]HTML/PHP Newsletter form

我對PHP相當陌生,並且正在嘗試使電子郵件提交(新聞通訊)表單有效。 我有以下HTML表單:

<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>

為此,我用以下代碼創建了一個mail-handler.php文件:

<?php 
$to = "horia.imperial@gmail.com"; // 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.
} ?>

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

從:到:horia.imperial@gmail.com日期:2017年3月16日,星期四,下午12:23主題:表單提交加密:標准(TLS)了解更多

如您所見,from:字段為空,這意味着無論我在輸入字段中輸入什么,$ from = $ _POST ['email']都會返回NULL。

我究竟做錯了什么?

另外,我還有一個問題:

如何使來自表單提交的消息顯示為表單下方的小工具提示,而不是將我重定向到link / mail-handler.php並回顯“您已訂閱我們的新聞通訊。謝謝,我們會盡快與您聯系。 ”

提前致謝!

輸入字段需要一個name屬性,以便隨表格一起發送

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

您沒有設置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())");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM