PHP分组形式,有些有额外的数据

发布于 2024-09-07 17:29:49 字数 2743 浏览 1 评论 0原文

有一个通过电子邮件发送的带有复选框的表格。

某些复选框具有用于输入文本的可选字段。就像“其他和更多信息”一样,

我如何将人们输入的文本附加到该选项的那些复选框中。

这是一组。

<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 = "[email protected]"; 
    $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>";
      }
    }
}

Have a form that is emailed with checkboxes.

Some checkboxes have an optional field to enter text. Like "other and more info"

How can I attach the text people entered to those check boxes that this option.

This is one group.

<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>

And my sendmail.php is like:

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

    $to = "[email protected]"; 
    $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!";
}
?>

UPDATED - Have tried recommendation but did not work. I can't get the value and text field together.

Tried this but still no results.

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

UPDATE 2 - Got it to work. Changed my textfield name to "otherwhereTXT" and by doing the following:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

琴流音 2024-09-14 17:29:49

您可以使用...

$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 中)并将它们添加到正文消息中。

You would use...

$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'];
}

This loops through each of your checked values (checkboxes only show up in $_POST if they've been checked) and adds them to the body message.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文