View Full Version : array keys

03-22-2007, 03:30 AM
I have a bunch of checkboxes and then on the next pg i check which boxes were ticked

Here is my code

$i = 0;
$array = "";
foreach(array_keys($_POST) as $myKey)
echo "ee";
if (ereg('value', $myKey))
echo "hi";
$id = ereg_replace("value='checkbox'", "", $myKey);
$array[$i] = $id;
$i = $i+1;

If none of the boxes are ticked i get errors
I want to check if boxes were not ticked and if none was ticked then send a user to another page
But ive tried every way i can think of and cant get it to work

Any ideas?

Len Whistler
03-22-2007, 07:14 AM
You want to validate form data with isset or strlen:

if (isset($var)) {
//$var has value.

} else {
// $var does not have value.

Also check out strlen.

03-22-2007, 07:18 AM
Good morning.

If you do the checkboxes this way

<input type="checkbox" name="chkbx[]" value="1" />
<input type="checkbox" name="chkbx[]" value="2" checked="checked" />
<input type="checkbox" name="chkbx[]" value="3" />
<input type="checkbox" name="chkbx[]" value="4" checked="checked" />
<input type="checkbox" name="chkbx[]" value="5" checked="checked" />
<input type="checkbox" name="chkbx[]" value="6" />
<input type="checkbox" name="chkbx[]" value="7" />
<input type="checkbox" name="chkbx[]" value="8" />

and submit it to your script, you will get the follow array:

$_POST["chkbx"] = Array (2, 4, 5);

Hope it helps

03-22-2007, 12:24 PM
The problem is i think there are always going to be values in the array because after checking check boxes (or not checking boxes) the user will click on a button to get to the next page where i want the validation to happen

When i use isset() and no checkbox has been checked it stil gives a positive answer because of the button that was clicked

any other suggestions!!

03-22-2007, 12:27 PM
also i cannot use the other example by creating an array because i need to use the "name" value to pass details on the item that was checked!