28*3600*24) { $ok = false; $error .= '- U bent te vroeg met inschrijven!.'; //too earlie } if((($date1-$date2))';//too late } How to incorate this into one??"> Too earlie or too late (calculate on time) - Forums

Forums

Too earlie or too late (calculate on time)

SPABO 13 Apr, 2010
Dear all,
I have a dropdown with various dates, I wish the user, when he slects a date get's a waring he is too late or too early (as the preiod for a set on certain days)
Pls find the formcode
<div class="form_item">
  <div class="form_element cf_dropdown">
    <label class="cf_label" style="width: 150px;">Wedstrijddatum</label>
    <select class="cf_inputbox validate-selection" id="select_1" size="1" title="Hier uw keuze maken"  name="select_1">
    <option></option>
    <option value="31-01-10"'.(isset($_POST['Wedstrijddatum']) && $_POST['Wedstrijddatum'] == '31-01-10' ? ' selected' : '').'>31-01-10</option> 
     <option value="28-02-10"'.(isset($_POST['Wedstrijddatum']) && $_POST['Wedstrijddatum'] == '28-02-10' ? ' selected' : '').'>28-02-10</option> 
     <option value="28-03-10"'.(isset($_POST['Wedstrijddatum']) && $_POST['Wedstrijddatum'] == '28-03-10' ? ' selected' : '').'>28-03-10</option>
     <option value="18-04-10"'.(isset($_POST['Wedstrijddatum']) && $_POST['Wedstrijddatum'] == '18-04-10' ? ' selected' : '').'>18-04-10</option> 
     <option value="16-05-10"'.(isset($_POST['Wedstrijddatum']) && $_POST['Wedstrijddatum'] == '16-05-10' ? ' selected' : '').'>16-05-10</option>
     <option value="13-06-10"'.(isset($_POST['Wedstrijddatum']) && $_POST['Wedstrijddatum'] == '13-06-10' ? ' selected' : '').'>13-06-10</option> 
     <option value="18-07-10"'.(isset($_POST['Wedstrijddatum']) && $_POST['Wedstrijddatum'] == '18-07-10' ? ' selected' : '').'>18-07-10</option>    </select>
    <a class="tooltiplink" onclick="return false;"><img height="16" border="0" width="16" class="tooltipimg" alt="" src="components/com_chronocontact/css/images/tooltip.png"/></a>
    <div class="tooltipdiv">Datum :: Kies datum</div>
  </div>
  <div class="cfclear">Β </div>
</div>


When i used my old script I used this to "calculate
$error = '';

if($_SERVER['REQUEST_METHOD'] == 'POST')  //Validate form
     
function getTime($time) 
{ 
$arr = explode("-", $time); 
$time = mktime(0,0,0,$arr[1], $arr[0], $arr[2]); 
return $time; 
}
$date1 = getTime($_POST['Wedstrijddatum']); 
$date2 = getTime(date("d-m-y")); 

if((($date1-$date2))>28*3600*24) { 
$ok = false; 
        $error .= '- U bent te vroeg met inschrijven!.<br />'; //too earlie
 } 

if((($date1-$date2))<5*3600*24) { 
 $ok = false; 
        $error .= '- U bent te laat met inschrijven!.<br />';//too late
 } 


How to incorate this into one??
GreyHead 13 Apr, 2010
Hi spabo,

The script should work in the server-side validation box. You'll need to make a few changes to fit in with the serverside format.

Bob
SPABO 15 Apr, 2010
Dear Bob
I can't solve it!!
I get this message:
Parse error: syntax error, unexpected $end in /storage/mijndomein/users/087742/public/sites/www.spabo-pp.nl/components/com_chronocontact/libraries/customcode.php(64) : eval()'d code on line 30.
==========
But also : It sends out the email with details !
Beste Kareltje
Hartelijk dank voor jouw inschrijving, een bevestiging wordt gestuurd naar het email-adres wat u heeft opgegeven.


Sorry to bother again, but this is in HTML code
<div class="form_item">
  <div class="form_element cf_dropdown">
    <label class="cf_label" style="width: 150px;">Wedstrijddatum</label>
    <select class="cf_inputbox validate-selection" id="select_6" size="1" title="Wedstr datum niet ingevuld"  name="Wedstrijddatum">
    <option value="">Wedstrijddatum</option>
      <option value="10-01-2010">10-01-2010</option>
<option value="10-05-2010">10-05-2010</option>
<option value="10-12-2010">10-12-2010</option>

    </select>
    <a class="tooltiplink" onclick="return false;"><img height="16" border="0" width="16" class="tooltipimg" alt="" src="components/com_chronocontact/css/images/tooltip.png"/></a>
				<div class="tooltipdiv">Wedstrijddatum :: Hier de wedstrijddatum invullen</div>
  </div>
  <div class="cfclear">Β </div>
</div>


And I have put this in On Submit Code BEFORE sending email
<?php
session_start();
$filename = basename($_SERVER['PHP_SELF']);
// Errors
$error = '';
if($_SERVER['REQUEST_METHOD'] == 'POST')  //Validate form    
    {
    if(!$_POST['Wedstrijddatum']) { 
        $ok = false; 
        $error .= '- U heeft geen wedstrijddatum ingevuld.<br />';    
     }
//hier dus
function getTime($time) 
{ 
$arr = explode("-", $time); 
$time = mktime(0,0,0,$arr[1], $arr[0], $arr[2]); 
return $time; 
}
$date1 = getTime($_POST['Wedstrijddatum']); 
$date2 = getTime(date("d-m-y")); 
if((($date1-$date2))>28*3600*24) { 
$ok = false; 
        $error .= '- U bent te vroeg met inschrijven!.<br />';
	} 
if((($date1-$date2))<6*3600*24) { 
	$ok = false; 
        $error .= '- U bent te laat met inschrijven!.<br />';
	}
?>


So , wher does it o wrong??
GreyHead 15 Apr, 2010
Hi spabo,

Looks like you are missing (at least) the final closing } in the OnSubmit Before code.

Bob
SPABO 15 Apr, 2010
Correct indeed, however, the code does not calculate ! (And therefore also still submits the form)😟 and emails are sent out to the adm and the confirmation to sender
GreyHead 15 Apr, 2010
Hi spabe,

True, but if you check my post you'll see that it needs to ge into the ServerSide Validation Box AND needs to be modified to the Server-side validation format.

Bob
SPABO 15 Apr, 2010
I'm getting puzzled now..
I did see this "ServerSide Validation Box AND needs to be modified to the Server-side validation format", but sorry, no idea what and where I should something in...
Pls bear in mind the is the first I use Chronoforms
GreyHead 15 Apr, 2010
Hi Spabo,

The Server-side validation box is on the bottom of the Validation tab.

Bob
SPABO 15 Apr, 2010
It drives me crazy...........
In the Valdition tab there is now:
Run Validation onlyOnSubmit YES
Enable Server Side Validation YES

I have removed the code from OnSubmit before sendeing emails and put the code
<?php
session_start();
$filename = basename($_SERVER['PHP_SELF']);
// Errors
$error = '';
if($_SERVER['REQUEST_METHOD'] == 'POST')  //Validate form    
    {
    if(!$_POST['Wedstrijddatum']) { 
        $ok = false; 
        $error .= '- U heeft geen wedstrijddatum ingevuld.<br />';    
     }
//hier dus
function getTime($time) 
{ 
$arr = explode("-", $time); 
$time = mktime(0,0,0,$arr[1], $arr[0], $arr[2]); 
return $time; 
}
$date1 = getTime($_POST['Wedstrijddatum']); 
$date2 = getTime(date("d-m-y")); 
if((($date1-$date2))>28*3600*24) { 
$ok = false; 
        $error .= '- U bent te vroeg met inschrijven!.<br />';
	} 
if((($date1-$date2))<6*3600*24) { 
	$ok = false; 
        $error .= '- U bent te laat met inschrijven!.<br />';
	}}
?> 


In : Server Side validation Code

And it still sends out emails and it still does not calculate/validate !
GreyHead 15 Apr, 2010
Hi SPABO,

AND needs to be modified to the Server-side validation format.



Bob
SPABO 15 Apr, 2010
I'm about to give up !!!!

What Serverside validation format ??

In the General tab I put this d-m-Y
In the HTML is eg 01-01-2010
Gettimesays (d-m-y)

What more shoud i do???
GreyHead 15 Apr, 2010
Hi Spabo,

There's an example right by the Serverside validation box and a dozen or two more in the forums here.

Bob
SPABO 15 Apr, 2010
Thanks, indeed this does not solve the problem.
Expected a bit more actually..
SPABO 16 Apr, 2010
Dear Bob
I have been trying and trying, I "think" I followed some instructions, but I cannot find info on validation a selected date
In the HTML you can select
name="Wedstrijddatum">
<option value="">Wedstrijddatum</option>
<option value="10-01-2010">10-01-2010</option>
<option value="10-05-2010">10-05-2010</option>
<option value="10-12-2010">10-12-2010</option>
So by the nam Wedstrijddatum the values are posted ($POST)

I went to the Valdationtab
Item 9 I put this is : 10-01-2010,10-05-2010,10-12-2010
Q1 : Necessary ?
In the Validationcode
<?php
$date1 = ($_POST['Wedstrijddatum']); 
 
$date2 = (date("dd-mm-yyyy")); 

if((($date1-$date2))>28) { 
$ok = false; 
        return '- Too early to subscribe!.<br />'; //too earlie
 } 

if((($date1-$date2))<5) { 
 $ok = false; 
        return '- Too late to subscribe!.<br />';//too late
 } 
?>

But it "validates" and so a message after submit, but whatever I select, it always says : Too late
So, what am I missing....
nml375 16 Apr, 2010
Hi,
If I may come with a suggestion; rather than simply returning an error stating too early/too late, for debugging purposes, add the values of the dates, the tests, and the calculations. This way, you can 'see' what is generating the unexpected condition.

That said, date() returns a formatted string - perhaps not the most suitable thing for mathematical expressions. Personally, I'd convert the dates into a 'unixtime' timestamps, which are integers (numbers) counting the number of seconds since Jan 1st, 1970. As there are 86400 seconds to a day, converting the diff. in seconds into days is then trivial.

/Fredrik
nml375 16 Apr, 2010
Hi again,
I've had some time to look deeper at your code:
The first few attempts fail because you simply set a value to $error, which doesn't mean anything to ChronoForms. The ServerSide Validation code is expected to return a string describing the error if the validation fails.

In your last attempt, you got this part right, but instead your math is severely flawed; date("dd-mm-yyyy") will return a string such as "1616-0404-10101010", not "16-04-1010". When php tries to make sense of this in the context of subtractions, it will try to convert this into an integer. Reading up on php, we learn that it takes the digits it finds up until the first non-digit character (in our case -). So the above "date" would be converted into the integer 1616. If we then advance this date 7 days, we get "2323-0404-10101010" which yields the integer 2323. Now, 2323-1616 = 707, quite alot larger than 28, and thus you get "Too early to subscribe".

I'd probably use a piece of code like this (untested):
<?
$then = JFactory::getDate(JRequest::getString('Wedstrijddatum', 'now'));
$now = JFactory::getDate();
$diff = $then->toUnix() - $now->toUnix();

if ($diff > 2419200) {
  return '- Too early to subscribe!';
} elseif {$diff < 432000) {
  return '- Too late to subscribe!';
}
?>


/Fredrik
SPABO 16 Apr, 2010
Haleluja!!!!!

Small detail : line 8 { should be (

ANYWAY GREAT, THANK YOU THANK YOU
πŸ˜€ πŸ˜€ πŸ˜€ πŸ˜€
This topic is locked and no more replies can be posted.