在使用$ _POST单击检查按钮后,使用$ _POST方法以前设置的数组变量将丢失

问题描述:

please forgive my confusing title, here is my problem:

  1. set an array variable through $_POST
  2. check which button has been clicked and process the $_POST variable

What is in my code:

<?php
...
$user = array_filter(array_map('array_filter', $_POST['user']));
...

$submit = isset($_POST['button']) ? trim($_POST['button']) : '';
   if ($submit == 'Confirm') {

        ...do something with $user;

   } else if ($submit == 'Cancel') {

        ...do something else with $user;

   }
?>

It appears that when the page is first loaded, $user has been set correctly, however, once the "Confirm" button is clicked, the $name array is lost and cannot be processed. Any idea of how to resolve this will be much appreciated! Many thanks.

an Idea, you need to store it in session. it seems you get $_POST['user'] from previous page/request. variable $_POST only used for passing variable between page, It will not exist anymore if you reload the page.

do it like:

$user = $_SESSION['user'] = array_filter(array_map('array_filter', $_POST['user']));