2016-12-03 3 views
-2

私は顧客が詳細を記入できる連絡フォームを持っています。提出されると、フォームは情報をPHPスクリプトに渡してHTMLスクリプトに書式設定し、ビジネス用メールに電子メールで送信します。空白のフォームは提出し続ける(HTML、PHP)

唯一の問題は、空メールを無作為に受信し続けることです。明らかに、PHPスクリプトが起動され、電子メールが送信されていますが、私はなぜそれがわかりません。

フォームには必須フィールドがありますので、誰かが空白で提出しようとしたとしても、フォームは必須となります。これらのメールを受信すると、少なくとも何かがあるはずです。

私は、PHPスクリプトにいくつかの追加検証を追加して、必要な値が空であるか不足しているかどうかを確認することを考えましたが、このフォームはこれを処理します。

何が起こっているか知っていますか?それはおそらく私が見落とした単純なものです。

<?php 

$name = $_POST['name']; 
$email = $_POST['email']; 
$budget = $_POST['budget']; 
$timeframe = $_POST['timeframe']; 
$desc = $_POST['desc']; 

//Send HTML formatted email 
$send = mail("[email protected]", 

"You have received an enquiry", 

    "<html> 
    <body> 
     <h3>Here is the information for the enquiry: </h3> 
     <br> 
     <p>Name: $name</p> 
     <p>Email: $email</p> 
     <p>Budget: $budget</p> 
     <p>Timeframe: $timeframe</p> 
     <br> 
     <p>Description of enquiry:</p> 
     <p>$desc</p> 
    </body> 
    </html> 
    ", "Content-type: text/html; charset=iso-8859-1"); 

if ($send) 
{ 
    header("Location:index.html"); 
} 
else 
{ 
    echo "An error occurred. Please return to the contact form and try again."; 
} 

?>

ここに行く
+1

さて、いくつかのコードは良いでしょう。しかし、一般的には、メールを送信する前にフォームデータに基づいてメッセージを作成していることを確認してください。 –

+0

私の悪い!はい、そうです。 PHPはPOST値を取得して変数に格納し、次にメール関数でこれらの変数を指定します。 – NathanJrT

+0

すべてのブラウザがフォームの 'required'属性を尊重しているわけではありません。 (私はサファリがそうでないと言いたい) – HPierce

答えて

0

、これを試してみてください! :)

<?php 
    //Check the request method 
    if ($_SERVER["REQUEST_METHOD"] == "POST") { 
     //Start by importing and sanitizing the fields 
     $name = sanitize($_POST["name"]); 
     $email = sanitize($_POST["email"]); 
     $budget = sanitize($_POST["budget"]); 
     $timeframe = sanitize($_POST["timeframe"]); 
     $desc = sanitize($_POST["desc"]); 

     //Make sure the fields are populated 
     if (empty($name)) {die("The &#39;Name&#39; field was not populated. Please return to the contact form and try again.");} 
     if (empty($email)) {die("The &#39;Email&#39; field was not populated. Please return to the contact form and try again.");} 
     if (empty($budget)) {die("The &#39;Budget&#39; field was not populated. Please return to the contact form and try again.");} 
     if (empty($timeframe)) {die("The &#39;Time Frame&#39; field was not populated. Please return to the contact form and try again.");} 
     if (empty($desc)) {die("The &#39;Description&#39; field was not populated. Please return to the contact form and try again.");} 

     //Everthing is fine, so send the email! 
     $send = mail("[email protected]", "You have received an enquiry", 
     "<html> 
      <body> 
       <h3>Here is the information for the enquiry: </h3> 
       <br> 
       <p>Name: $name</p> 
       <p>Email: $email</p> 
       <p>Budget: $budget</p> 
       <p>Timeframe: $timeframe</p> 
       <br> 
       <p>Description of enquiry:</p> 
       <p>$desc</p> 
      </body> 
      </html> 
      ", "Content-type: text/html; charset=iso-8859-1"); 

     if ($send) { 
      header("Location: index.html"); 
     } else { 
      die("An error occurred. Please return to the contact form and try again."); 
     } 
    } else { 
     die("An error occurred. Please return to the contact form and try again."); 
    } 

    function sanitize($data) { 
     $data = trim($data); 
     $data = stripslashes($data); 
     $data = htmlspecialchars($data, ENT_QUOTES); 
     return $data; 
    } 
?> 
関連する問題