PHP 后复选框帮助

发布于 2024-09-09 04:14:43 字数 551 浏览 5 评论 0原文

我无法接收在作为表单一部分的复选框字段中选中的项目列表。

我有一个带有几个复选框的 HTML 表格:

HTML

 <input type="checkbox" name="carry[]" value="1" />
 <input type="checkbox" name="carry[]" value="2" />
 <input type="checkbox" name="carry[]" value="3" />

PHP - 这是我用来将表单发布到电子邮件地址的方法

foreach($_POST as $key => $val) {

$body .= $key . " : " . $val . "\r\n";

,我在电子邮件中得到的值为:“carry:Array” - 不是选定的实际值,如何处理选定的复选框数组。 理想情况

下,我想要:“携带:1; 2; 3"(不带引号)

I"m having trouble receiving a list of items that are checked in a field of checkboxes which are part of a form.

I have an HTML table with a few checkboxes:

HTML

 <input type="checkbox" name="carry[]" value="1" />
 <input type="checkbox" name="carry[]" value="2" />
 <input type="checkbox" name="carry[]" value="3" />

PHP - this is what I'm using to post the form to an email address

foreach($_POST as $key => $val) {

$body .= $key . " : " . $val . "\r\n";

I get the value in my email as: "carry: Array" -- not the actual values that are selected. How do I handle an array of checkboxes selected in a form and post it?

Ideally, I would want: "carry: 1; 2; 3" (without the quotes)

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

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

发布评论

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

评论(3

魄砕の薆 2024-09-16 04:14:43

您可以检查该值是否是数组并以不同的方式处理它:

foreach($_POST as $key => $val) {
  if (is_array($val)) {
    $body .=  $key . " : " . implode(",",$val) . "\r\n";
  } else {
   $body .= $key . " : " . $val . "\r\n";
  }
}

You can check if the value is an array and handle it differently:

foreach($_POST as $key => $val) {
  if (is_array($val)) {
    $body .=  $key . " : " . implode(",",$val) . "\r\n";
  } else {
   $body .= $key . " : " . $val . "\r\n";
  }
}
清晨说晚安 2024-09-16 04:14:43

如果你想要字符串 '1; 2; 3',您应该将数组中的项目连接在一起:

$carry= implode('; ', $_POST['carry']);

但是,如果数组中的任何项目本身有分号,那么这样做自然会导致不明确的结果。

要迭代 post 数组,允许其任何成员成为数组:

foreach($_POST as $key=>$val) {
    if (is_array($val))
        $val= implode('; ', $val);
    $body.= "$key: $val\r\n";
}

或者,如果您不需要对精确格式进行太多控制并且调试转储就可以了(但您需要看到的不仅仅是无用的字符串 'Array'

$body= var_export($_POST, TRUE);

If you want the string '1; 2; 3', you should join together the items in the array:

$carry= implode('; ', $_POST['carry']);

However doing so will naturally cause ambiguous results if any of the items in the array themselves have a semicolon in.

To iterate over the post array allowing any of its members to be arrays:

foreach($_POST as $key=>$val) {
    if (is_array($val))
        $val= implode('; ', $val);
    $body.= "$key: $val\r\n";
}

Or, if you don't need so much control over the exact formatting and a debugging dump is fine (but you need to see more than just the useless string 'Array':

$body= var_export($_POST, TRUE);
走过海棠暮 2024-09-16 04:14:43

它打印进位:数组,因为这正是它的本质。您需要对其进行循环(第一个循环中的另一个循环)才能访问其中的值:

foreach($_POST as $key => $val) {
    if($key == 'carry') {
        foreach($val as $carry) {
            $body .= $carry;
        }
    }
    else {
        $body .= $key . " : " . $val . "\r\n";
    }
}

这完全未经测试,但希望逻辑是合理的:)

It's printing carry: Array because that's exactly what it is. You need to loop over it (another loop inside the first) to access the values inside:

foreach($_POST as $key => $val) {
    if($key == 'carry') {
        foreach($val as $carry) {
            $body .= $carry;
        }
    }
    else {
        $body .= $key . " : " . $val . "\r\n";
    }
}

That's completely untested but hopefully the logic is sound :)

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