Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

multiple date select form

Status
Not open for further replies.

p27br

Programmer
Aug 13, 2001
516
0
0
GB
hello

does anyone know if a script exists that would do the following :

a form which displays a complete month calendar where the user can select multiple dates that will be saved to a database table.
the user can scroll forward and back between months.
Example : I want to book the first 3 days of december :
initially, the form will display the current month, so the user must scroll forward to the next month (only one complete month per window), he then clicks on 1st,2nd, & 3d of December
and clicks a save button, and the days are saved to the DB.

I don't have any idea as to create this so I'm looking either for a ready made script that i could adapt to my needs, or some directions as to how to start out.
many thanks
 
do you want the date to be saved into the database or some arbiturary data?
 
i have the following script which displays a monthly calendar, the month and year are selected with comboboxes.
the code comes from a book, I added a checkbox next to the day number, so that the user can select dates to book. i'd like to then save the dates to a database.
for this i need to detect which checkboxes are checked.
this code is never executed =
Code:
       foreach($_POST['mycheckbox'] as $value){
           echo 'test';
           echo "<BR> You clicked checkbox number " .$value, "\n";
       }
   }

Code:
<?php
// Use the form helper functions defined in Chapter 6
require 'formhelpers.php';

$months = array(1 => 'January', 2 => 'February', 3 => 'March', 4 => 'April', 
                5 => 'May', 6 => 'June', 7 => 'July', 8 => 'August',
                9 => 'September', 10 => 'October', 11 => 'November', 
                12 => 'December');

$years = array();
for ($year = date('Y') - 1, $max_year = date('Y') + 5; $year < $max_year; $year++) {
    $years[$year] = $year;
}

if ($_POST['_submit_check']) {
    if ($errors = validate_form()) {
        show_form($errors);
    } else {
        show_form();
        process_form();
    }
} else {
    // When nothing is submitted, show the form and then
    // a calendar for the current month
    show_form();
    show_calendar(date('n'), date('Y'));
}

function validate_form() {
    global $months, $years;
    $errors = array();

    if (! array_key_exists($_POST['month'], $months)) {
        $errors[] = 'Select a valid month.';
    }

    if (! array_key_exists($_POST['year'], $years)) {
        $errors[] = 'Select a valid year.';
    }

    return $errors;
}

function show_form($errors = '') {
    global $months, $years, $this_year;

    // If the form is submitted, get defaults from submitted variables
    if ($_POST['_submit_check']) {
        $defaults = $_POST;
    } else {
        // Otherwise, set our own defaults: the current month and year
        $defaults = array('year' => date('Y'),
                          'month' => date('n'));
    }


    if ($errors) {
        print 'You need to correct the following errors: <ul><li>';
        print implode('</li><li>',$errors);
        print '</li></ul>';
    }

    print '<form method="POST" action="'.$_SERVER['PHP_SELF'].'">';
    input_select('month', $defaults, $months);
    input_select('year',  $defaults, $years);
    input_submit('Afficher','Afficher');
    input_submit('Enregistrer','Enregistrer');
    print '<input type="hidden" name="_submit_check" value="1"/>';
    print '</form>';
}

function process_form() {
    // si on a clicke sur afficher
    if (isset($_POST['Afficher'])) {
    show_calendar($_POST['month'], $_POST['year']);
    }
    //sinon, verifier si des dates ont ete selectionnees
    //si oui, enregistrer dans la DB
    //sinon, rien faire

   if (isset($_POST['Enregistrer'])) {
       foreach($_POST['mycheckbox'] as $value){
           echo 'test';
           echo "<BR> You clicked checkbox number " .$value, "\n";
       }
   }

}

function show_calendar($month, $year) {
    global $months;
    $weekdays = array('Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa');

    // Find the epoch timestamp for midnight on the first day of the month
    $first_day = mktime(0,0,0,$month, 1, $year);
    // How many days are in the month?
    $days_in_month = date('t', $first_day);
    // What day of the week (numerically) is the first day of the month?
    // You need this to put the first table cell in the right place
    $day_offset = date('w', $first_day);


    // Print the table header and the row of weekday names
    print<<<_HTML_
<table border="0" cellspacing="0" cellpadding="2">
<tr><th colspan="7">$months[$month] $year</th></tr>
<tr><td align="center">
_HTML_;
    print implode('</td><td align="center">', $weekdays);
    print '</td></tr>';


    // If the first day of the month is, say, a Tuesday, then you
    // need to put blank table cells under "Su" and "Mo" in the first
    // row so that the day 1 table cell goes under "Tu"
    if ($day_offset > 0) {
        for ($i = 0; $i < $day_offset; $i++) { print '<td>&nbsp;</td>'; }
    }

    // Print a table cell for each day of the month
    for ($day = 1; $day <= $days_in_month; $day++ ) {
        print '<td align="center">' . $day ;
        input_radiocheck('checkbox','mycheckbox[]', $_POST, $day);
        print  '</td>';
        $day_offset++;
        // If this cell was the seventh in the row, then
        // end the table row and reset $day_offset
        if ($day_offset == 7) {
            $day_offset = 0;
            print "</tr>\n";
            // If there are more days to come, then
            // start a new table row
            if ($day < $days_in_month) {
                print '<tr>';
            }
        }
    }

    // At this point, one table cell has been printed for each day
    // of the month. If the last day of the month isn't a Saturday
    // then the last row of the table needs to be padded with
    // some blank cells out to the end of the row
    if ($day_offset > 0) {
        for ($i = $day_offset; $i < 7; $i++) {
            print '<td>&nbsp;</td>';
        }
        print '</tr>';
    }
    print '</table>';
}
?>
 
i found out what the error is, the calendar is generated outside the form, so that's why the checkboxes aren't submitted.
thanks anyway for your help
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top