Slam Posted January 25, 2013 Share Posted January 25, 2013 (edited) Hey, I'm trying to insert dynamically generated radio buttons form to the db, but this code below inserts always first group(id) with always selected value 1. How can I insert all groups(ids) with proper value?? Here's what I have. Table radio_form `radio_form` (`name_id`, `name1`, `name2`) (1, 'Nike', 'Addidas'), (2, 'Google', 'Bing'), (3, 'Apple', 'Microsoft'), (4, 'Coca-Cola', 'Pepsi'), (5, 'Snowboard', 'Ski'), (6, 'Car', 'Bike'), (7, 'Futbol', 'Rugby'), (8, 'Hot', 'Cold'); Form page (hidden input to insert id of the group to the db) <?php if (isset($_POST['hide_id'], $_POST['selected'])) { $name_id = $_POST['hide_id']; $item_select = $_POST['selected']; $errors = array(); if (empty($_POST['selected'])) { $errors[] = 'All fields required!'; } if (!empty($errors)){ foreach ($errors as $error) { echo '<div id="error">', $error, '</div><br />'; } } else { $name_id = (int)$_POST['hide_id']; $user_id = $_SESSION['user_id']; $item_select = (int)$_POST['selected']; $query = "INSERT INTO `selection` VALUES ('$name_id', '$user_id', '$item_select')"; mysql_query($query); echo "<br />OK<br />"; /*header('Location: index.php'); exit();*/ } } ?> <form action="" method="POST" id="go" name="go"> <?php $items = get_items(); foreach($items as $item){ echo $item['name_id']; ?> <label for ="<?php echo $item['name1']; ?>"> <input type ="hidden" name="hide_id[<?php echo $item['name_id']; ?>]" value="<?php echo $item['name_id']; ?>"> <?php echo $item['name1']; ?> <input type ="radio" id="<?php echo $item['name1']; ?>" name="selected[<?php echo $item['name_id']; ?>]" value="1" /> </label> <input type ="radio" id="<?php echo $item['name2']; ?>" checked name="selected[<?php echo $item['name_id']; ?>]" value="2" /> <label for ="<?php echo $item['name2']; ?>"> <?php echo $item['name2']; ?> </label><br /> <?php } ?> <br /><br /> <button type ="submit" id="send" name="send">Send</button> </form> print_r($_POST); Array ( [hide_id] => Array ( [1] => 1 [2] => 2 [3] => 3 [4] => 4 [5] => 5 [6] => 6 [7] => 7 [8] => 8 ) [selected] => Array ( [1] => 2 [2] => 2 [3] => 1 [4] => 2 [5] => 2 [6] => 1 [7] => 1 [8] => 2 ) [send] => ) Edited January 25, 2013 by Slam Quote Link to comment Share on other sites More sharing options...
SalientAnimal Posted January 25, 2013 Share Posted January 25, 2013 (edited) I'm not sure if this is what you are after, but try doing your insert something like this: $query="INSERT INTO selection (value_1 , value_2 , radio_selection ) VALUES ('$_POST[value_1]' ,'$_POST[value2_]' ,'$_POST[radiobutton_1], $_POST[radiobutton_2]')"; Edited January 25, 2013 by SalientAnimal Quote Link to comment Share on other sites More sharing options...
Slam Posted January 25, 2013 Author Share Posted January 25, 2013 No, I think I need something like in this post http://forums.phpfreaks.com/topic/245143-php-mysql-multiple-rows-insert/?do=findComment&comment=1259415 but how to adjust to my situation?? Quote Link to comment Share on other sites More sharing options...
Slam Posted January 25, 2013 Author Share Posted January 25, 2013 After many attempts it finally works. Working code $user_id = $_SESSION['user_id']; foreach($_POST['selected'] as $key => $selected) { if(is_array($selected)) { echo 'Group:' . $key . '<br/>'; foreach($selected as $selected_group) { echo 'Selected:' . $selected_group . '<br/>'; } } else { echo 'Group:' . $key . '<br/>Selected value:' . $selected . '<br/>'; } $query = "INSERT INTO `selection` VALUES ('$key', '$user_id', '$selected')"; mysql_query($query); } Quote Link to comment 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.