zend : populating a checkbox array with $form->populate(); from controller - php

We have a zend form with text input fields and array of checkboxes, as shown below -
<input class="checkbox_Category" type="checkbox" name="tag[]" value="19"> somename1 <br/>
<input class="checkbox_Category" type="checkbox" name="tag[]" value="20"> somename2 <br/>
<input class="checkbox_Category" type="checkbox" name="tag[]" value="21"> somename3 <br/>
and using a
$formObject->populate($formDataArray);
in the controller to populate data in the whole form. All the text input fields seem to populate fine, but the checkboxes don't. Int the $formDataArray, the data for the checkboxes is in the format
[tag] => Array ( [0] => 20 [1] => 19 )
Along with the other form data like - [firstName] => 'somename' etc.
I am not able to figure out the format of the data the form is expecting, in order to get populated with populate();

hi might be problem with the name you have given as array please change like below
<input class="checkbox_Category" type="checkbox" name="tag" value="19"> somename1 <br/>
<input class="checkbox_Category" type="checkbox" name="tag" value="20"> somename2 <br/>
<input class="checkbox_Category" type="checkbox" name="tag" value="21"> somename3 <br/>
Please let me know if i can help you further

Without actually seeing your Zend_Form code, this is very difficult. However, most often, I've seen people mistake the 'checkbox' element with the 'multiCheckbox' element in Zend Framework. I know - it's a bit confusing - but checkbox is a single checkbox with an on/off value. MultiCheckbox handles multiple values - and I think it's what you are trying to accomplish. Let me show you a quick form that will work and generate your HTML above.
class Application_Form_Test extends Zend_Form
{
public function init()
{
$this->setMethod('post');
$multiOptions = array(
19 => 'somename1',
20 => 'somename2',
21 => 'somename3'
);
$this->addElement('multiCheckbox', 'tag', array(
'multiOptions'=>$multiOptions
));
$this->addElement('submit', 'submitbutton');
}
}
Now, if you use something like...
$form->populate($this->getRequest()->getPost());
in your controller, it will populate as expected.
Hope this helps!

Related

How handle multiple inputs from one form in PHP

I'm not sure how to ask this question. It procedural question I believe.
<input type="hidden" name="1[]" value="dummy">
<input type="radio" name="1[]" value="5">
<label> Very Good </label>
<input type="radio" name="1[]" value="4">
<label> Good </label>
<input type="text" name="1[]" size="20">
<br>
<input type="hidden" name="2[]" value="dummy">
<input type="radio" name="2[]" value="5">
<label> Very Good </label>
<input type="radio" name="2[]" value="4">
<label> Good </label>
<input type="text" name="2[]" size="20">
$_POST output:
[1] => Array
(
[0] => Text misc
)
[2] => Array
(
[0] => 5
[1] =>
)
From this I construct and INSERT statement.
INSERT INTO coached_tracked (coached_id, value, note)
VALUES ($key, $value[0], $value[1]);
This is are dynamically generated form inputs. A radio button, text field pair.
How can I handle an occurrence where the radio is not selected and the text field has value, like in the first instance. I want the option of having nothing selected so a default value seems not called for. I tried with both with and without a dummy value (I saw an example suggesting a hidden field as a possible solution.)
Suggestions.
You should not tell the database what ID to use. Let the database itself determine that by using an auto-incremented column.
First, start with a logical input name. Using just numbers is extremely confusing and looking at your code, I have absolutely no idea what you're doing. We also want everything to go into the same PHP $_POST variable to not have to iterate over all possible number cominations. That means we can just iterate over the one single array.
Let's say you're adding a coach to a database, so logically we would start with:
<input name="coach">
Now when we want to add multiple coaches instead of just one, we can use HTML array names, however I would recommend you hard-code them instead of auto-incrementing in your HTML, which should simplify things later on. We also pluralize it to coaches:
<?php
for ($i=0;$i<10;$i++) {
?>
<input name="coaches[<?=$i?>]">
<?php
}
Now if each coach contains a certain properties, let's say name, salary, note, etc, we can add the properties to the input names like so:
<?php
for ($i=0;$i<10;$i++) {
?>
<input name="coaches[<?=$i?>][name]">
<input name="coaches[<?=$i?>][salary]">
<input name="coaches[<?=$i?>][note]">
<?php
}
Then in PHP you just iterate over $_POST['coaches'] and then use the properties for each coach how you wish:
if (isset($_POST['coaches'])) {
foreach ($_POST['coaches'] as $coach) {
$name = $coach['name'];
$salary = $coach['salary'];
$note = $coach['note'];
// Now execute the query:
// INSERT INTO coached_tracked (name, salary, note)
// VALUES ($name, $salary, $note);
}
}
Note: remember to sanitize any user-supplied data by using prepared statements with bound parameters to make sure you're not open to SQL injection attacks.

What changes need to make to the HTML input fields in order to create desired array in PHP?

I've designed one HTML form as follows :
<form action="sample_test.php" method="post">
<input type="text" name="fileName" value="8.png" id="fileName[]">
<input type="text" name="fileLink" value="https://www.filepicker.io/api/file/zZ993JyCT9KafUtXAzYd" id="fileLink[]">
<input type="text" name="fileName" value="2_OnClick_OK.jpg" id="fileName[]">
<input type="text" name="fileLink" value="https://www.filepicker.io/api/file/1w3cKCW1TMmytb7md3XQ" id="fileLink[]">
<input type="submit" name="Submit" value="Submit File">
</form>
Then the code in sample_test.php is as follows :
<?php
print_r($_POST); die;
?>
The output I got is as follows :
Array ( [fileName] => 2_OnClick_OK.jpg [fileLink] => https://www.filepicker.io/api/file/1w3cKCW1TMmytb7md3XQ [Submit] => Submit File )
But this is not the desired output. I want the desired output array to be printed in following manner:
Array
(
[8.png] => Array
(
[0] => https://www.filepicker.io/api/file/zZ993JyCT9KafUtXAzYd
)
[2_OnClick_OK.jpg]
(
[0] => https://www.filepicker.io/api/file/1w3cKCW1TMmytb7md3XQ
)
)
For now I've just demonstrated with two elements only but in real situations hundreds of such elements could present on the form.
So what changes do I need to make in my HTML as well as PHP code? Please help me.
Thanks in advance.
What you ask is impossible by just modifying the HTML code, because you would like a value (of fileName) to become an index in the array you get. That's impossible, the index will always be the name of the input.
However, if you have a look here : POSTing Form Fields with same Name Attribute , you will be able to get arrays of fileName and fileLink, and I'm pretty sure you can do something from there.
A few things wrong, but you are close. Make the name field an array instead of the id - plus your ids need to be unique.
<input type="text" name="fileName[]" value="8.png" id="fileName1">
<input type="text" name="fileLink[]" value="https://www.filepicker.io/api/file/zZ993JyCT9KafUtXAzYd" id="fileLink1">
<input type="text" name="fileName[]" value="2_OnClick_OK.jpg" id="fileName2">
<input type="text" name="fileLink[]" value="https://www.filepicker.io/api/file/1w3cKCW1TMmytb7md3XQ" id="fileLink2">
Not tested, but should do the trick.

codeigniter create array of checked checkboxes

I have php form which has numerous check boxes. I need to store all the selected check boxes in an array and then use them from my controller. So far, in my controller I have
$data['categories']= array($this->input->post('category'))
where my check boxes are called "category". however this method is only storing a single check box value, even when numerous check boxes are selected.
I then intend to pass this array to a model for processing.
Thank you for the help, I appreciate any suggestions.
In your view, use category[] as a name for checkboxes.
Example:
<input type="checkbox" name="category[]" checked> Option 1
<input type="checkbox" name="category[]" checked> Option 2
etc...
In your view
<td><input type="text" name="category[]"/></td>
<td><input type="text" name="category[]"/></td>
<td><input type="text" name="category[]"/></td>
Notice, we added two brackets, to indicate that this is an array.
Then in your Controller you can loop through it or whatever you like.
foreach ( $this->input->post('category') as $category)
{
// some stuff here
}

Looping through multiple POST elements

I am trying to figure out how to loop through multiple POST form data that is dynamically pulled from a database and re-submit the modified data to a different table. For some reason (probably old age) I can't seem to come up with a solution that works.
I am already looping out all the records from one table (call it roster) and need to submit it to another table (call it roster2). The form is something similar to this:
<form name="name" action="form.php" method="post">
<input type="text" name="name1" value="15">
<input type="text" name="attended" value="1">
<input type="checkbox" name="nameid_12" value="12">
<input type="text" name="name2" value="8">
<input type="text" name="attended" value="1">
<input type="checkbox" name="nameid_6" value="6">
</form>
The 'name' and 'nameid' fields will always change and the number of records displayed will always be different (one day it could be 5 and the next 100).
What is the best way to loop through the POST data to submit it to the database keeping all the associations intact?
I am relatively new to working with PHP and I can't seem to figure out a good way to do this.
If you're going to generate them dynamically, I would recommend using PHP to place the ID of each form in the inputs on that form. So your original form would end up with these names:
<input type="text" name="name[12]" value="15">
<input type="text" name="attended[12]" value="1">
<input type="checkbox" name="nameid[12]" value="12">
<input type="text" name="name[6]" value="8">
<input type="text" name="attended[6]" value="1">
<input type="checkbox" name="nameid[6]" value="6">
Then your arrays will have keys corresponding to their form's ID. The array structure looks like this.
Array (
[name] => Array ( [12] => 15, [6] => 8 )
[attended] => Array ( [12] => 1, [6] => 1 )
[nameid] => Array ( [12] => 12 [6] => 6 )
)
Now we need to figure out which ids are actually present today. The array_keys() function generates an array of keys from any source array. Keys will be the same for each of the three elements, so I arbitrarily take the keys from [name].
$id_array = array_keys($_POST['name']);
Then, to access each element of the POST array, we'll use a foreach.
foreach ($id_array as $id) {
//assign variables
$name = $_POST['name'][$id];
$attended = $_POST['attended'][$id];
$nameid = $_POST['nameid'][$id];
//store
//Using whichever database style you like. I prefer PDO.
}
You can loop through the fields and retain their keys like so:
foreach ($_POST as $field_name => $field_value) {
// Storage
}

PHP form processing with variableform inputs

Got a form that has the option to add many inputs for ordering pictures via picture number.
In theory a customer could order 1 picture or 100, how would I go about the PHP.
As coding up to 100 $_POST[] for each possible field seems crazy as each of the added fields as it's own unique NAME attr using jQuery.
Anyone got any bright ideas?
Using field names that end in square brackets will cause PHP to create the entries as an array:
<input name="foo[]" value="foo" />
<input name="foo[]" value="bar" />
<input name="foo[]" value="moo" />
<input name="foo[]" value="cow" />
will produce the following: $_REQUEST['foo'] (or $_POST['foo']/$_GET['foo']) is an array like this:
array(
0 => 'foo',
1 => 'bar',
2 => 'moo',
3 => 'cow'
);
You could try something like
for ($i=0;$i<100;$i++){
if (isset($_POST['picture'.$i])){
// Do something
} else {
break;
}
}
You can do something like this
<input type="checkbox" value="picnumber" name="pictures[]" />
<?php
$pics = $_POST['pictures']; // here you will get an array of values of the selected images
?>

Categories