If you want to use a PHP session, I would take advantage of AJAX. You'll need to create a file to handle the array as you create it. Here's a simple example.
Your first page
<?php
//start the PHP session to save your array in
session_start();
?>
<script type="text/javascript">
var myArray = [];
<?php
if(isset($_SESSION['myArray'])) {
foreach($_SESSION['myArray'] as $item){
// prepopulate the session array from PHP
echo "myArray[] = ".$item.";";
}
}
?>
function addItemToArray(){
var addvalue = document.getElementById("txtMyText").value;
myArray.push(addvalue);
if (window.XMLHttpRequest){ var xmlhttp=new XMLHttpRequest(); }else { var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
document.getElementById("myDiv").innerHTML += xmlhttp.responseText .", ";
}
}
xmlhttp.open("GET","save_array.php?newValue="+,true);
xmlhttp.send();
}
</script>
<div id="show_my_array">
<?php
//show array saved from last time
if(isset($_SESSION['myArray'])) {
foreach($_SESSION['myArray'] as $item){
// prepopulate the session array from PHP
echo $item.", ";
}
}
?>
</div>
<input type="text" id="txtMyText">
<button type="button" id="myButton" onclick="addItemToArray()">Add to Array</button>
save_array.php
<?php
session_start();
if(!isset($_SESSION['myArray'])){
$_SESSION['myArray'] = array();
}
$_SESSION['myarray'][] = $_GET['newValue'];
echo $_GET['newValue'];
?>