How to transfer full form array to another PHP file?

2

I have this form and I want to pass the array (as it is) $array_dos_pagamentos to recive.php but I can only pass single values. How can I pass the complete array?

<form action='http://www.xxx.com/wp-content/xx/xx/recive.php' method='post'  class="my-form_recibos" target='hidden-form'>
 <label> <input type='checkbox' autocomplete="off" class="" name='enviarmail'    value='yes' >enviar pedido de recibos ?</label>

<input type="hidden" name="result" value="<?php $array_dos_pagamentos); ?>">

  <input type='Submit' value='Salvar'   onclick='saved(<?php echo $fid ?>)' />
</form> 

The array:

Array ( [0] => Array ( [nome] => Claudia Mateus [total] => 20 [email] => [email protected] ) [1] => Array ( [nome] => Joana Gonçalves [total] => 20 [email] => [email protected] ) [2] => Array ( [nome] => Paulo Abreu [total] => 20 [email] => [email protected] ) ) 
    
asked by anonymous 14.01.2016 / 19:25

2 answers

1

There are two ways to do this, which I know of.

Using JSON (quoted by @rray):

  

For the form:

<?
$json = json_encode($array_dos_pagamentos); 
// Isto ira converter a array em json
?>
<input type="hidden" name="result" value="<?= htmlentities($json, ENT_QUOTES, 'UTF-8') ?>">
  

To receive the data:

<?
$array = json_decode($_POST['result'], true);
// Isto irá tornar o JSON em array
?>

Information on link

Using Serialize + HMAC (or AES-CGM):

If you can not use JSON there is a not very recommended alternative, using serialize() , remember to never use serialize() alone, vise documentation .

  

For the form:

<?

$serialize = serialize($array_dos_pagamentos);
$hmac = hash_hmac('sha384', $serialize, 'SUA_SENHA');

$value = $hmac.$serialize;

?>
<input type="hidden" name="result" value="<?= htmlentities($value, ENT_QUOTES, 'UTF-8') ?>">
  

To receive the data:

$value = $_POST['result'];

$hmac = mb_substr($value, 0, 96, '8bit');
$serialize = mb_substr($value, 96, null, '8bit');

if(hash_equals($hmac, hash_hmac('sha384', $serialize, 'SUA_SENHA'))){
    $array = unserialize($serialize);
}

This will prevent the input value from being changed by the user, which is a big problem when using serialize() .

Information on link .

    
14.01.2016 / 20:20
4

In this case you can transform the array into a json and the recive.php return it to its original form.

<input type="hidden" name="result" value="<?php echo json_encode( $array_dos_pagamentos); ?>">

In recive.php, the json_last_error () function detects errors syntax in json, is available in version 5.3 of php and json_last_error_msg () in php5.5

<?php
    $json = json_decode($_POST['result'], true) or die(json_last_error());
    echo '<pre>';
    print_r($json);
    
14.01.2016 / 19:33