Connecting Tech Pros Worldwide Forums | Help | Site Map

Using checkboxes to update multiple database entries

mountain.dog@gmail.com
Guest
 
Posts: n/a
#1: Mar 29 '07
I have a query that shows a list of options that a user can toggle on
or off using a checkbox.

query...
form...
while($row = mysql_fetch_array($result))...
<input name="menu_show_attribute[]" type="checkbox"
class="checkbox"');
if ($row['menu_show_attribute'] == 1) {
echo (' value="'.$row[menu_id].'" checked />
} else {
echo (' value="'.$row[menu_id].'" />');
}

The list is generated from boolean values in a DB. This populates my
form with checkboxes, some are checked and others are not. I'm using
an if statement to sort it (checked). This works fine.

The problem is when the user un-checks a checkbox, the value does not
get passed and the DB does not update - set/change it to 0. The only
values that get passed are the checkboxes that are checked. Below is
the query / code after the submit button has been pressed. Are radio
buttons the answer? Any help / suggestions would be very much
appreciated.

//process form
for ($i = 0; $i < count($menu_show_attribute); $i++) {

if (isset($menu_id) == 'checked') {
$menu_show_attribute = 1;
} elseif ($menu_show_attribute != 'checked') {
$menu_show_attribute = 0;
}

$query = 'UPDATE menu SET menu_show_attribute = "'.
$menu_show_attribute.'" WHERE menu_id = "'.
$menu_show_attribute[$i].'"';
echo("<br>");
echo $query;
$result = mysql_query($query, $db) or die(mysql_error());
}


Geoff Berrow
Guest
 
Posts: n/a
#2: Mar 29 '07

re: Using checkboxes to update multiple database entries


Message-ID: <1175177552.525042.244720@b75g2000hsg.googlegroups .comfrom
mountain.dog@gmail.com contained the following:
Quote:
>The problem is when the user un-checks a checkbox, the value does not
>get passed and the DB does not update - set/change it to 0. The only
>values that get passed are the checkboxes that are checked. Below is
>the query / code after the submit button has been pressed. Are radio
>buttons the answer? Any help / suggestions would be very much
>appreciated.
Checkboxes only return a value if checked. The solution is to make your
update query update all fields with the default value set to unchecked.
The unchecked fields will automatically be unchecked because the POSTed
value will not exist.

Of course you will have to explicitly name your check boxes something
like <input name='menu_show_attribute[".$row['menu_id']."]'
type='checkbox'
class='checkbox')

And of course you won't be able to loop through the array
$_POST['menu_show_attribute'] because the unchecked values will not
show. However you could include a hidden field
<input name='hidden_menu_id[".$row['menu_id']."]' type='hidden'>

Then update your db by looping through $_POST['hidden_menu_id']

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
mountain.dog@gmail.com
Guest
 
Posts: n/a
#3: Mar 29 '07

re: Using checkboxes to update multiple database entries


Thanks. I had a feeling checkboxes were not going to work. Oh well.
Do think radio buttons would work?

On Mar 29, 9:43 am, Geoff Berrow <blthe...@ckdog.co.ukwrote:
Quote:
Message-ID: <1175177552.525042.244720@b75g2000hsg.googlegroups .comfrom
mountain....@gmail.com contained the following:
>
Quote:
The problem is when the user un-checks a checkbox, the value does not
get passed and the DB does not update - set/change it to 0. The only
values that get passed are the checkboxes that are checked. Below is
the query / code after the submit button has been pressed. Are radio
buttons the answer? Any help / suggestions would be very much
appreciated.
>
Checkboxes only return a value if checked. The solution is to make your
update query update all fields with the default value set to unchecked.
The unchecked fields will automatically be unchecked because the POSTed
value will not exist.
>
Of course you will have to explicitly name your check boxes something
like <input name='menu_show_attribute[".$row['menu_id']."]'
type='checkbox'
class='checkbox')
>
And of course you won't be able to loop through the array
$_POST['menu_show_attribute'] because the unchecked values will not
show. However you could include a hidden field
<input name='hidden_menu_id[".$row['menu_id']."]' type='hidden'>
>
Then update your db by looping through $_POST['hidden_menu_id']
>
--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDshttp://www.ckdog.co.uk/rfdmaker/

Geoff Berrow
Guest
 
Posts: n/a
#4: Mar 29 '07

re: Using checkboxes to update multiple database entries


Message-ID: <1175180377.334506.304110@l77g2000hsb.googlegroups .comfrom
mountain.dog@gmail.com contained the following:
Quote:
>Thanks. I had a feeling checkboxes were not going to work.
Where did I say that? Of course checkboxes will work.
Quote:
>Do think radio buttons would work?
No.
--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Closed Thread


Similar PHP bytes