簡體   English   中英

PHP分組表格,有些包含額外的數據

[英]PHP grouped form and some has extra data

具有通過電子郵件發送的帶有復選框的表格。

一些復選框有一個可選字段,用於輸入文本。 像“其他信息”

我如何將輸入的文本附加到該選項的復選框中。

這是一組。

<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>";
      }
    }
}

你會用...

$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中顯示復選框),並將它們添加到正文消息中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM