I have a webpage in which users select whether they are either available (=1) (beschikbaar) or unavailable (=0) on a certain date, which are provided in a list.
I'm using radio-buttons to make sure users can't select available AND unavailable for the same date, however. As long as users actually enter whether they're available or not, the code works fine. My problem occurs when the users don't enter anything, as then, because of the hidden field, all empty dates in the list are sent to the database with value 0.
I need the hidden field to make sure the user entries and dates are actually matched, but it seems to be the cause of the aforementioned issue. Could somebody shed light on what I might do to solve this?
le code:
<?php
if(isset($_POST['beschikbaarheid'])) {
mysql_connect("localhost", "****", "****")or die("cannot connect to server");
mysql_select_db("****")or die("cannot select db");
$UserID = $_POST['UserID'];
$beschikbaarheid = $_POST['beschikbaarheid'];
$Datum = $_POST['Datum'];
foreach ($_POST['Datum'] as $date){
$index = strftime('%d%m%y',strtotime($date));
$beschikbaarheid = (isset($_POST['beschikbaarheid'][$index]) && ($_POST['beschikbaarheid'][$index] == 1 ))?1:0;
mysql_query("INSERT INTO `Werkdagen` (`UserID`, `Datum`, `bevestigd`, `invuldatum`, `beschikbaarheid`) VALUES ('$UserID', '$date', FALSE, NOW(), '$beschikbaarheid')");
-
<?php
setlocale(LC_TIME, 'dutch');
date_default_timezone_set("Europe/Amsterdam");
$two = strtotime('+1 weeks'); $date = time(); while ($date <= $two) {
echo "<li class=".strftime('%A',$date) . "><input name='Datum[]' type='hidden' id='".strftime('%A %e %B %Y',$date) . "' value='".strftime('%Y-%m-%d',$date)."'><input name='beschikbaarheid[".strftime('%d%m%y',$date) . "]' type='radio' value='1'><input name='beschikbaarheid[".strftime('%d%m%y',$date) . "]' type='radio' value='0'>".strftime('%A %e %B %Y',$date) . "</li>";
$date += 86400;
}