Cakephp 2 get multiple checkbox and insert into DB

I’m a newbie in programming. I am working on a home project, here I have a list of tools from my database
Screenshot-2018-3-27%20NGS%20Network%20General%20Service
I want to get the value from input mouvement, technicien and Mission and also the checked checkbox. I used javascript to get checked checkbox but I don’t know how to insert them into the database.

First start with retrieving the selections;
if ($this->request->is(‘post’)) {
$mouvement = $this->request->getData(‘Mouvement’);
$technicien = $this->request->getData(‘Technicien’);
$mission = $this->request->getData(‘Mission’);
$checkbox = $this->request->getData(‘Checkbox1’);
The latter of course does not work; you will have to use a foreach() to retrieve all checkboxes so you can know which one was checked. You will then probably already know what was on the line because that’s information that was already retrieved when building the page.
Next, you will need to create an entity so you can store it. Storing is learned from the cookbook. Building up code to store a record is done (if I know my PHP) by building an entity;
$article = new Article([
‘id’ => 1,
‘title’ => ‘New Article’,
‘created’ => new DateTime(‘now’)
]);;
You would be able to replace the column names with the columns from your table. Storing is done with code like below, where you can replace ‘article’ with your entity.
if ($this->request->is(‘post’)) {
if ($this->Articles->save($article)) {
$this->Flash->success(__(‘Your article has been saved.’));
}

Disclaimer; I am not world’s most enlightened CakePHP guru so if there is something wrong in this code or if it should have been done with other methods, I bow to more superior wisdom. I’m happy to learn something new.

1 Like