Question:
I have a <select>
which is fed by data from the database. I need to manipulate this select and multiply it if the user wants to enter more different data.
I also need to change the "name" attribute of each <select>
in order to get the values correctly in $this->request->data
because I implode the data.
I have a function that runs in pure php, but now I'm trying to redo it in CakePHP . I've never done anything with Javascript or Jquery in CakePHP , so I'm kind of lost on that. My main question is how to use Javascript with Cake. I have a script.js file that I use in the pure php application. Now I want to use this with Cakephp, but I don't know how to do it. I've already put the file in the webroot/js folder, but I don't know how to go from there.
Answer:
Cakephp follows a pattern for creating form fields if you are using the Cake Form Helper
The pattern is always like this:
$this->Form->input('model.id'); // <input type="text" name="data[model][id]" />
Based on this return, you could add the fields normally by jQuery, but setting the element's name
according to Cakephp's standard. See a simple example
$(function(){
var $form = $('#meu-formulario');
var $baseSelect = $('<select></select>');
$('.algum-evento').click(function(){
var $newSelect = $baseSelect.clone();
$newSelect.attr({name: 'data[model][novo_campo]'});
$form.append($newSelect);
});
});