Jump to content

Help with PHP form


au_stevo
Go to solution Solved by Ch0cu3r,

Recommended Posts

Hey guys,
 
I need help with the below PHP form. I've managed to get it semi-working thanks to a user on another forum, I've lost the link to the forum but this place looks awesome so I think I'll be staying here.

 

The form came with the theme I'm running for the website, what it was lacking was a minimal 24 hour period in-between the current time and selected booking to prevent people from booking and arriving straight away. I also need to implement a captcha system but have no idea where to start on that.

 
I've managed to get it semi working to the point where it does prevent a booking within 24 hours, however it displays the success message and failure message, instead of just the failure message. The failure message also doesn't use the same formatting as the success message. Can someone please give me a hand in having the form display the error properly, so using the same format as the success message and also please give me a hand with the captcha implementation. 

 

Thank you very much,

Steve

 

Link to form:

 

http://tinyurl.com/pf8t9u4

 

 
Here's my the form 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 = '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"> </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> </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>
		
		<?php } ;?>
		
		  </div>
        <div class="clear"></div>

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

		
    </div>


      

    <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(); ?>
Edited by au_stevo
Link to comment
Share on other sites

Here you are setting the error if the booking time is not greater than 24 hours

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

    }

To stop the email/success message being sent you need to change $dateZoneError = true;  to   $hasError = true;

 

Also the above if/else could be rewritten as just

        if ($booking_time < $time_restriction ) {
            $dateZoneError = 'You need to book 24 hours';
            $hasError = true;
        }
Edited by Ch0cu3r
Link to comment
Share on other sites

Awesome, thanks for that. It's now displaying only the error message upon failure and not the success message. The only issue that still remains is the error message is unformatted, unlike the Success message which has the coloured background around the text. Any idea how I'd sort that out?

Link to comment
Share on other sites

  • Solution

Your are displaying the error in a div that has a CSS class of failure

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

Apply a CSS definition in your stylesheet for .failure class

.failure {background:#F00; padding:10px; margin-top:10px; font-weight:bold; color:#FFF;} /* Display red background with white text */
Edited by Ch0cu3r
Link to comment
Share on other sites

You're an absolute legend, thank you so much! I was looking around for the CSS entries but couldn't find anything, you opened my mind; I realized there's more CSS files that aren't visible straight from wordpress. It all makes sense now and works fine. 

 

Thanks again! You've been a massive help.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.