I would like to quote Mr Weedpacket himself before describing my issue:
if 'checkbox' is an unchecked checkbox, then isset($GET['checkbox']) will return false, because $POST['checkbox'] is not set (unchecked checkboxes are not including in the returned form data).
($POST['checkbox']=='') will do two things: since $POST['checkbox'] does not exist, PHP will assume that it is empty - the value of an empty variable is equivalent to null, false, 0, or an empty string. An empty string is equal to an empty string, so the test succeeds.
I have created multipage forms that store fields in array e.g:
<?php
session_start();
require_once('common_functions.php');
$_SESSION['form1_data']='';
<input name="incorporated" type="checkbox" id="incorporated" value="1">
<input type="checkbox" name="regionalassociation" value="1"/>
<input type="checkbox" name="regionalcharity" value="1"/>
?>
my formhandler takes the session array and inserts each checkbox value into it's respective field in the database:
session_start();
require_once('common_functions.php');
$_SESSION['form1_data']=$_POST;
// build main query from form 1
$sql = "INSERT INTO qualityuser SET qualityuser_id = NULL ";
foreach ($_SESSION['form1_data'] as $col =>$val ){
if($col <> 'submit'){
$sql .=", $col = '$val' ";
}
}
mysql_query($sql, $connection) or die(mysql_error());
//get last insert id if you want to put it in other tables as a reference
$sql ="select LAST_INSERT_ID() as id from qualityuser";
$result = mysql_query($sql, $connection) or die(mysql_error());
$row=mysql_fetch_array($result);
$id = $row['id'];
my problem - as emphasised by Weedpacket's quote, is that if the checkbox is not checked then it doesn't exist and so it cannot be recorded in the database as 'uncheckec' or 'no'. I need to keep the formhandler above working like it does at the moment - but as you can see the checkbox name must be the same as the field in the database otherwise it doesn't work.
Can anyone signpost me to an efficient workaround? I have read isset in the manual and looked in the archives but I could find no evidence in the context of my formhandler. Most fixes tend to result in having checkbox names as an array i.e checkbox[]- - this could not work given my formhandler
Thankyou in advance