-1

I couldn't find replies to this question that worked for me. I need to add values to the array via input field. I tried with SESSION but it also didn't work. When inspecting array with print_r every time array started again with index [0] thus overwriting former user input, thus not adding new value to the end. But adding new values into array standard way, by declaring new value works perfectly:

$a = array();
$a[] = 5;
$a[] = 'Hello';

etc.

Can you give me some hint?

Thanks in advance Danny

Arun
  • 3,640
  • 7
  • 44
  • 87
Danny
  • 1
  • 2

1 Answers1

0

Here you go. First start a session. Then create the array if not exists in session array. Add the value of the inputName named field. The second one is checking, is the value is already in the array, and if yes, do not add again.

Later, you can access $_SESSION['myArray'] on any page.

Do not forget to start the session on every page by session_start(); where you use it.

<?php 
session_start();
//If session array not exists create one
if (empty($_SESSION['myArray'])) {
    $_SESSION['myArray'] = array();
}
if (!empty($_GET['inputName'])) {
    $_SESSION['myArray'][] = $_GET['inputname'];
}

//If want to check is the value of inputName exists, and add only once:
if (!empty($_GET['inputName']) && !in_array($_GET['inputName'], $_SESSOION['myArray'])) {
    $_SESSION['myArray'][] = $_GET['inputname'];
}
?>    
HTML starts here.
vaso123
  • 12,347
  • 4
  • 34
  • 64