Jump to content


Photo

Multiple radio buttons insert to db

multiple radio buttons

  • Please log in to reply
3 replies to this topic

#1 Slam

Slam

    Newbie

  • Members
  • Pip
  • 9 posts

Posted 25 January 2013 - 12:35 AM

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 by Slam, 25 January 2013 - 12:38 AM.


#2 SalientAnimal

SalientAnimal

    Advanced Member

  • Members
  • PipPipPip
  • 299 posts

Posted 25 January 2013 - 05:37 AM

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 by SalientAnimal, 25 January 2013 - 05:38 AM.


#3 Slam

Slam

    Newbie

  • Members
  • Pip
  • 9 posts

Posted 25 January 2013 - 01:59 PM

No, I think I need something like in this post http://forums.phpfre...t/#entry1259415 but how to adjust to my situation??

#4 Slam

Slam

    Newbie

  • Members
  • Pip
  • 9 posts

Posted 25 January 2013 - 04:31 PM

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);
         }        





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com