在php(智能)中发布数组

时间:2010-09-18 16:49:43

标签: php arrays forms smarty

我正在尝试在表单中发布多个答案(使用复选框)。问题是选择你可用的月份,这是我的代码...

$smarty->assign('month', array( '1' => 'January',  '2' => 'February',   '3' => 'March',  '4' => 'April',   '5' => 'May',  '6' => 'June',   '7' => 'July',  '8' => 'August',   '9' => 'September',  '10' => 'October'
                                                    ,   '11' => 'November',  '12' => 'December' )    );

然后我用来发布值的行如下......

<span style="color:#000000;">'($_POST['monthsAvailable']) .'</span>

然而,当我收到电子邮件时,它只是读取“数组”。我想知道发布数组的正确格式是什么?任何建议都非常感谢!

2 个答案:

答案 0 :(得分:0)

如果$_POST['monthsAvailable']然后打印它将只打印一个数组。定义类似这样的函数:

function printMonths($array){
  global $smarty;
  $out = '';
  foreach ($monthIndex in $array){
    $out .= $smarty['month'][$month].' ';
  }
  return $out;
}

...

<span style="color:#000000;">'(printMonths($_POST['monthsAvailable'])) .'</span>

如果$ _POST ['monthsAvailable']包含已经过月份的名称,请使用explode()

答案 1 :(得分:0)

尝试使用Smarty::foreach

服务器端php

<?php
$months = array( 
  '1' => 'January',
  '2' => 'February',
  '3' => 'March',
  '4' => 'April',
  '5' => 'May',
  '6' => 'June',
  '7' => 'July',
  '8' => 'August',
  '9' => 'September',
  '10' => 'October',
  '11' => 'November',
  '12' => 'December' );

$smarty->assign('months', $months);
$smarty->assign('monthsAvailable', $_POST['monthsAvailable']);
?>

Smarty模板

<ul>
{foreach from=$monthsAvailable item=mIndex}
    <li>{$months[$mIndex]}</li>
{/foreach}
</ul>

如果将$ _POST ['monthsAvailable']定义为月份索引数组,它应该有效

$_POST['monthsAvailable'] = array('1','10','12');