2016-07-08 8 views
1

私はシンプルなPHP購読フォームを書いています。実行すると何もしません。PHPベースの形式ではコンテンツが送信されませんか?新鮮な目を探しています

<form class='subscribe' method='post' action='<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>'> 
       Subscribe for occasional updates and reminders! 
       Email: <input type='text' class='input' name='email' id='email' > 
       <!--There will be some type of CAPTCHA here--> 
        <label for='Subscribe'> 
         <input value='Subscribe' type='submit' id='subscribebutton' class='input' name='subscribebutton'> 
        </label> 
       <span class='error'><?php echo $inError ?> </span> 
       <?php 
         $email = ''; 
         $inError = ''; 
         if($_SERVER['REQUEST_METHOD'] == 'POST') { 
         if(empty($_POST['email'])) 
          $inError = "You must provide an email to register!"; 
         elseif(stripos($_POST['email'],'@') !== TRUE || stripos($_POST['email'],'.') !== TRUE) 
          $inError = "Please provide a properly formatted email to register."; 
         else 
         { 
          $email = sanatizeInput($_POST['email']); 
          $emailFile = fopen('emails.txt','a') or die ('Sorry. Subscriptions are disabled for the time being.'); 
          fwrite($emailFile, $email); 
         } 
         } 
         function sanatizeInput($data) { 
          $data = trim($data); 
          $data = stripslashes($data); 
          $data = htmlspecialchars($data); 
          return $data; 
         } 

       ?> 
ノーコンテンツや不適切な形式のコンテンツでフォームを送信する場合には、エラーを投げていない

は、それがファイルを書いていない、私が送信ボタンを押したとき、それは正確に何もしないだと思わ (:PHPは、このようになりますフィールドをクリアしてフォームを提出するように見えますが)。
これはおそらく単純なものですが、私の目はそれを完全に見逃しています。
ご協力いただければ幸いです。
ありがとうございます。 ザック

+1

'stripos(...)!== TRUE'は決して動作しません。 striposは、見つかった文字列の整数インデックス、またはブール値falseを返します。 ** TRUEを返すことは決してできないので、あなたの 'if()'はいつも成功するでしょう。 –

答えて

2

フォームタグを適切に閉じて、ページのPHPコードを上に移動します。このような何か、それは動作するはずです。

<?php 
$email = ''; 
$inError = ''; 
if ($_SERVER['REQUEST_METHOD'] == 'POST') { 
    if (empty($_POST['email'])) 
     $inError = "You must provide an email to register!"; 
    elseif (stripos($_POST['email'], '@') !== TRUE || stripos($_POST['email'], '.') !== TRUE) 
     $inError = "Please provide a properly formatted email to register."; 
    else { 
     $email = sanatizeInput($_POST['email']); 
     $emailFile = fopen('emails.txt', 'a') or die('Sorry. Subscriptions are disabled for the time being.'); 
     fwrite($emailFile, $email); 
    } 
} 

function sanatizeInput($data) { 
    $data = trim($data); 
    $data = stripslashes($data); 
    $data = htmlspecialchars($data); 
    return $data; 
} 
?> 
<form class='subscribe' method='post' action='<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>'> 
    Subscribe for occasional updates and reminders! 
    Email: <input type='text' class='input' name='email' id='email' > 
    <!--There will be some type of CAPTCHA here--> 
    <label for='Subscribe'> 
     <input value='Subscribe' type='submit' id='subscribebutton' class='input' name='subscribebutton'> 
    </label> 
    <?php if(!empty($inError)){ ?><span class='error'><?php echo $inError; ?> </span> <?php } ?> 
</form> 
+0

あなたは速いです。今すぐ私の答えを貼り付けようとしていた。 もう1つ留意すべき点は、isset($ inError)句を追加して、存在する場合にのみエコーします。私はテストしたときにそれから未定義のエラーが発生しました。 – Conceptz

+0

うん。 $ inErrorを表示する前に必要なチェックを追加しました。 –

+0

ありがとう!すべてが今働きます。私はこれに比較的新しいので、本当に助けに感謝します! – user3213722

関連する問題