reading checkbox array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mzfp2
Forum Contributor
Posts: 137
Joined: Mon Nov 11, 2002 9:44 am
Location: UK
Contact:

reading checkbox array

Post by mzfp2 »

Hi

is it possible to read the values of a array of checkboxes submitted in a form?

the number of checkboxes generated is variable, and it's values as well. So i need to implement a script to read the array values as part of the the $HTTP_POST_VARS is if possible? but how?
User avatar
haagen
Forum Commoner
Posts: 79
Joined: Thu Jul 11, 2002 3:57 pm
Location: Sweden, Lund

Post by haagen »

You have to know the names of the checkboxes, because there is not a way yo tell if the value is from a checkbox or any other control. Try something like this.

Code: Select all

foreach($_POST as $key => $val){
  if(sscanf($key, "checkboxnr%d", $nbr){

    echo "Checkbox number $nbr hade the followinf value, $val";

  }
}
Note! I don't really remember the syntax of sscanf. But it think this would work.
mzfp2
Forum Contributor
Posts: 137
Joined: Mon Nov 11, 2002 9:44 am
Location: UK
Contact:

Post by mzfp2 »

phew associative arrays in play there,

ok .. is it possible therefore to give each checkbox the same name, would the code above still distinguish between the different checkboxes?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

I tried to explain it here: viewtopic.php?t=4396
Post Reply