ted
ted

Reputation: 4181

How to pass associative array to a php page in POST?

I am trying to use Magento 1.9 XmlConnect module to save the billing address function. In xml connect there is an action to do this, saveBillingAddressAction. In savebillingaddressAction method one line is trying to access an array from the POST variables like following -

$data = $this->getRequest()->getPost('billing', array());

How I can pass an array from client side to server side in POST variable so that billing param have the array with the needed data?

Magento repository - CheckoutController.php.

Upvotes: 4

Views: 1677

Answers (1)

Samuel Cook
Samuel Cook

Reputation: 16828

You can create arrays out of form elements using square brackets [].

<input type="hidden" name="billing[]" value="billing-info1">
<input type="hidden" name="billing[]" value="billing-info2">
<input type="hidden" name="billing[]" value="billing-info3">

This will return a zero based array (ie 0=>'billing-info1',1=>'billing-info2', etc).

If you'd like to use an associative array you just need to create a key:

<input type="hidden" name="billing[key0]" value="billing-info1">
<input type="hidden" name="billing[key1]" value="billing-info2">
<input type="hidden" name="billing[key2]" value="billing-info3">

Then your return will be something like:

'key0' => 'billing-info1',
'key1' => 'billing-info2',
'key2' => 'billing-info3'

Upvotes: 6

Related Questions