Jump to content

Archived

This topic is now archived and is closed to further replies.

AndyB

checkbox array - how to retrieve ALL values

Recommended Posts

I've a simple application with a database containing four yes/no enum fields. All I want to do is be able to edit their values from a form that displays four checkboxes either checked for yes or unchecked for no.  I can read the POSTed value of all checkboxes that are checked, but in order to update the database record, I also need to be able to get the checkboxes which are not checked (or were unchecked in editing).

The form code looks like this ...

[code] echo "<td>";
echo "<input type='checkbox' value='". $i. "' name='chbox[]'";
if ($row['shift1']=="1") {
echo " checked='checked'";
}
echo "/>"; $i++;
echo "</td>";
 
echo "<td>";
echo "<input type='checkbox' value='". $i. "' name='chbox[]'";
if ($row['shift2']=="1") {
echo " checked='checked'";
}
echo "/>"; $i++;
echo "</td>";

echo "<td>";
echo "<input type='checkbox' value='". $i. "' name='chbox[]'";
if ($row['shift3']=="1") {
  echo " checked='checked'";
}
echo "/>"; $i++;
echo "</td>";[/code]

Once POSTed, I need to be able to constuct a simple UPDATE query that looks like:

[code]$query = "UPDATE $dbtable SET (shift1,shift2,shift3,shift4) VALUES ( ... whatever goes here ...) WHERE whatever condition ...";[/code]

Constructing the query from the $_POST array has me stumped since only checked values are returned by the form.

Share this post


Link to post
Share on other sites
run a loop
if($_POST["value"]){
$value = "y";
}else{
$value = "n";
}

OR
Have a default Not Null field as the enum
Where Default is N

Share this post


Link to post
Share on other sites
[quote author=onlyican link=topic=105728.msg422467#msg422467 date=1156613176]
run a loop
if($_POST["value"]){
$value = "y";
}else{
$value = "n";
}

OR
Have a default Not Null field as the enum
Where Default is N
[/quote]

Ah, but then you'd still have to know what indexes to check for!

You can't use the POST data. Like you said, it only returns the checked values. There is only one solution to this problem: don't.

You can not use indexes that don't exist, obviously.

One of the things you COULD do, is use 'SHOW FIELDS' to get the possible options, and set any values not in the superglobal to 0. Other options ofcourse include using an external source (textfile, XML), or 'hard-coding' the form options.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.