Hi!
I'm a newbie in javascript but have som e experience from php, but unfortunately not OOP. Any way, I have a site where user shall pick a startDate and an endDate and then click a submit button.
After that I validate the dates in PHP and then I'm going to insert them in a table.
However I would like to do more validation on the client side. What I want to do is follow:
1 Check if startDay is not already passed (already there)
2 Check if endDay is ahead of startDay
3 Check if endDay is no more than 14 days ahead of startDay
Best Regards, muuucho, Sweden
- // Require the configuration before any PHP code as the configuration controls error reporting:
- require ('./includes/config.inc.php');
- // The config file also starts the session
- // Require the database connection:
- require (MYSQL);
- $reg_errors=array();
- // Include the header file:
- include ('./includes/header.html');
- /* PAGE CONTENT STARTS HERE! */
- if (isset($_POST['arr'])){$arr=$_POST['arr'];}else{$arr=NULL;}
- if (isset($_POST['dep'])){$dep=$_POST['dep'];}else{$dep=NULL;}
- echo'<div id="box1">
- <h3>Välj datum</h3>';
- if (isset($_POST['dp'])){
- //validate
-
- $day=$arr;
- //Check that
- if(!preg_match('/\d{4}-\d{2}-\d{2}$/', $day) or !checkdate(substr($day,5,2), substr($day,8,2), substr($day,0,4))){
- echo '<div class="error">Felaktigt Ankomstdatum!</div><br>';
- $arr=null;
- }elseif((mktime(0,0,0,substr($day,5,2),substr($day,8,2),substr($day,0,4)) < floor(mktime(0, 0, 0, date("m"), date("d"), date("Y"))))){
- echo '<div class="error">Ankomstdag har redan varit. Vänligen försök igen! </div><br>';
- $arr=null;
- }
-
-
-
- $day=$dep;
- if(!preg_match('/\d{4}-\d{2}-\d{2}$/', $day) or !checkdate(substr($day,5,2), substr($day,8,2), substr($day,0,4))){
- echo '<div class="error">Felaktigt Avresedatum!</div>';
- $dep=null;
- }elseif($arr!=null &&(mktime(0,0,0,substr($day,5,2),substr($day,8,2),substr($day,0,4)) <=(mktime(0,0,0,date(substr($arr,5,2)), date(substr($arr,8,2)), date(substr($arr,0,4)))))){
- //datecheck arr/dep not ok
- echo '<div class="error">Avresedag måste väljas senare än ankomstdag. Vänligen försök igen</div><br>';
- $dep=null;
- }elseif ($arr!=null &&(((mktime(0,0,0,date(substr($dep,5,2)), date(substr($dep,8,2)), date(substr($dep,0,4))))-
- (mktime(0, 0, 0, date(substr($arr,5,2)), date(substr($arr,8,2)), date(substr($arr,0,4)))))/86400)>MAX_DAYS){
-
- echo '<div class="error">Det går bara att boka '.MAX_DAYS.' nätter on-line. Vid längre bokningar, vänligen kontakta oss! </div><br>';
- $dep=null;
- }
-
- if($arr && $dep){
- echo"Ankomst: ".$arr;
- $start_ts = strtotime($arr);
- $end_ts = strtotime($dep);
- $diff = $end_ts - $start_ts;
- $stay= round($diff / 86400);
- echo "<br>Vill stanna $stay dag/dagar";
- }
- }
- ?>
- <form name="datepicker" action="index.php" method="post">
- <p>Ankomst: <input type="text" name="arr" id="datepicker1" <?php if ($arr){echo'value="'.$arr.'"';}?>/></p>
- <p>Avresa: <input type="text" name="dep" id="datepicker2"<?php if ($dep){echo'value="'.$dep.'"';}?>/><br /></p>
- <input type="submit" class="formbutton" value="Sök lediga rum" name="dp" />
- </form>
- </div>
- <?php
-
- /* PAGE CONTENT ENDS HERE! */
- // Include the footer file to complete the template:
- require ('./includes/footer.html');
- ?>