开发者

PHP: Saving weekday + the two times

I am trying to make so you can select your availability time, monday to sunday, from xx开发者_开发问答:00 to xx:00.

For the HTML i did a for loop, outputting the weekdays checkbox and two time options, looks like this: jsfiddle.net/Sbcbg

the source:

$weekDays = array('Måndag', 'Tisdag', 'Onsdag', 'Torsdag', 'Fredag', 'Lördag', 'Söndag');
echo '<table cellspacing="0" cellpadding="0" width="300">';
    for($i=0; $i<count($weekDays); $i++){
     echo '<tr><td>';
     echo '<input type="checkbox" name="day'.$i.'">'.$weekDays[$i].'</option>';
     echo '</td><td>';
     echo '<select>';       
        for($f=23; $f>18; $f--){
         echo ' <option name="from'.$i.'" select="">'.$f.':00</option> ';
        }
     echo '</select>';
     echo ' till ';
     echo '<select>';
        for($t=0; $t<10; $t++){
         echo ' <option name="from'.$i.'" select="">'.$t.':00</option> ';
        }
     echo '</select>';
     echo '</td></tr>';
    }
echo '</table>';

How should I know what are what for the saving part?

How can I see if one weekday is checked, and if it is then take the two option values (the two times) and output them?

So if you check Tisdag(Tuesday) and select time 22:00 and in the other you select time 04:00, then it should output that you have checked Tuesday 22:00-04:00 when you submit the form.

Thanks in forward


You seem to have some mismatched tags here. For example:

'<input type="checkbox" name="day'.$i.'">'.$weekDays[$i].'</option>'

You are closing an option that you never open.

I think you want a checkbox to indicate that this day is day that has some availability. I would recommend changing it to:

'<label for="day'.$i.'"><input type="checkbox" name="day'.$i.'" value="1"/>'.$weekDays[$i].'</label>'

Then for your selects, you want to define the name of the selects and the values of the options. The name will then be the index in the $_POST array, and the value will be the value of the option the user selected.

Such as:

echo '<select name="from'.$i.'">';       
    for($f=23; $f>18; $f--){
     echo ' <option value="'.$f.':00">'.$f.':00</option> ';
    }
 echo '</select>';

Then your submit checks would look something like:

for($i = 0; $i< 7; $i++) {
    if(isset($_POST['day'.$i]) && ($_POST['day'.$i] == 1) {
        $from = $_POST['from'.$i];
        $to = $_POST['to'.$i];
        //do something with this data
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜