Search the Community
Showing results for tags 'multiple radio buttons'.
-
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] => )