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:
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))."\" />";
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
Execute a PHP Script from Another PHP Script
PHP MySQL_Real_Escape_String() -> Stripslashes() Leaving Multiple Slashes
Fetching Mail from a Pop3 Server Using PHP
PHP 5.4 Pdo Could Not Connect to MySQL 4.1+ Using the Old Insecure Authentication
Create New Product Attribute Programmatically in Woocommerce
PHP - Time Remaining Until Specific Time from Current Time of Page Load
How to Give Container as Argument to Services
How to Force an Image Download in the Browser
How to Use Two Submit Buttons, and Differentiate Between Which One Was Used to Submit the Form
Differencebetween Null and Empty
PHP Rename Array Keys in Multidimensional Array
How to Connect PHP with Microsoft Access Database