0

Hi Im struggling with this problem, can some one help? I got a series of check boxes dynamically populated. the line looks like below which is part of a While loop.

echo "<td class='brc'><input type='checkbox' name='delz[]' value='$wec' ></td>";

when validating i'm finding very difficult to see at-least once check box is check in the loop. I got the following to work, which identifies vise versa what i need. How do I change this to give me an output when nothing is checked a message echo saying 'nothing checked'?

    $selectboxes = $_POST['delz'];
foreach($selectboxes as $A)
{if($selectboxes == ""){echo "Some check boxes are selected !!!"; return;}}
D Karolis
  • 51
  • 5
  • `var_dump` is your friend. Learn what your program does by looking into variables, e.g. `$_POST`. I'm sure you find the solution to your problem then in no time. If you struggle with the basics, e.g. if you don't know *exactly* what `$_POST` is and what it has to do with forms, here is a good description: http://php.net/language.variables.external – hakre Apr 18 '13 at 08:35

2 Answers2

1

deselected checkboxes are not passed through HTML forms.

therefore:

$checkboxesSelected = isset($_POST['delz']);

if ($checkboxesSelected)
{
    echo "Some check boxes were selected";
} else {
    echo "No check boxes were selected.";
}

should work.

hakre
  • 193,403
  • 52
  • 435
  • 836
STT LCU
  • 4,348
  • 4
  • 29
  • 47
  • Also, write code more verbose in answers. Like with indentation, nice-named variables. I edited your answer, you see what I mean? It might be drastic, but the OP obviously is new to programming so make it look good. – hakre Apr 18 '13 at 08:45
  • After this looks nicely now, I've removed my comments because they are not needed any longer. You might want to remove yours, too :) – hakre Apr 18 '13 at 08:50
  • *Shooting from the hip*? ;-) +1 to the end result. – Majid Fouladpour Apr 18 '13 at 08:53
  • at least i checked that the safety was removed, the bullet chambered, and nobody on the shooting ground. ;) – STT LCU Apr 18 '13 at 08:55
-2

i think the simplest way to get checkboxes series

<?
$result = array();
foreach($_POST['delz'] as $checkbox){
   $result[]  = $checkbox; //u got all values that checkboxes has actually checked
}
print_r($result);
?>
vaibhav
  • 129
  • 4
  • If you use PHP tags, [please do not use the short ones in code-examples](http://stackoverflow.com/questions/200640/are-php-short-tags-acceptable-to-use) (You can leave them out here in SO, you will get the PHP highlighting anyway). Also re-read your code: When I first read it I was under the impression this somehow re-invents the wheel. Can you explain why I got that feeling? – hakre Apr 18 '13 at 08:47