insert batch with group checkbox - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28) +--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30) +--- Thread: insert batch with group checkbox (/showthread.php?tid=76910) |
insert batch with group checkbox - eleumas - 07-02-2020 Hi. I have a group of checkbox like this:
Code: <input type="checkbox" name="preview[]" value="1"/> and the follow code in my controller: PHP Code: for($i = 0; $i < $count; $i++) The value of checkbox is wrong. This is or always 1 or always 0. I have tried too the ternary operator, something like this: PHP Code: $c = $this->request->getPost('preview') == 1 ? 1 : 0; but the problem has not been resolved. Someone can help me please? Thanks RE: insert batch with group checkbox - marcogmonteiro - 07-02-2020 I'm not really getting on what you're trying to achieve. Makes no sense to me that the checkbox value is always the same. Maybe if you provide the entire form HTML for more context and explain what you're really trying to do we could help you better. RE: insert batch with group checkbox - eleumas - 07-02-2020 (07-02-2020, 06:41 AM)marcogmonteiro Wrote: I'm not really getting on what you're trying to achieve. Makes no sense to me that the checkbox value is always the same. Hi Marco, thanks for help me. I would like use the checkbox for select the preview images. The preview images should have a value 1 the others 0. RE: insert batch with group checkbox - DarkKnight - 07-02-2020 (07-02-2020, 07:19 AM)eleumas Wrote:(07-02-2020, 06:41 AM)marcogmonteiro Wrote: I'm not really getting on what you're trying to achieve. Makes no sense to me that the checkbox value is always the same. Why don't you pass Product id to checkbox instead 1 for all RE: insert batch with group checkbox - eleumas - 07-02-2020 (07-02-2020, 11:26 AM)DarkKnight Wrote:(07-02-2020, 07:19 AM)eleumas Wrote:(07-02-2020, 06:41 AM)marcogmonteiro Wrote: I'm not really getting on what you're trying to achieve. Makes no sense to me that the checkbox value is always the same. Ok i can pass the product id but the problem is the same. what change if i pass product id? please, can you explain better. Thanks. RE: insert batch with group checkbox - ojmichael - 07-02-2020 Your code assumes a checkbox that is not selected will still submit a value.. that's never going to work. I'm also not sure that nested loops are necessary here? Your field names should be something like: Code: Row 1: images[0][title], images[0][alt], images[0][text] and images[0][preview] Then you would do like this: PHP Code: $images = []]; RE: insert batch with group checkbox - marcogmonteiro - 07-03-2020 If you have this: PHP Code: <input type="checkbox" name="preview[]" value="<?= $product->id ?>"/> then in your controller you would do PHP Code: foreach($this->request->getPost('preview) as $key => $product_id { |