OK, I have to admit I have a real mental block when it comes to arrays so sorry if this seems a stupid question. I need to know how to error check a section of a form that is submitted as a multi-dimensional array.
I have a form that where people have to offer help at an event by firstly selecting one or more days and then for each day select one or more jobs and (depending on event requirements) a time to help. The part of the form that does this is:
<?php
$total_days=0;
while ($dayrow=$helper_days->get_row()) { $day_id=$dayrow['day_id']; ?>
<h3><?php echo $dayrow['day'];?> <input type="checkbox" <?php if ($helping['day_id']==$day_id || $total_job_days==1) { echo 'checked';}?> id="day_id[<?php echo $total_days;?>]" name="helpdays[<?php echo $total_days;?>][day_id]" value="<?php echo $day_id;?>"></h3>
<?php if ($helper_job_req=='t') { ?>
<p><span class="infolabel">Job(s)</span>
<?php
$helper_jobs = new helpers;
$helper_jobs->show_id=$_SESSION['show_id'];
$helper_jobs->load_jobs();
?>
<select id="job_id[<?php echo $total_days;?>]" name="helpdays[<?php echo $total_days;?>][job_id][]" size="4" multiple>
<?php while ($jobrow=$helper_jobs->get_row()) { ?>
<option value="<?php echo $jobrow['job_id'];?>"><?php echo $jobrow['job'];?></option>
<?php } ?>
</select><br />
<span class="infolabel"> </span>Please select all that you are willing to do<br />
<span class="infolabel"> </span>(hold Ctrl and click to select more than one).
</p>
<?php } if ($helper_time_req=='t') { ?>
<p><span class="infolabel">Time:</span>
<?php
$helper_job_times = new helpers;
$helper_job_times->show_id=$_SESSION['show_id'];
$helper_job_times->load_job_times();
while ($timerow=$helper_job_times->get_row()) { ?>
<?php echo $timerow['job_time'];?><input name="helpdays[<?php echo $total_days;?>][job_time_id]" type="radio" class="fields" id="job_time_id[<?php echo $total_days;?>]" value="<?php echo $timerow['job_time_id'];?>" <?php if ($helping['job_time_id']==$timerow['job_time_id']) { echo 'checked';}?> />
<?php } ?>
What I need to do is firstly check that at least one day has been selected and if not then display an error message. Then if at least one day has been selected I need to check if at least one job and (if required) a time have been selected and if not display another error message but only for the day(s) that have actually been selected.
I am presuming I need to use a foreach but I am not sure how I can split this (or the submitted array) to check as required.
Might be best if you post an array of typical return values for us to iterate through, and then explain;
[i ]do you mean you want to filter the array for bad things, tampering etc, or,
[ii] are you going to check the values off against a white-list of permitted values, or
[iii] do you simply mean that you check the values are not null?
Can you show the form output copied from the source in the browser view?
Showing the php in the form construction confuses the issue. There could be an issue with the form output. Also can you show the result of print_r($_POST).
I need to check that at least one day is selected (i.e. not null) and then check that a job and time have been selected for that day (again, not null).
There is no problem with the form output as it has been working as required for a couple of years now, I have just never put in the error checking for this part of the form.
I have tried array_key_exists now as well but I am either doing it wrong or it doesn’t do what I need (thought it would look for an array key like [day_id]).
Not really sure if this will help. I went off the HTML because i’m not really sure what your functions are doing. Not sure if this is the best way to do is, but i got it to collect errors. Been a bit since i did PHP so i’m not sure if this is the best way to handle checking values in arrays.
for ($i=0; $i<=2; $i++) {
if ($_POST['helpdays'][$i]['day_id'] != ''){
//-- Check required Fields if day is selected
if ($_POST['helpdays'][$i]['job_id'][0] == '') {
$dayerrors[$i][] = "No Jobs Selected";
}
if ($_POST['helpdays'][$i]['job_time_id'][0] == '') {
$dayerrors[$i][] = "No Jobs Times Selected";
}
if ($_POST['helpdays'][$i]['judge'][0] == '') {
$dayerrors[$i][] = "No Judge Selected";
}
}
}
if (count($dayerrors)) {
echo "<ul class=\\"error\\">\
";
foreach (array_keys($dayerrors) as $key) {
$section = $dayerrors[$key];
for ($i = 0, $count=count($section); $i < $count; $i++) {
echo "<li>".$section[$i]."</li>\
";
}
}
echo "</ul>\
";
}
Thanks Webdevgirl - with a bit of tweaking it is working a treat now.
As I needed to check that at least one day had been selected first I changed the first part of the code slightly (the $errors+=1 is to tie in with the rest of my form error checking):
$day=0;
foreach ($_POST['helpdays'] as $key=>$dayhelping) {
if ($dayhelping['day_id']!='') { $day++; }
}
if ($day==0) { $errors+=1 && $message3 = 'Please select day(s) to help'; }
else {
for ($i=0; $i<=2; $i++) {
if ($_POST['helpdays'][$i]['day_id'] != ''){
//-- Check required Fields if day is selected
if ($_POST['helpdays'][$i]['job_id'][0] == '') {
$dayerrors[$i][] = "Please select job(s)";
$errors+=1;
}
if ($_POST['helpdays'][$i]['job_time_id'][0] == '') {
$dayerrors[$i][] = "Please select a time";
$errors+=1;
}
}
}
}
The 2nd part of the code I then put into my form and that ended up looking like this (snippet):
<?php
$total_days=0;
while ($dayrow=$helper_days->get_row()) { $day_id=$dayrow['day_id']; ?>
<h3><?php echo $dayrow['day'];?> <input type="checkbox" <?php if ($_POST['helpdays'][$total_days]['day_id']==$day_id || $total_job_days==1) { echo 'checked';}?> id="day_id[<?php echo $total_days;?>]" name="helpdays[<?php echo $total_days;?>][day_id]" value="<?php echo $day_id;?>"></h3>
<?php
if (count($dayerrors)) {
echo '<ul>';
foreach (array_keys($dayerrors) as $key) {
if ($key==$total_days) {
$section = $dayerrors[$key];
for ($i = 0, $count=count($section); $i < $count; $i++) {
echo '<li><span class="errortext">'.$section[$i].'</span></li>';
}
}
}
echo '</ul>';
}
?>
<?php if ($helper_job_req=='t') { ?>
<p><span class="infolabel">Job(s)</span>
<?php
$helper_jobs = new helpers;
$helper_jobs->show_id=$_SESSION['show_id'];
$helper_jobs->load_jobs();
?>
<select id="job_id[<?php echo $total_days;?>]" name="helpdays[<?php echo $total_days;?>][job_id][]" size="4" multiple>
<?php while ($jobrow=$helper_jobs->get_row()) { ?>
<option value="<?php echo $jobrow['job_id'];?>" <?php if ($_POST['helpdays'][$total_days]['job_id'][0]==$jobrow['job_id']) { echo 'selected'; } ?>><?php echo $jobrow['job'];?></option>
<?php } ?>
</select><br />
<span class="infolabel"> </span>Please select all that you are willing to do<br />
<span class="infolabel"> </span>(hold Ctrl and click to select more than one).
</p>
<?php } if ($helper_time_req=='t') { ?>
<p><span class="infolabel">Time:</span>
<?php
$helper_job_times = new helpers;
$helper_job_times->show_id=$_SESSION['show_id'];
$helper_job_times->load_job_times();
while ($timerow=$helper_job_times->get_row()) { ?>
<?php echo $timerow['job_time'];?><input name="helpdays[<?php echo $total_days;?>][job_time_id]" type="radio" class="fields" id="job_time_id[<?php echo $total_days;?>]" value="<?php echo $timerow['job_time_id'];?>" <?php if ($_POST['helpdays'][$total_days]['job_time_id']==$timerow['job_time_id']) { echo 'checked'; } ?> />
<?php } ?>
<?php } else { ?>
<input type="hidden" id="job_time_id[<?php echo $total_days;?>]" name="helpdays[<?php echo $total_days;?>][job_time_id]" value="1" />