1

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?

1
  • New window conditionally means that normally Javascript is involved not PHP. So I'd say the simple answer is "No." without going into too much detail. Commented Jul 23, 2012 at 15:47

3 Answers 3

1

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.

1
  • I used session to keep array values and javascript to open new window.. worked well :) Thank you! Commented Jul 24, 2012 at 4:58
0

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.

0

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']; ?>">

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.