GPlev Posted April 11, 2021 Share Posted April 11, 2021 I have a form that has a list of checboxes, each checkbox has multiple values. IE. Checkbox1 has Date 1 and Cost1. Checkbox2 has Date2 and cost2 and so on. This is what I have in form. echo "<input type=checkbox name=service_id[] value=".$id."><label>".$description."</label> Date:<input type=date name=date[] value=".date('Y-m-d').">Date:<input type=number name=cost[] value=".$cost."><br>"; When i run the for loop to insert the values into a table i have this. foreach($_POST['service_id'] as $key => $value){ $cat1=$_POST['service_id'][$key]; $cat2=$_POST['date'][$key]; $cat3=$_POST['cost'][$key];} The problem is that when I select certain boxes (ie, checkbox #2), it inserts Date1 and Cost1 but does use the correct service id (ie checkbox) I am clearly not storing the values into the array correctly. Any thoughts? Quote Link to comment https://forums.phpfreaks.com/topic/312456-multiple-values-in-a-checkbox/ Share on other sites More sharing options...
Barand Posted April 11, 2021 Share Posted April 11, 2021 (edited) Only checked checkboxes are posted so doing it that way the indexes can get out of synch. I'd recommend using the id as the input array keys. <input type=checkbox name='service_id[$id]' .. > <input type='date' name='date[$id]' .. > etc Edited April 11, 2021 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/312456-multiple-values-in-a-checkbox/#findComment-1585727 Share on other sites More sharing options...
GPlev Posted April 11, 2021 Author Share Posted April 11, 2021 that worked. thank you Quote Link to comment https://forums.phpfreaks.com/topic/312456-multiple-values-in-a-checkbox/#findComment-1585728 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.