2017-04-15 4 views
0

私はPHPフォームを作成していますが、通常の方法でチェックボックスを処理しています。 しかし、このコードでは常に問題は何ですか?checboxをコンタクトフォームで処理する

<form method="post" action="php/form.php"> 
    <div class="form-check pull-left"> 
    <label class="form-check-label"> 
    <input class="form-check-input" type="checkbox" value="yes" name="check1"> 
    Option one is this and that&mdash;be sure to include why it's great 
    </label> 
</div> 
</form> 

if (isset($_POST['check1'])) { 
    $check1 = "yes"; 
} else { 
    $check1 = "no"; 
} 
$body="check1: $check1"; 
mail('[email protected]', $subject, $body) 

私はいつもあなたが入力を提出していない場合、これは正常ですノー

+0

試してみてください:if(isset($ _ POST ['check1'])&& $ _POST ['check1'] == 'yes') – Scaffold

+0

フォームのフルコードを投稿してください。 –

+0

@Scaffoldの最初の条件はすでにfalseを返しています。次に、もう1つのAND条件を追加して、どのように条件を真にすることができますか? –

答えて

0

するとページが最初にロード、何ゆえ$_POSTが空と$_POST['check1'] == NULLあり、掲載されていません。

ロジックに基づいて、この場合、check1noを設定し、電子メールを送信します(投稿されていない場合でも)。

フォーム

<button type="submit" name="submit">Submit POST</button> 

submitボタンを追加し、すべて一緒に$_POST['submit']

if (isset($_POST['submit'])) 

の存在を確認することによって行うことができる あなたが何かを確実にしなければならない

が掲載されている 、:

<form method="post"> 
    <div class="form-check pull-left"> 
     <label class="form-check-label"> 
      <input class="form-check-input" type="checkbox" value="yes" name="check1"> 
      Option one is this and that&mdash;be sure to include why it's great 
     </label> 
     <button type="submit" name="submit">Submit POST</button> 
    </div> 
</form> 

<?php 

if (isset($_POST['submit'])) { 
    if (isset($_POST['check1'])) { 
     $check1 = "yes"; 
    } else { 
     $check1 = "no"; 
    } 
    $body = "check1: $check1"; 
    mail('[email protected]', $subject, $body) 
} 

このように、ema ilは、ユーザーが実際にフォームを送信したときにのみ送信され、ページが読み込まれるたびに送信されることはありません。

0

でメールを受信します。 $ _POSTが空でないかどうかをチェックしてメール関数をラップします。

はこれを試してみてください:

<form method="post"> 
    <div class="form-check pull-left"> 
    <label class="form-check-label"> 
    <input class="form-check-input" type="checkbox" value="yes" name="check1" checked> 
    Option one is this and that&mdash;be sure to include why it's great 
    </label> 
</div> 
<input type="submit" value="ok"> 
</form> 
<?php 

if (isset ($_POST) && !empty ($_POST)) 
{ 
    if (isset($_POST['check1'])) { 
    $check1 = "yes"; 
    } else { 
    $check1 = "no"; 
    } 
    $body="check1: $check1"; 
    mail('[email protected]', $subject, $body) 
} 
関連する問題