Forum Moderators: coopster

Message Too Old, No Replies

Dealing with checkboxes and $_POST array

         

fintan

2:27 pm on Sep 15, 2005 (gmt 0)

10+ Year Member



Hi I'm doing a form checking script. It loops through the $_POST array and checks what's needed. It does it based on how many elements are in the array.

The form contains checkboxes and if they are not submited it throws the count off.

Is there a way to account for the checkboxes?


if(isset($_POST['MM_insert'])/* &&!empty($_POST['STAFFNUMBER'])*/){
$_POST['Established'] == 'on'? $est = '1' : $est = '0';
$_POST['Employee'] == 'on'? $emp = '1' : $emp = '0';

$post_values = array_values($_POST); // Gets values of the array
$post_keys = array_keys($_POST);
$x <= 0;
// print_r($post_keys);

$post_count = count($post_values); // counts values in the array
$var_type_cast = array('text','number','date','varchar','hyperlink'); // variable type
$required = array(0,1,2,7,9,10,15); // required variables
$required_values = array_values($required);
$required_count = count($required); // count required variables
// print_r($required_values);

for($i = 0; $i < $post_count; $i++){ // Builds an array that can be checked later
if(empty($post_values[$i])){
$ans[] = 'yes';

}
else{
$ans[] ='no';
}
}

$b = $ans;
$c = array_combine($post_keys, $b);
// print_r($c);

$newvalues = array_values($required); // Gets values of the array

for($i = 0; $i < $required_count; $i++){ // Builds an array
if($c[$i] == 'yes'){
$first_check = 'yes';
}
}

$post_values = array_values($_POST);
$x = 0;

while($x <= count($_POST)){ // replace all ' with ''
$replace[] = eregi_replace("'", "''", stripslashes($post_values[$x++]));
}

$rsltcombine = array_combine($_POST, $replace);

$rsInsert = "Sql insert";
$insert = new Get_Sql(); $insert_rslts = $insert->SqlSingle($rsInsert);
header("Location: index.php");
}

Thanks

fintan.

StupidScript

10:20 pm on Sep 16, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



How is your form coded? Are your checkboxes an array by virtue of your form code? (i.e.
name="box[]"
as opposed to
name="box"
)

If

box[]
, then you've got
$_POST["box"]
as a natural array (
$_POST["box"][0]
). Otherwise,
$_POST["box"]
will only reference the last
box=
element in the
$_POST
array.

Are you having trouble handling

$_POST["box"]
elements? Or is it just messing with your
$post_count
?

StupidScript

11:36 pm on Sep 16, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



BTW, I mention the
box[]
method because that would give you 1 element in the
$_POST
array to deal with instead of trying to account for all of the individual checkboxes. If only one box or all of the boxes are checked, the array exists as a single element (with multiple internal elements). If none are checked, it doesn't exist. I thought it might help ... ;)

fintan

8:41 am on Sep 19, 2005 (gmt 0)

10+ Year Member



Thanks for the reply StupidScript.

I came up with a script that checks what's needed then compares to what's submitted. It doesn't matter what type of form field it is. If you want I'll post it. Just have to finish off the type casting and what type of data is submitted. Thanks again

fintan.