Form elements into $_POST arrays - html

I'm probably being stupid, because its Friday afternoon, but I just can't work this out.
What I want is to have a form where users can add infinite "records". To do this they click an "add record" button. This runs javascript which adds a new row (tr) to a table within the <form>.
This new row has three input fields.
What I want is for this to be sent to the post variable in a format such as (but not exact if it can't be done, but there's a better way):
$_POST['record'] = array(
array(
"input1" => "value",
"input2" => "value",
"input3" => "value"
),
array(
"input1" => "value",
"input2" => "value",
"input3" => "value"
),
array(
"input1" => "value",
"input2" => "value",
"input3" => "value"
),
);
I know you can get arrays by using the name like so:
<input type="text" name="record[]" />
But this is only 1 input element. Is there any way to get a structure like above with 3 elements?
Thank you.

You can't easily get what you're looking for exactly, but you can use name="record[input1][]" (and input2 etc.) and the result is:
$_POST['record'] = array(
"input1"=>array(
"value", "value", "value"
),
"input2"=>array(
"value", "value", "value"
),
"input3"=>array(
"value", "value", "value"
)
);
You could then transform it into your desired format like so:
$out = Array();
foreach(array_keys($_POST['record']['input1']) as $i) {
foreach($_POST['record'] as $k=>$v) {
$out[$i][$k] = $v[$i];
}
}

I think you are on the right track. using name=record[]. You'll get something like
$_POST['record'] = array(
"record" => array(
"value",
"value",
"value"
),
"field2" => array(
"value",
"value",
"value"
),
"field3" => array(
"value",
"value",
"value"
)
);
So to get each row, you'd use
$cnt = count( $theArray['record'] );
for ($x=0; $x<$cnt; $x++){
echo $theArray['record'][$x];
echo $theArray['field2'][$x];
echo $theArray['field3'][$x];
}

I just tried --
<form action="" method="post">
<input type="text" name="record['set1'][]" />
<input type="text" name="record['set1'][]" />
<input type="text" name="record['set1'][]" />
<input type="text" name="record['set2'][]" />
<input type="text" name="record['set2'][]" />
<input type="text" name="record['set2'][]" />
<input type="text" name="record['set3'][]" />
<input type="text" name="record['set3'][]" />
<input type="text" name="record['set3'][]" />
<input type="submit" value="submit" />
</form>
Output --
Array
(
[record] => Array
(
['set1'] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
['set2'] => Array
(
[0] => 4
[1] => 5
[2] => 6
)
['set3'] => Array
(
[0] => 7
[1] => 8
[2] => 9
)
)
)
Is this how you wanted it?

I think this would be more clearly for you,
$record = array();
foreach($_POST['record'] as $val){
$record[] = $val;
}
print_r($record);

Related

symfony numberType does not work in [mapped=>false]

I add in a form a mapped => 'false' fields. I assign the NumberType::class but when it's rendered it's created with type="text" parameter. This is the code in the controller:
->add('debe', NumberType::class,
array( "attr" => array("pattern" => "[0-9]*", "size" => 10),
"mapped" => false ) )
The HTML code generated is this:
<input type="text" id="scg_debe" name="scg[debe]" required="required" pattern="[0-9]*" size="10" />
Thanks for any help.
So, I assume you want type="number", which is a type introduced by html5.
So essentially, you have to set the html5 option on your form field.
->add('debe', NumberType::class, [
'attr' => [
'pattern' => '[0-9]*',
'size' => 10,
],
'mapped' => false,
'html5' => true, // <-- this one
])

checkboxlist not rendering checkbox checked correctly

I have a two array that I'm going to include them into a checkbox,
echo Html::checkboxList('item', $selectedItem, $dataItem, [
'item' => function($index, $label, $name, $checked, $value) {
return "<label class='col-md-2'>
<input type='checkbox' {$checked} name='{$name}' value='{$value}'>
<span>{$label}</span>
</label>";
}
]);
I see in code in browser like this :
<label class="col-md-2">
<input 1="" name="item[]" value="1" disabled="" type="checkbox">
<span>Login</span>
</label>
The checked render into 1="" .
Please advise.
UPDATE
$dataItem
E:\wamp64\www\yii_tresnamuda\modules\it\views\request\preview.php:128:
array (size=6)
1 => string 'Login' (length=5)
2 => string 'Printer' (length=7)
3 => string 'Monitor' (length=7)
4 => string 'Computer' (length=8)
5 => string 'Network' (length=7)
6 => string 'Lain Lain' (length=9)
$selectedItem
E:\wamp64\www\yii_tresnamuda\modules\it\views\request\preview.php:129:
array (size=2)
2 => int 2
1 => int 1
The problem is in your {$checked} element of your item template. Try this:
echo Html::checkboxList('item', $selectedItem, $dataItem, [
'item' => function($index, $label, $name, $checked, $value) {
return "<label class='col-md-2'>
<input type='checkbox' name='{$name}' value='{$value}' ".($checked ? 'checked' : '').">
<span>{$label}</span>
</label>";
}
]);
Also, I think you have to change $selectedItem specification form [2=>2, 1=>1] to just [2,1]

How to remove 'label' decorator from Zend 2 form

I have the simple Radio element:
$form->add([
'name' => 'account_type',
'type' => 'Zend\Form\Element\Radio',
'options' => [
'label' => 'Account type',
'value_options' => [
1 => 'Employer',
2 => 'Performer'
]
]
]
);
But in my view I get this html:
<div class="zf-form-el-account_type">
<label for="account_type">Account type</label>
<div class="zf-radio">
<label>
<input type="radio" name="account_type" class="account_type" value="1">Employer
</label>
</div>
<div class="zf-radio">
<label>
<input type="radio" name="account_type" class="account_type" value="2">Performer
</label>
</div>
</div>
How I can to remove this empty label wrapper around an radio element? Or how I can to insert some tag after radio element? Thanks.
I extended standard view helper:
<?php
namespace Application\Form\View\Helper;
use Zend\Form\View\Helper\FormRadio;
use Zend\Form\Element\Radio as RadioEl;
class FormRadioElement extends FormRadio
{
protected function renderOptions(RadioEl $element, array $options, array $selectedOptions, array $attributes)
{ ...
... and set template in helper like:
$template = '%s%s';
Then I declared it in my bootstrap:
public function getViewHelperConfig() {
return [
'invokables' => [
'formRadioHelper' => 'Application\Form\View\Helper\FormRadioElement',
]
];
}
... and called in my view like:
<?php echo $this->formRadioHelper($form->get('account_type'))?>

yii2 ActiveForm numeric textfield

I've created an ActiveForm using yii2 like this:
<?=$form->field($item, 'finalPrice', [
'options' => [
'tag' => 'div',
'class' => '',
],
'template' => '<span class="col-md-2 col-lg-2"><label class="control-label">Final item price</label>{input}{error}</span>'
])->textInput([
// ** i want numeric value **
])->label(false)?>
and it rendered a result of:
<span class="col-md-2 col-lg-2"><label class="control-label">Final item price</label><input type="text" id="item-finalprice" class="form-control" name="Item[finalPrice]"><p class="help-block help-block-error"></p></span>
now i want to make it < input type="number" .. and not text.. (so user could change value using browser up/down buttons). is there any way to do it?
You can use ->textInput(['type' => 'number'] eg :
<?=$form->field($item, 'finalPrice', [
'options' => [
'tag' => 'div',
'class' => '',
],
'template' => '<span class="col-md-2 col-lg-2"><label class="control-label">Final item price</label>{input}{error}</span>'
])->textInput([
'type' => 'number'
])->label(false)?>
Try this . it worked for me
<?= $form->field($model, 'amount')->textInput(['type' => 'number']) ?>
Field like Phone Number/Membership No etc, some time we allow user only to enter numeric input in a text field. In such case applying pattern match rule work great for me.
Simply set a rule in the model class and you are done.
public function rules()
{
return [
...
[['contactno'], 'string', 'max' => 25],
[['contactno'], 'match' ,'pattern'=>'/^[0-9]+$/u', 'message'=> 'Contact No can Contain only numeric characters.'],
...
];
}
<?= $form->field($model, 'code')->textInput(['type'=>'number']) ?>

Codeigniter - Input post array insert into mysql database

I dont understand how to exactly insert a row into a MySQL table. If I use my code, I add a new row for every post. Rather than that, I want only one row with all the values.
Here is the code sample.
HTML:
echo form_open('account/update');
echo "<p><label for='gender'>Gender</label>
<input type='text' name='gender' id='gender' value='".$gender."' />
</p>
<p>
<label for='age'>Age</label>
<input type='text' name='age' id='age' value='".$age."' />
</p>
<p>
<label for='bio'>Biografie</label>
<input type='text' name='bio' id='bio' value='".$bio."' />
</p>
<p>
<label for='skills'>Skills</label>
<input type='text' name='skills' id='skills' value='".$skills."' />
</p>
<p><input type='submit' value='Save' /></p>";
echo form_close();
Controller:
function update()
{
$userid = $this->session->userdata("userid");
$datanew = array(
'userid' => $this->session->userdata("userid"),
'gender' => $this->input->post('gender'),
'age' => $this->input->post('age'),
'bio' => $this->input->post('bio') ,
'skills' => $this->input->post('skills')
);
$this->session->set_userdata($data);
$this->load->model('model_account');
$this->load->model("model_user");
$this->model_account->profile_insert($datanew);
$this->load->view("change_avatar");
redirect("account/show/".$this->session->userdata("userid"));
}
Model:
function profile_insert($datanew)
{
$this->db->insert('profile', $datanew);
}
I get 5 rows if I submit the HTML form.
This works for me:
I Set the userid to unique and made a if statement in the controller ti switch between a insert and update function.
Controller:
function update()
{
$datanew = array(
'userid' => $this->session->userdata("userid"),
'gender' => $this->input->post('gender'),
'age' => $this->input->post('age'),
'bio' => $this->input->post('bio') ,
'skills' => $this->input->post('skills')
);
$exists = $this->db->select('profile')->where('userid', $this->session->userdata("userid"));
if($exists)
{
$this->session->set_userdata($datanew);
$this->load->model('model_account');
$this->load->model("model_user");
$this->model_account->profile_update($datanew);
$this->load->view("change_avatar");
redirect("account/show/".$this->session->userdata("userid"));
}
else
{
$this->session->set_userdata($datanew);
$this->load->model('model_account');
$this->load->model("model_user");
$this->model_account->profile_insert($datanew);
$this->load->view("change_avatar");
redirect("account/show/".$this->session->userdata("userid"));
}
}
model:
function profile_insert($datanew)
{
$this->db->insert('profile', $datanew);
}
function profile_update($datanew)
{
$this->db->update('profile', $datanew);
}
Thanks for helping me