0

i am trying to pass an array in form using a hidden element in php. I first create the array then then are foreach which runs through each element in the loop, next I echo a html type with type hidden that will be in my form. Last I want to print out the hidden form element using print_r

Here is my code

<?php

$postvalue=array("a","b","c","e", "f", "g");
foreach($postvalue as $key => $value)
{
  echo '<input type="hidden" name="result[]" value="'. $value. '">';
}



print_r($_POST['result']);

<?

When I run this code I get the following error from the print_r

Notice: Undefined index: result

How do I fix this?

JazZ
  • 4,469
  • 2
  • 20
  • 40
Becky
  • 1
  • 1
  • `$_POST['result']`? from where this came suddenly in your code? – Alive to die - Anant Feb 27 '17 at 15:27
  • `$_POST` data is only set after a form has been submitted by e.g. clicking on a submit button. – apokryfos Feb 27 '17 at 15:28
  • What do you do? I cant see an `
    ` here? And there is no magic that makes a form from one `` !?
    – JustOnUnderMillions Feb 27 '17 at 15:28
  • Also _Life Cycle of a HTML Form_ `$_POST` wont exist untill the form has been Submitted. That `print_r()` need to be placed in a test that checks if the form is being submitted OR is just being sent for the first time to the browser – RiggsFolly Feb 27 '17 at 15:30
  • You are receiving that error because in this given code the `$_POST['result']` does not exist yet, like others have mentioned the form would have to post this to the page first for it to exist – happymacarts Feb 27 '17 at 15:33
  • oh right so how do I print the input then? – Becky Feb 27 '17 at 15:46

0 Answers0