Jump to content

checkbox array - how to retrieve ALL values


AndyB

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.
Link to comment
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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.