Php, Pass Array Through Post

Passing an array to another page (Form)

i'm glad @ksealey pointed out a more proper method of doing this, but for the sake of answering the question...the reason it's not working is that the serialize alone is not enough to prevent the invalid html. see result of what the serialize leaves in the html:

Sample Image

so you need to be sure the html you produce is valid. you might use encoding like base64 to produce safe html:

echo  " <form id=\"my_form\" action=\"\" method=\"post\"";
echo "enctype=\"multipart/form-data\">";
echo "<input type=\"hidden\" name=\"id\" value=\"10\">";
echo "<input type=\"hidden\" name=\"input_name\" ";

echo "value=\"".base64_encode(serialize($my_array))."\" />";

Sample Image

then you can just add the decode to your output:

$passed_array = unserialize(base64_decode($_POST['input_name']));
print_r($passed_array);

Post Data PHP array in an array

DeliveryAddress is an array of objects (once JSON encoded)

So if you want to post data according to the JSON you wrote as example then the PHP array has to be built this way:

$postData = array(
'deliveryAddress' => array(

array (
'ID'=> '5',
'address'=> 'example@example2.com',
'method'=> 'EMAIL',
'checkbox'=> true,
'flashlight'=> false
),

array (
'ID'=> '7',
'address'=> 'example45@example3.com',
'method'=> 'EMAIL',
'checkbox'=> true,
'flashlight'=> false
)

)
);

Note that on the "PHP side" deliveryAddress is now an array of associative arrays (that once json_encoded will turn in an array of objects).

PHP pass array to Rscript

This seems to be a problem with converting $tr and $ck to strings. Lacking experience in php i base my experience on other languages, and a few other questions.

When executing from the terminal/cmd the arguments should all be strings. Following the example from an answer here you could likely use something similar to (untested!)

$ck=["WT1","WT2"];
$tr=["Al1","Al2"];
exec('Rscript getdata.R "' . implode(",", $ck) . '" "' . implode(",", $tr) . '"');

Note that this will then return the input as a string within R, which will then have to be converted back to vectors.

Passing a multidimensional array through hidden input via POST

You just need to encode array to json and decode it on action page

echo '<input type="hidden" name="itemsArr" value="'.json_encode($itemsArr). '">';

And at your action Page just decode it

$itemsArr = json_decode($_POST['itemsArr'],true)


Related Topics



Leave a reply



Submit