Creating and populating form fields dynamically in CakePHP -
in form have simple drop-down input list populated data controller. here input field:
echo $this->form->input('user_id', array('label'=>'employee'));
and here how populate it:
$employees = $this->user->getemployeelist($this->auth->user('company_id')); $this->set('users', $employees);
now need allow user create these employee drop-downs dynamically. thought use jquery add html needed fields, how populate them same $employees array?
you must write javascript, or jquery plugin that. in view.ctp file transform this->user->getemployeelist($this->auth->user('company_id')) in javascript variable. when create new element in click event populate field values:
var employeelist= <?=$users?> $('#addvar').on('click', function(){ varcount++; $node = $('<p><label for="var'+varcount+'">employee '+varcount+': </label><input type="text" name="var'+varcount+'" id="var'+varcount+'"><span class="removevar">remove employee</span></p>').val(employeelist); $(this).parent().before($node); });
Comments
Post a Comment