Search code examples
phparraysnew-window

PHP: open another PHP with array value


Is it possible to open another PHP file (print_array.php) passing $array via PHP function?

HTML:

<form method=post>
<input type="checkbox" name="array[]" value="111">
<input type="checkbox" name="array[]" value="222">
<input type="checkbox" name="array[]" value="333">
<button type="submit" name="action" value="print">Print</button>
<button type="submit" name="action" value="delete">Delete</button>
<button type="submit" name="action" value="add">Add New</button>
</form>

array[] is all checked, so, $array value is 111, 222, and 333.

Then, PHP function:

switch ($action) {
  case 'print': printing($_post['array']); break; /* open new window */
  case 'add': break; /* same window */
  case 'delete': break; /* same window */
  default: break;
}

function printing($array) {
  /* open print_array.php in a new window showing $array value */
}

And then, only if action=print, open print_array.php in a new window with $array value.

if (is_array($array)) {
  print_r($array);
} else {
  ...
}

The result in print_array.php would be

Array ( [0] => 111 [1] => 222 [2] => 333 )

I don't think header("Location: print_array.php") can pass array value. Is there any good way to open another PHP page in a new window passing array value?


Solution

  • To open the window using Javascript, use window.open() when the page loads.

    To get the data to the new page, use _SESSION variables. When you get to the script with the swtich, use:

    start_session(); 
    $_SESSION['data'] = $_POST['array'];
    

    On the new window (after you use header() to get to it, use:

    start_session();
    $array = $_SESSION['data'];
    

    You will have access to the entire array in the new window.