Question

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?

Was it helpful?

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.

OTHER TIPS

Keep in mind that PHP doesn't open windows and such. What you are looking to do sounds like it needs to be achieved in HTML and/or javascript.

Best way would be to use javascript:

Firs declare your checkboxes to call a javascript function when clicked:

<input type="checkbox" name="array[]" onclick="myfunction(111)" value="111">

Then include the following in your page:

 <script type="text/javascript">
      function myfunction(number) {
          if (number == 111) {
              location.href="mypage.html";
          }
      }
 </script>

You can also use $_POST or $_GET for this if you strictly want to go with PHP.

At the server end you can do switch($_GET['array']) and do your stuff. This way you would have to change your form declaration to

<form method=post action="<?php echo $_SERVER['PHP_SELF']; ?>">

or

<form method=post action="<?php echo $_SERVER['PHP_SELF']; ?>">
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top