Jump to content


Photo

checkbox array - how to retrieve ALL values


  • Please log in to reply
2 replies to this topic

#1 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 26 August 2006 - 05:15 PM

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 ...

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>";

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

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

Constructing the query from the $_POST array has me stumped since only checked values are returned by the form.
Legend has it that reading the manual never killed anyone.
My site

#2 onlyican

onlyican
  • Members
  • PipPipPip
  • Advanced Member
  • 921 posts
  • LocationHants - UK

Posted 26 August 2006 - 05:26 PM

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
Tell me the problem, I will try tell you the solution

#3 448191

448191
  • Staff Alumni
  • Advanced Member
  • 3,545 posts
  • LocationNetherlands

Posted 26 August 2006 - 05:34 PM

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


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.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users