Home>

The languages ​​used are PHP and MySQL.

For example, on the screen of A, it is written as follows, and $transcation is an associative array.

<? php
  foreach ($transcation_data as $transcation) {
?>
 <tr>
  <td><? = $transcation ['date']?></td>
  <td><? = $transcation ['account_name']?></td>
  <td><? = $transcation ['account_sub_name']?></td>
  <td><? = $transcation ['transaction_amount']?></td>
  <td><? = $transcation ['transaction_remark']?></td>
 </tr>
<? php
  }
?>

And I want to bring the associative array $transcation_data used in foreach to the next screen B as it is.
I wrote it as follows.

<input type = "hidden" name = "transcation_data" value = "<? = $Transcation_data?>">


On the B screen,

$_POST ['transcation_data'];


However, the contents were empty. I tried it in the session, but it didn't work.

$_SESSION ['transcation_data']


I would appreciate it if you could teach me.
Thank you.

Postscript: I'm sorry I didn't have enough information.
1. Enter the following items on the form screen and send it to screen 2 by POST.

2. The received POST is created in a loop to create a multidimensional array and displayed on the screen.
Since $transcation_data used here contains all the data,
I want to pass it to the next screen.

for ($i = 0;$i<5;$i ++) {
  if (! empty ($_ POST ['date'. $I])) {
    $account_data = explode (",", $_ POST ['account_id'. $I]);
    $account_id = $account_data [1];
    $account_name = $account_data [0];
    $account_sub_data = explode (",", $_ POST ['account_sub'. $I]);
    $account_sub_id = $account_sub_data [0];
    $account_sub_name = $account_sub_data [1];
    $transcation_data [] = [
      'date' =>$_POST ['date'. $I],
      'account_id' =>$account_id,
      'account_name' =>$account_name,
      'account_sub_id' =>$account_sub_id,
      'account_sub_name' =>$account_sub_name,
      'transaction_amount' =>$_POST ['transaction_amount'. $I],
      'transaction_remark' =>$_POST ['transaction_remark'. $I]
    ];;
  } else {
    continue;continue;
  }
}


Write the following in the form tag and send POST

<input type = "hidden" name = "transcation_data" value = "<? = $Transcation_data?>">

3. The last is the file to be registered in the DB, but the data is not received here.

$regi1 = $_POST ['regi1'];// Judge registration or update
 if ($_ POST ['transaction_data']) {
    $transaction_data = $_POST ['transaction_data'];
    }
 if ($_ POST ['transaction_id']) {
  $transaction_id = $_POST ['transaction_id'];
  } // Required for updates
 require_once ('../ db/db.php');
// Registration
if ($regi1 == 1) {
  $sql ='INSERT INTO transaction (account_id, account_sub_id, transaction_amount, transaction_remark, transaction_date, transaction_name) VALUES (?,?,?,?,?,?)';
  $stmt = $db->prepare ($sql);
  foreach (array_filter ($transcation_data) as $transcation) {
    $stmt->bindValue (1, $transcation, PDO :: PARAM_INT);
    $stmt->bindValue (2, $transcation, PDO :: PARAM_INT);
    $stmt->bindValue (3, $transcation, PDO :: PARAM_INT);
    $stmt->bindValue (4, $transcation, PDO :: PARAM_STR);
    $stmt->bindValue (5, $transcation, PDO :: PARAM_STR);
    $stmt->bindValue (6, $_SESSION ["u_id"], PDO :: PARAM_INT);// Only this will carry around the session from login
    $stmt->execute ();
  }
}

Thanking you in advance.

  • Answer # 1

    Premise

    At firstPHP form value inheritancePlease check around.

    Even if I try to display the array as it is with a short tag, it is not displayed

    When handling as an HTML element such as hidden, it is necessary to check and debug the HTML source displayed in the browser.

    If you want to use session, please check the PHP manual first to learn the behavior and how to use it.

    When sending with hidden

    How do I use an array in an HTML form to submit the array in a form? It should be sent in the form of.

    <input name = "AnotherArray []" />
    <input name = "AnotherArray []" />
    <input name = "AnotherArray [email]" />
    <input name = "AnotherArray [phone]" />


    On the page that receives the form submitted in

    var_dump ($_ POST);


    Then you can understand the behavior.
    In this case, it looks like a deeper array.

    <input name = "AnotherArray [0] []" />
    <input name = "AnotherArray [0] []" />
    <input name = "AnotherArray [0] [email]" />
    <input name = "AnotherArray [0] [phone]" />
    <input name = "AnotherArray [1] []" />
    <input name = "AnotherArray [1] []" />
    <input name = "AnotherArray [1] [email]" />
    <input name = "AnotherArray [1] [phone]" />


    As a matter of fact, if you check how it is stored in $_POST, you can see the HTML shape you should aim for.

    As a flow

    First, write the HTML directly and check that the array is as desired on the receiving side.

    Reproduce the HTML created in 1 with PHP (in this case,foreach as $key =>$transcationIt seems that you need to specify the key like that)
    Is the flow

    When using session

    PHP manual
    After actually trying and understanding the sample of

    On the page displaying the formsession_start ()Then store the value in $_SESSION

    Even on the receiving pagesession_start ()Then inherit the value from $_SESSION
    I will implement it like that.

  • Answer # 2

    Please process in session