0

チェックボックスが電子メールで送信されるフォームを用意します。

一部のチェックボックスには、テキストを入力するためのオプションのフィールドがあります。「その他の詳細情報」のように

このオプションのチェックボックスに入力したテキストを添付するにはどうすればよいですか。

これは1つのグループです。

<form action="sendemail.php" method="POST" name="Contact Form">
  <div><input type="checkbox" class="checkbox" name="check[]" value="Word of Mouth?">Word of Mouth?</div>
  <div><input type="checkbox" class="checkbox" name="check[]" value="Have you seen our Sign?  Where?">Have you seen our Sign? Where?<input type="text" style="float:right; width:150px;" name="sign" /></div>
  <div><input type="checkbox" class="checkbox" name="check[]" value="Other.">Other. <input type="text" style="float:right; width:300px;" name="other" /></div>
<p><input type="submit" value="Submit" name="submit"></p>

そして私のsendmail.phpは次のようなものです:

<?php
if(isset($_POST['submit'])) {

    $to = "myemail@mysite.com"; 
    $subject = "Contact Form";
    $firstname_field = $_POST['firstname'];
    $lastname_field = $_POST['lastname'];

    foreach($_POST['check'] as $value) {
        $check_msg_0 .= "  $value\n";
    }

    $body = "From: $firstname_field $lastname_field\n Learned about us from: \n $check_msg_0\n \n ";

    echo "Data has been submitted to $to!";
    mail($to, $subject, $body);

} else {
    echo "Nope didn't work!";
}
?>

更新-推奨を試みましたが、機能しませんでした。値とテキストフィールドを一緒に取得できません。

これを試しましたが、まだ結果がありません。

<form action="sendemail.php" method="POST" name="Contact Form">
  <div><input type="checkbox" class="checkbox" name="check[]" value="Word of Mouth?">Word of Mouth?</div>
  <div><input type="checkbox" class="checkbox" name="check[]" value="Other.">Other. <input type="text" style="float:right; width:300px;" name="otherwhere" /></div>
<p><input type="submit" value="Submit" name="submit"></p>

if (count($_POST['check']) > 0) {
    foreach ($_POST['check'] as $value) {
        if (isset($_POST['check']) && $_POST['check'] == 'Other') {
            $check_msg_0 .= $value.": ".$_POST['otherwhere']."<br>";
        }
        else { 
            $check_msg_0 .= $value."<br>";
        }
     }
}

更新2-動作するようになりました。テキストフィールド名を「otherwhereTXT」に変更し、次のようにしました。

if (count($_POST['check']) > 0) {
     foreach ($_POST['check'] as $value) {
         if ($value == 'other') {
        $check_msg_0 .= "Other: ".$_POST['otherwhereTXT']."<br>";
         } else { 
        $check_msg_0 .= $value."<br>";
      }
    }
}
4

1 に答える 1

0

あなたが使うだろう...

$body = "From: $firstname_field $lastname_field\n Learned about us from: \n";

if (count($_POST['check'] > 0 {
    foreach ($_POST['check'] as $value) {
        $body .= $value."\n";
    }
}
if (strlen($_POST['other']) > 0) {
    $body .= "\n\nOther: ".$_POST['other'];
}

これにより、チェックされた各値がループされ(チェックボックスは、チェックされている場合にのみ$ _POSTに表示されます)、本文メッセージに追加されます。

于 2010-06-30T19:51:45.137 に答える