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

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.

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'

Related

Magento 2 form mini filter where category ID in array

I am trying to use the following method to filter the category id on my mini form submit
<input type="hidden" name="cat" value="1">
But this only allows to filter by 1 category id, I need to be able to pass in an array of category ID's to the mini form. Is there anyway I can use a similar method to filter the search results by an array of category id's? Or can anyone point me in the right direction on how to achieve this?
You can use serialize() and base64_encode().
$cat_ids = array(1, 2, 3);
$post_cat_ids = base64_encode(serialize($cat_ids));
// Input field
<input type="hidden" name="cat" value="<?php echo $post_cat_ids; ?>">
On server side you can get back array:
$cat_ids = unserialize(base64_decode($_POST['cat']));
print_r($cat_ids);

Yii - cannot define a safe array of attributes for form submit

I have a PHP form that need to submit a array of numbers, what we have in view:
<input type="text" id="ProductForm_sizeobj_1" name="ProductForm[sizeobj[1]]" value="13">
<input type="text" id="ProductForm_sizeobj_2" name="ProductForm[sizeobj[2]]" value="13">
<input type="text" id="ProductForm_sizeobj_3" name="ProductForm[sizeobj[3]]" value="13">
And I define in form class:
public $sizeobj = array();
public function rules() {
return array(
array('/** other attributes **/, sizeobj', 'safe')
);
}
Since "Sizeobj" is a dynamic attribute and the size will growth more then 3, therefore I use array. However after form submitted the error throw as follow:
Failed to set unsafe attribute "sizeobj[1" of "ProductForm".
I believe I might using the wrong method to setup array attribute, or wrong rule, any advice? I'm new to Yii, any help is appreciated.
Use name="ProductForm[sizeobj][1]" instead of name="ProductForm[sizeobj[1]]"

How to send Jqxgrid in a form without using hidden textbox?

My CI view consists of a grid and data is added dynamically to this grid.This is saved to a database when a save button is clicked..
Here is the screenshot of the view.
[IMG]http://i40.tinypic.com/16a7dhl.png[/IMG]
When I submit the form,Grid data is first stored in an array and then array elements are joined together with a seperator in between them into a string.This string is stored in a hidden textbox and is submitted along with the form.After submission,in the controller they are seperated again & stored in the database.I have read that this method is prone to error.
Is there a better way to send array of data in a table to the controller than the above method? I have used Jqxgrid.
Use the jqxGrid's "getrows" to get all records as an Array.
You can use an array for the names of the fields.
<input type="hidden" name="field_name[]" value="foo">
<input type="hidden" name="field_name[]" value="bar">
$post = $this->input->post();
extract($post);
foreach($field_name as $k => $v){
$this->db->insert('tablename', array('fieldname' => $v));
}

Creating Link to Same Page with Request Parameters in Symfony

How might one use the url_for or link_to helpers to create a link to the same page passing along all GET parameters?
I have a search page which accepts multiple parameters as filters to hide certain results. For example, search.html?query=abc&people=1&groups=0 would search for all items containing 'abc' that aren't groups. I would therefore like a link which toggles these filters, passing the rest of the parameters unchanged to the current page. In the above example, links would be created to search.html?query=abc&people=1&groups=1 and search.html?query=abc&people=0&groups=0.
I am wondering if there is any way to specify 'this' as a route, such that if the route or page changed, the code wouldn't have to.
Moreover, how can one pass all parameters to the helper? $sf_params can be used to access all the parameters, and the 'query_string' property can be passed to the helper, but is there any method of combining the two short of creating the string manually?
GET parameters can be passed to a link_to method as an array in the third argument, or url_for in the second. This is illustrated as follows:
link_to('Text to display', 'routename', array('group' => 0, 'people' => 1), $link_attributes);
url_for('routename', array('group' => 0, 'people' => 1));
This is hidden in the source code. Here, $params is the list of GET parameters, and $options HTML attributes to add to the tag.
function link_to2($name, $routeName, $params, $options = array())
function url_for2($routeName, $params = array(), $absolute = false)
The $sf_params function can be converted to a suitable array for passing with getAll(). See the documentation here. Note that this is different from previous versions of Symfony. Also be aware you might have to call getRawValue() from within a template:
$params = $sf_params->getRawValue()->getAll();
$sf_context->getInstance()->getRouting()->getCurrentRouteName() can then be passed as $routeName to get the current route. Thanks to Tom for this
put vars in hidden form data types to pass them on
<form action="page.php" method="GET">
<input name="people" type="hidden" value="<?php echo htmlspecialchars($_GET['people'], ENT_QUOTES); ?>">
<input name="groups" type="hidden" value="<?php echo htmlspecialchars($_GET['groups'], ENT_QUOTES); ?>">
<input name="query" type="text" value="some_data">
<input name="send" type="submit" value="send">
</form>

Add many select elements with [] in name

I want to be able to add to Zend_Form many Zend_Form_Element_Select.
I've got some loop in My_Form_Selects extends Zend_Form with
$element = $this->createElement('Select', 'element[]');
$this->addElement($element);
but it creates only one select element (Zend_Form ignores [] in element's name).
How should I do this ?
Have you tried:
$foo = new Zend_Form_SubForm();
$foo->setElementsBelongTo('foo')
->setElements(array(
'bar' => 'text',
'baz' => 'text'
));
echo $foo;
which results in HTML something like:
<input type="text" name="foo[bar]" id="foo.bar" value="" />
<input type="text" name="foo[baz]" id="foo.baz" value="" />
via
From the manual:
Zend_Form::setIsArray($flag): By setting the flag TRUE, you can indicate that an entire form should be treated as an array. By default, the form's name will be used as the name of the array, unless setElementsBelongTo() has been called. If the form has no specified name, or if setElementsBelongTo() has not been set, this flag will be ignored (as there is no array name to which the elements may belong).
You may determine if a form is being treated as an array using the isArray() accessor.
Zend_Form::setElementsBelongTo($array): Using this method, you can specify the name of an array to which all elements of the form belong. You can determine the name using the getElementsBelongTo() accessor.

Categories