views:

55

answers:

2

Hey,

I'm trying to post multiple answers(using checkboxes) in a form. The question is pick what months your available, here is my code...

$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' )    );

Then the line I use to post the values is as follows...

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

However when I recieve the email it just reads "Array". I was wondering what is the correct format for posting arrays? Any advice is most appreciated!

A: 

if $_POST['monthsAvailable'] then printing it will just print an array. Define a function something like this:

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

...

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

If $_POST['monthsAvailable'] contains the names of the months already, just use explode().

fredley
A: 

Try to use Smarty::foreach

Server side 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 template

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

It should work in case the $_POST['monthsAvailable'] defined as an array of months indexes

$_POST['monthsAvailable'] = array('1','10','12');
Igor
Thanks for help Igor. Will try that now.
Jason