1
votes

Usually, I always used CakePHP form helper to list down select box with options.

I would like to know,if I can create it manually(I mean without CakePHP form helper) ?

Example: Using cakephp form helper to create select box

<?php echo $this->Form->input('product_name',array('options'=>$myoptions));?> 

But I want to do something like below

<select name="data[Product][product_name]" id="product">
 <options value="1">One</options>
<options value="2">Two</options>
<options value="3">Three</options>
</select>

Any ideas? Thanks CakePHP 2.5.7

--EDIT--

I want to clone multiple select box that will follow CakePHP form structure.

Okay the problem start here. By default I'd use CakePHP form helper to draw the select box and their options.

I wrote this kind of code:

<?php echo $this->Form->input('Redemption.0.type_id',array('class'=>'form-control','id'=>'selectProduct','label'=>false,'empty'=>'Choose Type','options'=>$types));?>

It will return in HTML structure like below:

<select name="data[Redemption][0][type_id]" class="form-control" id="selectProduct">
<option value="">Choose Type</option>
<option value="1">Toner</option>
<option value="2">Ink Catridge</option>
</select>

I want to clone the existing select box above to new select box but with different name value like this:

<select name="data[Redemption][1][type_id]" class="form-control" id="selectProduct">
    <option value="">Choose Type</option>
    <option value="1">Toner</option>
    <option value="2">Ink Catridge</option>
    </select>

Notice that integer value on name="data[Redemption][1][type_id]" has increased.That is what I wanted.

But now,I've no idea to do cloning with data changing as stated. I have try several jquery method such as .clone() .append() but it's not work as the options data inside the select box become redundant.

Can you please enlighten me how to duplicate/clone select box in CakePHP?

Thanks. CakePHP 2.5.7

2
Why do you want to do this? Of course it is possible, but unless you've found a particular limitation of the FormHelper there is little reason to manually do this. - drmonkeyninja
because I want to add multiple select field dynamically ,let say I want products field can be increased when user clicking 'add more' button while options data in the select box are the same. - Malik Perang
Unless I've misunderstood your comment it sounds like want you want to do is client-side rather than server-side; in which case you can still use the FormHelper as normal, but want a JavaScript solution for adding to the output <select>. It would be helpful if you edit your question to provide more details of what you are actually trying to achieve. As it stands your question isn't clear. - drmonkeyninja
drmonkeyninja is right, if you want your form to change when a user clicks a specific button, that is a JS event and has nothing to do with Cake. You should still use the Form helper in this situation. - Oldskool
@drmonkeyninja question edited sir. - Malik Perang

2 Answers

0
votes
<?php
    echo $this->Form->input( 'product_name', array(
        'type' => 'select',
        'options' => array(
            'key1' => 'val1',
            'key2' => 'val2',
        ),
        'empty' => true,
    ));
?>
0
votes

You can add other conditional values to your select options just by adding values to the variable array like this:

$myOptions = array(
    '1' => 'One',
    '2' => 'Two',
    '3' => 'Three'
);

// If user make some action
if($someAjaxAction == true) {
   $myOptions[4] = 'Four';
}

echo $this->Form->input('product_name', array('options' => $myOptions));