0

I am getting all types of errors on the following code. I want to post data from my form and store in a $_SESSION array for future processing

Illegal string offset 'SurveyDate'
Illegal string offset 'Income'

<input class="formFields" type="date" id="txtDateOfSurvey" name="mycensus[0][SurveyDate]"
<input class="formFields" type="numeric" id="txtIncome" name="mycensus[0][Income]"

<?php
    session_start();

    if( !isset($_SESSION['mycart2']))
    {
        $_SESSION['mycart2'] = array();
    }
    $_SESSION['mycart2'] = array_merge($_SESSION['mycart'], $_POST['mycensus']);


    foreach($_SESSION['mycart2'] as $v)
    {
        echo $v['SurveyDate'] . ' was born on ' . $v['Income'] . '<br>';
    }

?>

I want the array mycart2 to contain all the entries that I enter on my form.

JustCarty
  • 3,839
  • 5
  • 31
  • 51
sra2786
  • 35
  • 4
  • Dump `$_SESSION['mycart2']` and see what the output looks like. It looks as though it is returning a string and therefore `$v` is each letter within the string. – JustCarty Feb 19 '19 at 13:46
  • 2
    Possible duplicate of [Illegal string offset Warning PHP](https://stackoverflow.com/questions/9869150/illegal-string-offset-warning-php) – JustCarty Feb 19 '19 at 13:46
  • the array_merge seems a bit pointless .. why not just set `$_SESSION['mycart2']` to equal `$_POST['mycensus']`? – treyBake Feb 19 '19 at 14:29

1 Answers1

0
<?php
  session_start();
  if(is_array(mycensus)){
    $survey_date = mycensus[0]['SurveyDate'];
   $Income = mycensus[0]['Income'];
}
  ?>

<input class="formFields" type="date" id="txtDateOfSurvey" name="survey_date" value="<?php echo $survey_date; ?>">
<input class="formFields" type="numeric" id="txtIncome" name="income" value="<?php echo $Income; ?>">
<?Php


    if( !isset($_SESSION['mycart2']))
    {
        $_SESSION['mycart2'] = array();
    }
    $_SESSION['mycart2'] = array_merge($_SESSION['mycart'], $_POST['mycensus']);


    foreach($_SESSION['mycart2'] as $v)
    {
        echo $v['SurveyDate'] . ' was born on ' . $v['Income'] . '<br>';
    }

?>
kalaivanan
  • 63
  • 8