Welcome Guest, Not a member yet? Register   Sign In
Taking Checkbox fields and writing them to the database.
#1

[eluser]maddtechwf[/eluser]
I have three db tables that are vehicle, users, and vehicles_to_users. When a new vehicle is being added they can select the driver for the vehicle. When they click Add, it writes a majority of the data to the vehicle db. Once it writes that data I get the insertion id so that I can write the user id and vehicle id to the vehicle_to_user database.

This is the code I have written in my model but it doesn't seem to be working when it comes to the vehicle_to_user db.

Code:
function insertVehicle($nickname, $year, $make, $model, $notes, $mileage, $users)
{
  $this->db->trans_start();
  
  $this->db->set('name', $nickname);
  $this->db->set('vyear', $year);
  $this->db->set('vmake', $make);
  $this->db->set('vmodel', $model);
  $this->db->set('notes', $notes);
  $this->db->set('initial_mileage', $mileage);
  $this->db->insert('vehicle');
  $vehicle_id = $this->db->insert_id();
  
  foreach($users->result() as $user)
  {
   $this->db->set('user_id', $user);
   $this->db->set('vehcile_id', $vehicle_id);
   $this->db->insert('vehicle_to_user');
  }

  $this->db->trans_complete();
}

View Code (No issues here with the available users showing)
Code:
<label for="">Drivers (Users)</label>
            &lt;?php foreach ($users as $user): ?&gt;
             &lt;?php echo form_checkbox('vehicle_drivers', "{$user["id"]}"); ?&gt;&lt;?php echo "{$user["first_name"]} {$user["last_name"]}"; ?&gt;
            &lt;?php endforeach; ?&gt;

Controller
Code:
public function add()
  {
   $nickname = $this->input->post('vehicle_name');
   $year = $this->input->post('vehicle_year');
   $make = $this->input->post('vehicle_make');
   $model = $this->input->post('vehicle_model');
   $notes = $this->input->post('vehicle_notes');
   $mileage = $this->input->post('vehicle_init_mileage');
   $users = $this->input->post('vehicle_users');
  
   $this->vehicle_m->insertVehicle($nickname, $year, $make, $model, $notes, $mileage, $users);
  }
#2

[eluser]Tim Brownlaw[/eluser]
[quote author="maddtechwf" date="1397941841"]...
This is the code I have written in my model but it doesn't seem to be working when it comes to the vehicle_to_user db.
[/quote]

Instead of one of us just giving you the answer, let's see if we can help you find it yourself!

The real question is "I don't understand what is going on here...."
So let's see if we can remedy that!

Ok so what you need to do is go from the end and work your way back to the the start!

You've generated a checkbox(s) on your Form! This is where you are wanting to get the information from and this is the bit that's "not happening!" or getting into your DB.

Time to do some "looking"!
So the first thing to check is - what is the HTML being generated for the checkbox!
Is it correct? Is it what you are expecting?

What is the Name of the Checkbox?

What do you see in your posted information when you
1. Check No Checkboxes
2. Check One Checkbox
3. Check ALL Checkboxes

How do you see what it's returning?

The simple way is to do a var_dump($_POST) - the good ole fashioned way and see exactly what is coming back from your form... That is if you are using method="post" in your form!

I'll leave it there for now and see what you discover!

Cheers
Tim


#3

[eluser]maddtechwf[/eluser]
Thank you for pointing me in the correct direction to look. I used the var_dump($_POST) and returned everything I expected to get except for when it came to my check boxes. When I selected two users, it returned that I checked two boxes. I also return 0 when I clicked no users. So the data that is coming from my POST is not correct for the user selected.

Any suggestions on where I should look next?
#4

[eluser]maddtechwf[/eluser]
Tim,

I actually found the solution. I had to add a [] at the end of the name of the checkbox. Now it is reading in each response for users that I select.
#5

[eluser]Tim Brownlaw[/eluser]
That's what I like to hear!

Excellent... Well done!

Cheers
Tim

#6

[eluser]maddtechwf[/eluser]
Will my foreach loop work for inserting data into the vehicle_to_user table?
#7

[eluser]Tim Brownlaw[/eluser]
Well that's the next question.

Have you tried it to see what happens?

1. Do you want only one driver per vehicle or
2. Do you want to allow many drivers per vehicle.

And you will need to trap the case where no drivers are selected as you'll want at least 1.

If you just want one driver per vehicle then what you have should work - at a guess...
If you have many drivers then you'll need to perform the insert in your join table per driver for that vehicle...

That should give you something to ponder on...

Cheers
Tim
#8

[eluser]InsiteFX[/eluser]
Radioboxes and checkboxes are returned empty if they are not checked.

If they are checked then they will return a TRUE value otherwise they are emptied.

They will not return a FALSE value.




Theme © iAndrew 2016 - Forum software by © MyBB