2017-03-16 4 views
1

私はかなりPHPに新しいです、私は電子メールの提出(ニュースレター)フォーム作業をしようとしています。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. 
} ?> 

問題: は、私は次のHTMLフォーム持ってIを押すと を提出ボタンを押すと、それは私のアドレスに電子メールを送信しますが、私は次の電子メール受け取る:

から: へ:[email protected] DATをE:木、2017年3月16日12:23 被写体に:フォームの提出 暗号化:標準(TLS)は、あなたが見ることができるように

詳細はこちらから:フィールド$ことを意味し、空でありますfrom = $ _POST ['email']は入力フィールドに何を入力してもNULLを返します。

私は間違っていますか?

また、私はもう一つ質問があります:どのように私は、フォームの送信からのメッセージは、代わりに "/メールhandler.phpをリンクし、エコーために私をリダイレクトする、フォームの下に小さなツールチップとして表示されます

することができますがあなたはニュースレターを購読しました。ありがとうございます、まもなくご連絡させていただきます。

ありがとうございます!

+1

、あなたは '名=「」'ではなく 'IDを=「」'必要 - エラー報告とあなたをオンにします」 d "Undefined index"のエラーが発生しました – Epodax

+0

[HTML Form POSTがnullの可能性のある重複](http://stackoverflow.com/questions/21450748/html-form-post-is-null) – ConquerorsHaki

答えて

3

入力フィールドは、あなたが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())"); 
関連する問題