How to transfer a full array of the form to another PHP file?

Asked

Viewed 762 times

2

I have this form and I want to pass the array (as is) $array_dos_pagamentos to the recive.php but I can only pass unique values. How can I pass the full 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] ) ) 
  • Whence $array_dos_pagamentos it would not be better to separate myself from other fields?

  • 1

    You must have a better solution than that. It would be nice to explain the initial need, because in principle, if the data has to be reused, you should probably store it on the server side even temporarily. Or at least keep the structure of the original form on the next page.

2 answers

4

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

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

In recive.php it looks like this, the function json_last_error(), detect syntax errors in json, is available in php version 5.3 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);

1


There are two ways I know.

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 in http://php.net/manual/en/function.json-encode.php

Using Serialize + HMAC (or AES-CGM):

If you cannot use JSON there is an alternative not very recommended, using serialize(), remember to never use serialize() alone, aimed at 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 the serialize().

Information in http://php.net/manual/en/function.serialize.php.

  • Failure to escape the serialized data before putting in the value=. The serialize can have a diversity of characters that would break the form/input. Starting with quotes.

  • True, I had forgotten it. I will edit.

  • thanks with Serialize worked to see. JSON did not give for some reason :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.