Forum Moderators: coopster

Message Too Old, No Replies

pass array via form submit

pass php array of objects to file with form submit

         

ron_isc

5:48 pm on Jun 24, 2003 (gmt 0)

10+ Year Member



Does anyone know if it is possible to pass an array of objects from one php file to another. The container class is in a seperate file and the submitting php file constructs the array of objects. Is it possible to pass the entire array with the submit action?
The array of objects is created in this function.
function loadProducts($number)
{
$index = 0;
$result_array = array();
$result = query_db ("SELECT * FROM item WHERE orderid = $number");
while($row = mysql_fetch_array($result))
{
$result_array[$index] = new products;
$nodeId = $row['nodeid'];
// This variable will be pulled from radio button box on form when completed
$status = "Value from radio check box";
//$contents[$index]->set_status($status);
$result_array[$index]->set_index($index);
$result_array[$index]->set_vendor(prodSelect("vendor", $nodeId));
$result_array[$index]->set_name($row['title']);
$result_array[$index]->set_options(validateString($row['options']) . "<br>" . cleanString($row['addons']));
$result_array[$index]->set_date($row['added']);
$result_array[$index]->set_sku($row['sku']);
$result_array[$index]->set_quantity($row['qnty']);
$result_array[$index]->set_status($status);
$index = $index + 1;
}
return $result_array;
}
$contents = loadProducts($orderId);
When the array was passed using this form field the contents of the array were null.
<input type=hidden name=new_contents value="<?=$contents?>">
Any help would be greatly appreciated, thanks!

jatar_k

12:25 am on Jun 25, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



<input type=hidden name=new_contents value="<?=$contents?>">

this won't work, you can't echo an array or object like that. I assume all of this info gets output as html? If so the values that are output to the browser can be written into the form individually and submitted but I am not 100% sure if I completely understand.

As an aside want a quick piece of shorthand?

$index = $index + 1;
is the same as
$index++; // I use this for counters
and the same as
$index += 1; // I use this to add int values to themselves

for strings you can do

$strvar .= "more strings";

jatar_k

12:31 am on Jun 25, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



also Welcome to WebmasterWorld ron_isc
in case it hasn't been offered yet

grahamstewart

12:37 am on Jun 25, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can pass an array via <input type="hidden" but you will have to do it for each array element..

e.g.


<input type="hidden" name="user[firstname]" value="Graham">
<input type="hidden" name="user[lastname]" value="Stewart">
<input type="hidden" name="user[gender]" value="M">

The submit page will now have access to the array as $_POST['user'] (or $_GET['user'] if you use a GET method on the form).

And of course you can do multi-dimensional arrays like this too..

e.g.


<input type="hidden" name="product[1][name]" value="Widget">
<input type="hidden" name="product[2][name]" value="Grommit">

ruserious

7:25 am on Jun 25, 2003 (gmt 0)

10+ Year Member



Passing data between pages through the user/browser via hidden-forms is easily manipulated by the users. Double check that you sanitize all the data that you are getting via GET/POST/COOKIE.

An alternative way would be, to generate any only pass a session_id to the user. Then in your db/flatfile you save the data associated with that session_id.

vincevincevince

11:23 pm on Jun 25, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



either use sessions, or (as i recommend) write your own session-like script ;-)