dazzclub Posted August 28, 2008 Share Posted August 28, 2008 Hello, I have some checkboxes that i would like to use that inserts data into table if clicked. I first using something like this --------------------- <?PHP $ch1 = 'unchecked'; $ch2 = 'unchecked'; $ch3 = 'unchecked'; $ch4 = 'unchecked'; $ch5 = 'unchecked'; if (isset($_POST['Submit1'])) { if (isset($_POST['prod1'])) { $prod1 = $_POST['ch1']; if ($prod1 = = 'car') { $prod1 = 'checked'; } } ...insert into table... --------------------- But i realise this can become to long as i have at leaset 8 checkboxes (different categories). I then thought it would be best to use array? My main concern is that each check box has its own category (inside the products table) so im thinking i would write some like this if checkboxes are true (as in checked) then insert them into the table and all the write tables else dont worry about it, am i on the right track cheers Darren Link to comment https://forums.phpfreaks.com/topic/121714-should-i-use-array-for-this/ Share on other sites More sharing options...
phpoet Posted August 28, 2008 Share Posted August 28, 2008 You can name your checkboxes something like <input type='checkbox' id='product_01' name='products[]' value='1'> <input type='checkbox' id='product_02' name='products[]' value='2'> <input type='checkbox' id='product_03' name='products[]' value='3'> Then, in your PHP code you can get all of the selected values in an array like this $selected_products = $_POST['products']; foreach($selected_products as $product_id) { echo "You selected product: $product_id"; } All of the products that the user selects will be in the $selected_products array. Link to comment https://forums.phpfreaks.com/topic/121714-should-i-use-array-for-this/#findComment-627887 Share on other sites More sharing options...
dazzclub Posted August 29, 2008 Author Share Posted August 29, 2008 Hi there Thanks for your help...so far i have created this ------------------------- $product_interest = array('bathsafety', 'uvSafety', 'hotWarningLabel', 'feedingSpoon', 'bedroomAndNursery'); //coming from a checkbox or multiple select statement $valid = true; if(is_array($_POST['input'])) { $valid = true; foreach ($_POST['input'] as $input) { if (!in_array($input, $product_interest)) { $valid = false; } } if($valid) { $q = "INSERT INTO product_interest (bathsafety, uvSafety, hotWarningLabel, feedingSpoon, bedroomAndNursery) VALUES ( '$valid')"; } } ------------------------- still no update...but im working on it....cheers Link to comment https://forums.phpfreaks.com/topic/121714-should-i-use-array-for-this/#findComment-628670 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.