0

我目前正在构建我的餐厅网站,其中包含的 PHP 表单非常有限。这让我对保留感到有些头疼。我想做的是在每次预订之前创建一个 24 小时的限制。因此,如果是 2013 年 10 月 10 日下午 1 点,则用户无法在 2013 年 10 月 10 日下午 1:30 创建预订。用户至少可以在 2013 年 11 月 10 日下午 1 点进行预订。

失败时,我希望它打印一条错误消息,就像其他 if 语句中是否有失败一样。

我一直在考虑包括这样的东西,但不知道从哪里开始。

<?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

以下是php预订(联系表格)

<?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 = 'designesia@gmail.com';
            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 <noreply@somedomain.com>';
                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(); ?>

请指导我正确的方向。

编辑:

这是新修改的表格,它将所有输入日期的值视为错误。Xor,我使用了您提供的代码并修改为我认为正确的代码。我还注意到表单没有提供代码中定义的任何错误。

以下是我网站的链接,格式为: http: //tinyurl.com/pf8t9u4

编辑#2:

以下是有效的新代码,它不允许在 24 小时内预订。但是,我试图让它显示关于 24 小时时间故障的错误消息。我模仿了使用标记变量显示成功消息的原始方法,但是它失败了。我创建了 dateZoneError 和 date_failure_message。错误是

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

编码:

<?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 = 'designesia@gmail.com';
            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 <noreply@somedomain.com>';
                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(); ?>
4

1 回答 1

1

您应该更准确地了解您的问题,例如您在数据库表中保存日期的列类型,无论是日期时间还是时间戳等,但无论如何,逻辑是检查当前时间是小于还是大于 24 小时的预订时间。如果少于 24 小时传递错误,否则添加到数据库

    $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 --------*/

似乎它是一个语法错误,你可能错过了一个右花括号或其他东西。但老实说,您的错误解析方法太复杂了。我建议您将错误存储在一个数组中,并在将数据添加或插入数据库之前检查它是否为空。例如:

    $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;
        }
    }
于 2013-10-05T10:15:03.607 回答