Govno
Govno

Reputation: 53

Limit date in PHP form

I'm currently building my restaurant website and the included PHP form is quite limited. It's caused me a few headaches with reservations. What I'd like to do is create a 24 hour limit before each booking can be made. So if it's 1PM on 10/10/2013, a user cannot create a booking at 1:30PM on the 10/10/2013. The user will at minimal be able to make a booking at 11/10/2013 1PM.

On failure, I would like it to print an error message just like if there are failures in the other if statements.

I've been thinking of including something like this, but don't know where to start.

<?php
$t=date("H");
if ($t<"20")
{
echo "“Sorry, you need to book 24 hours in advance, please select another date.”;
}
else
{
$date = trim($_POST['datepick']);
}
?>    

http://www.w3schools.com/php/php_if_else.asp

Below is the php reservation (contact form)

<?php
/**
* Template Name: Reservation
*
*/
$nameError = '';
$emailError = '';
$commentError = '';
$countError = '';
$dateError = '';
$timeError = '';
?>
<?php
    global $de_data;
    $de_data = get_option( 'Lezatos_options' ); 
    $de_email = $de_data['DE_email_contact'];
    $de_time_open  = $de_data ['DE_time_open'];
    $de_time_close  = $de_data ['DE_time_close'];
    $de_time_step  = $de_data ['DE_time_step'];
    $success_message  = $de_data ['DE_booking_success_message'];

?>
<?php 
if(isset($_POST['submitted'])) {


        if(trim($_POST['contactName']) === '') {
            $nameError = 'You forgot to enter your name.';
            $hasError = true;
        } else {
            $name = trim($_POST['contactName']);
        }

        if(trim($_POST['email']) === '')  {
            $emailError = 'You forgot to enter your email address.';
            $hasError = true;
        } else if (!eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($_POST['email']))) {
            $emailError = 'You entered an invalid email address.';
            $hasError = true;
        } else {
            $email = trim($_POST['email']);
        }


            if(function_exists('stripslashes')) {
                $comments = stripslashes(trim($_POST['comments']));
            } else {
                $comments = trim($_POST['comments']);
            }

        if(trim($_POST['person_num']) === '') {
            $countError = 'You forgot to enter your number of people.';
            $hasError = true;
        } else {
            if(function_exists('stripslashes')) {
                $person = stripslashes(trim($_POST['person_num']));
            } else {
                $person = trim($_POST['person_num']);
            }
        }

        if(trim($_POST['datepick']) === '') {
            $dateError = 'You forgot to enter your date.';
            $hasError = true;
        } else {
            if(function_exists('stripslashes')) {
                $date = stripslashes(trim($_POST['datepick']));
            } else {
                $date = trim($_POST['datepick']);
            }
        }

        if(trim($_POST['time']) === '') {
            $timeError = 'You forgot to enter your time.';
            $hasError = true;
        } else {
            if(function_exists('stripslashes')) {
                $time = stripslashes(trim($_POST['time']));
            } else {
                $time = trim($_POST['time']);
            }
        }

        if(!isset($hasError)) {

            if($de_email):
            $email_address = $de_email;
            else:
            $email_address = '[email protected]';
            endif;

            $emailTo = $email_address;
            $subject = 'New Reservation';
            $sendCopy = trim($_POST['sendCopy']);
            $body = "Name: $name \n\nEmail: $email \n\nPerson: $person \n\nDate: $date \n\nTime: $time \n\nComments: $comments";
            $headers = 'From: My Site <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;

            mail($emailTo, $subject, $body, $headers);

            if($sendCopy == true) {
                $subject = 'You emailed Your Name';
                $headers = 'From: Your Name <[email protected]>';
                mail($email, $subject, $body, $headers);
            }

            $emailSent = true;

        }
} ?>


<?php get_header(); ?>
        <div class="container">
            <div class="eight columns">&nbsp;</div>
<div class="eight columns booking_form_holder">
         <?php if(get_option('DE_contact_text')<>''): ?>
              <?php echo stripslashes(get_option('DE_contact_text')); ?>
              <div style=" margin-bottom:20px;"></div>
              <?php endif; ?>

        <form action="<?php the_permalink(); ?>" id="contactForm" method="post">
              <div id="de_form" class="booking_form">
              <table class="table-form">
  <tr>
    <td><?php echo __('Name','Lezzatos'); ?></td>
    <td><input type="text" name="contactName" id="contactName" value="<?php if(isset($_POST['contactName'])) echo $_POST['contactName'];?>" class="requiredField" />
      <?php if($nameError != '') { ?>
      <span class="error"></span>
      <?php } ?></td>
  </tr>
  <tr>
    <td><?php echo __('Email','Lezzatos'); ?></td>
    <td><input type="text" name="email" id="email" value="<?php if(isset($_POST['email']))  echo $_POST['email'];?>" class="requiredField email" />
      <?php if($emailError != '') { ?>
      <span class="error"></span>
      <?php } ?></td>
  </tr>
  <tr>
    <td><?php echo __('Person','Lezzatos'); ?></td>
    <td><input type="text" name="person_num" id="person_num" value="<?php if(isset($_POST['person_num']))  echo $_POST['person_num'];?>" class="requiredField" />
      <?php if($countError != '') { ?>
      <span class="error"></span>
      <?php } ?></td>
  </tr>
  <tr>
    <td><?php echo __('Date','Lezzatos'); ?></td>
    <td><input type="text" name="datepick" id="datepick" value="<?php if(isset($_POST['datepick']))  echo $_POST['datepick'];?>" class="requiredField" />
      <?php if($dateError != '') { ?>
      <span class="error"></span>
      <?php } ?></td>

  </tr>
  <tr>
    <td><?php echo __('Time','Lezzatos'); ?></td>
    <td><input type="text" name="time" id="time" value="<?php if(isset($_POST['time']))  echo $_POST['time'];?>" class="requiredField" />
      <?php if($timeError != '') { ?>
      <span class="error"></span>
      <?php } ?></td>
  </tr>
  <tr>
    <td><?php echo __('Comment','Lezzatos'); ?></td>
    <td><textarea name="comments" id="commentsText" rows="6" cols="30" class="requiredField"><?php if(isset($_POST['comments'])) { if(function_exists('stripslashes')) { echo stripslashes($_POST['comments']); } else { echo $_POST['comments']; } } ?></textarea>
      <?php if($commentError != '') { ?>
      <span class="error"></span>
      <?php } ?></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td><!--<input style="width:24px; display:inline-block;" type="checkbox" name="sendCopy" id="sendCopy" value="true"<?php //if(isset($_POST['sendCopy']) && $_POST['sendCopy'] == true) echo ' checked="checked"'; ?> />Send a copy of this email to yourself
                <input type="text" name="checking" id="checking" class="screenReader" value="<?php if(isset($_POST['checking']))  echo $_POST['checking'];?>" /> -->
      <input type="hidden" name="submitted" id="submitted" value="true" />
      <button class="btn" type="submit"><?php echo __('SEND','Lezzatos'); ?></button></td>
  </tr>

</table>

        </form>
        </div>

        <?php if(isset($emailSent) && $emailSent == true) { ?>

        <div class="thanks">
        <?php 
        if($success_message){ echo $success_message;
        }else{ echo 'Thank You! Your reservation was successfully sent.';
        }
        ?>
        </div>

        <?php } ;?>

        </div>
        <div class="clear"></div>

        </div>
        </div>
        <!-- ********** close content *********** -->

    <script type='text/javascript' src='<?php echo get_template_directory_uri()?>/js/datepickr.js'></script>
    <script type='text/javascript' src='<?php echo get_template_directory_uri()?>/js/jquery.timePicker.js'></script>

        <script type="text/javascript">
            new datepickr('datepick');

            new datepickr('datepick2', {
                'dateFormat': 'm/d/y'
            });

            new datepickr('datepick3', {
                'fullCurrentMonth': false,
                'dateFormat': 'l, F j'
            });
        </script>   

        <script type="text/javascript">
          jQuery(function() {
            $("#time").timePicker({
                step:<?php if($de_time_step){echo $de_time_step;}else{ echo "60";}?>, 
                startTime:"<?php if($de_time_open){echo $de_time_open;}else{ echo "08:00";}?>", 
                endTime:"<?php if($de_time_close){echo $de_time_close;}else{ echo "22:00";}?>"
                });
            });
        </script> 

<?php get_footer(); ?>

Please guide me in the right direction.

EDIT:

This is the newly modified form which is treating all values entered into date as an error. Xor, I used the code you provided and modified to what I thought was right. I've also noticed that the form doesn't provide any of the errors defined within the code.

Below is a link to my website with the form: http://tinyurl.com/pf8t9u4

EDIT #2:

Below is the new code that worked, it wouldn't allow a booking within 24 hours. However, I tried to make it display an error message about the 24 hour time failure. I mimicked the original method of displaying the success message using flagged variables, however it's failing. I created dateZoneError and date_failure_message. The error is

Parse error: syntax error, unexpected $end in /home/sol/public_html/wp-content/themes/lezzatos/page_reservation.php on line 252

The Code:

<?php
/**
* Template Name: Reservation
*
*/
$nameError = '';
$emailError = '';
$commentError = '';
$countError = '';
$dateError = '';
$timeError = '';
$time_restriction = strtotime("+1 day"); // 24 hours from now
$booking_time = strtotime($_POST['datepick']);
$dateZoneError= '';
?>
<?php
    global $de_data;
    $de_data = get_option( 'Lezatos_options' ); 
    $de_email = $de_data['DE_email_contact'];
    $de_time_open  = $de_data ['DE_time_open'];
    $de_time_close  = $de_data ['DE_time_close'];
    $de_time_step  = $de_data ['DE_time_step'];
    $success_message = $de_data ['DE_booking_success_message']; 
    $date_failure_message = $de_data ['DE_date_failure_message'];

?>
<?php 
if(isset($_POST['submitted'])) {


        if(trim($_POST['contactName']) === '') {
            $nameError = 'You forgot to enter your name.';
            $hasError = true;
        } else {
            $name = trim($_POST['contactName']);
        }

        if(trim($_POST['email']) === '')  {
            $emailError = 'You forgot to enter your email address.';
            $hasError = true;
        } else if (!eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($_POST['email']))) {
            $emailError = 'You entered an invalid email address.';
            $hasError = true;
        } else {
            $email = trim($_POST['email']);
        }


            if(function_exists('stripslashes')) {
                $comments = stripslashes(trim($_POST['comments']));
            } else {
                $comments = trim($_POST['comments']);
            }

        if(trim($_POST['person_num']) === '') {
            $countError = 'You forgot to enter your number of people.';
            $hasError = true;
        } else {
            if(function_exists('stripslashes')) {
                $person = stripslashes(trim($_POST['person_num']));
            } else {
                $person = trim($_POST['person_num']);
            }
        }

        if(trim($_POST['datepick']) === '') {
            $dateError = 'You forgot to enter your date.';
            $hasError = true;
        } else {
            if(function_exists('stripslashes')) {
                $date = stripslashes(trim($_POST['datepick']));
            } else {
                $date = trim($_POST['datepick']);
            }
        }

        if ($booking_time > $time_restriction ) {
            // success .. 
        } else {
            $dateZoneError = 'You need to book 24 hours';
            $dateZoneError = true;

    }

        if(trim($_POST['time']) === '') {
            $timeError = 'You forgot to enter your time.';
            $hasError = true;
        } else {
            if(function_exists('stripslashes')) {
                $time = stripslashes(trim($_POST['time']));
            } else {
                $time = trim($_POST['time']);
            }

        }




        if(!isset($hasError)) {

            if($de_email):
            $email_address = $de_email;
            else:
            $email_address = '[email protected]';
            endif;

            $emailTo = $email_address;
            $subject = 'New Reservation';
            $sendCopy = trim($_POST['sendCopy']);
            $body = "Name: $name \n\nEmail: $email \n\nPerson: $person \n\nDate: $date \n\nTime: $time \n\nComments: $comments";
            $headers = 'From: My Site <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;

            mail($emailTo, $subject, $body, $headers);

            if($sendCopy == true) {
                $subject = 'You emailed Your Name';
                $headers = 'From: Your Name <[email protected]>';
                mail($email, $subject, $body, $headers);
            }

            $emailSent = true;

        }
} ?>


<?php get_header(); ?>
        <div class="container">
            <div class="eight columns">&nbsp;</div>
<div class="eight columns booking_form_holder">
         <?php if(get_option('DE_contact_text')<>''): ?>
              <?php echo stripslashes(get_option('DE_contact_text')); ?>
              <div style=" margin-bottom:20px;"></div>
              <?php endif; ?>

        <form action="<?php the_permalink(); ?>" id="contactForm" method="post">
              <div id="de_form" class="booking_form">
              <table class="table-form">
  <tr>
    <td><?php echo __('Name','Lezzatos'); ?></td>
    <td><input type="text" name="contactName" id="contactName" value="<?php if(isset($_POST['contactName'])) echo $_POST['contactName'];?>" class="requiredField" />
      <?php if($nameError != '') { ?>
      <span class="error"></span>
      <?php } ?></td>
  </tr>
  <tr>
    <td><?php echo __('Email','Lezzatos'); ?></td>
    <td><input type="text" name="email" id="email" value="<?php if(isset($_POST['email']))  echo $_POST['email'];?>" class="requiredField email" />
      <?php if($emailError != '') { ?>
      <span class="error"></span>
      <?php } ?></td>
  </tr>
  <tr>
    <td><?php echo __('Person','Lezzatos'); ?></td>
    <td><input type="text" name="person_num" id="person_num" value="<?php if(isset($_POST['person_num']))  echo $_POST['person_num'];?>" class="requiredField" />
      <?php if($countError != '') { ?>
      <span class="error"></span>
      <?php } ?></td>
  </tr>
  <tr>
    <td><?php echo __('Date','Lezzatos'); ?></td>
    <td><input type="text" name="datepick" id="datepick" value="<?php if(isset($_POST['datepick']))  echo $_POST['datepick'];?>" class="requiredField" />
      <?php if($dateError != '') { ?>
      <span class="error"></span>
      <?php } ?></td>

  </tr>
  <tr>
    <td><?php echo __('Time','Lezzatos'); ?></td>
    <td><input type="text" name="time" id="time" value="<?php if(isset($_POST['time']))  echo $_POST['time'];?>" class="requiredField" />
      <?php if($timeError != '') { ?>
      <span class="error"></span>
      <?php } ?></td>
  </tr>
  <tr>
    <td><?php echo __('Comment','Lezzatos'); ?></td>
    <td><textarea name="comments" id="commentsText" rows="6" cols="30" class="requiredField"><?php if(isset($_POST['comments'])) { if(function_exists('stripslashes')) { echo stripslashes($_POST['comments']); } else { echo $_POST['comments']; } } ?></textarea>
      <?php if($commentError != '') { ?>
      <span class="error"></span>
      <?php } ?></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td><!--<input style="width:24px; display:inline-block;" type="checkbox" name="sendCopy" id="sendCopy" value="true"<?php //if(isset($_POST['sendCopy']) && $_POST['sendCopy'] == true) echo ' checked="checked"'; ?> />Send a copy of this email to yourself
                <input type="text" name="checking" id="checking" class="screenReader" value="<?php if(isset($_POST['checking']))  echo $_POST['checking'];?>" /> -->
      <input type="hidden" name="submitted" id="submitted" value="true" />
      <button class="btn" type="submit"><?php echo __('SEND','Lezzatos'); ?></button></td>
  </tr>

</table>

        </form>
        </div>

        <?php if(isset($emailSent) && $emailSent == true) { ?>

        <div class="thanks">
        <?php 
        if($success_message){ echo $success_message;
        }else{ echo 'Thank You! Your reservation was successfully sent.';
        }
        ?>
        </div>

        <?php } ;?>

        <?php if(isset($dateZoneError) && $dateZoneError== true) { ?>

        <div class="failure">
    <?php
    if($date_failure_message){ echo $date_failure_message;
        }else{ echo 'You need to allow 24 hours between now and the selected booking date.';
    }
    ?>
    </div>


        </div>
        <div class="clear"></div>

        </div>
        </div>
        <!-- ********** close content *********** -->

    <script type='text/javascript' src='<?php echo get_template_directory_uri()?>/js/datepickr.js'></script>
    <script type='text/javascript' src='<?php echo get_template_directory_uri()?>/js/jquery.timePicker.js'></script>

        <script type="text/javascript">
            new datepickr('datepick');

            new datepickr('datepick2', {
                'dateFormat': 'm/d/y'
            });

            new datepickr('datepick3', {
                'fullCurrentMonth': false,
                'dateFormat': 'l, F j'
            });
        </script>   

        <script type="text/javascript">
          jQuery(function() {
            $("#time").timePicker({
                step:<?php if($de_time_step){echo $de_time_step;}else{ echo "60";}?>, 
                startTime:"<?php if($de_time_open){echo $de_time_open;}else{ echo "08:00";}?>", 
                endTime:"<?php if($de_time_close){echo $de_time_close;}else{ echo "22:00";}?>"
                });
            });
        </script> 

<?php get_footer(); ?>

Upvotes: 0

Views: 2435

Answers (1)

XoR
XoR

Reputation: 132

You should have been more precise about your question like the column type you are saving your date in the database table, whether it is date time or timestamp etc, but anyway the logic is to check whether the current time is lesser or greater than 24 hours of booking time. if lesser than 24 hours pass an error, or else add to the database

    $time_restriction = strtotime("+1 day"); // 24 hours from now
    $booking_time = strtotime($_POST['datepick']); // time of checking in

    if ($booking_time > $time_restriction ) {
        // success .. 
    } else {
        // error
        // you cannot book prior to 24 hours

    }

    /*edit --------*/

seems like its a syntax error, you might have missed a closing curly braces or something. but to be honest your error parsing method is too complicated. I would suggest you to store the errors in an array and check if its empty before you add or insert the data's into the database. for ex :

    $errors = array();
    if(trim($_POST['contactName']) === '') {
        $errors[] = 'You forgot to enter your name.';
    } else {
        $name = trim($_POST['contactName']);
    }
    if(trim($_POST['datepick']) === '') {
        $errors[] = 'You forgot to enter your date.';

    } else {
        if(function_exists('stripslashes')) {
            $date = stripslashes(trim($_POST['datepick']));
        } else {
            $date = trim($_POST['datepick']);
        }
    }       
    if(empty($errors)) {
        // add datas into the database
    } else {
        // errors found
        foreach ($errors as $error ) {
            echo $error;
        }
    }

Upvotes: 1

Related Questions