Forums

howto add a form item

daffy 02 Apr, 2009
Hi I have a simple form with two email entries and two date entries.
I need to know the length in days between the dates.
I have written a server-side validation in php.
<?php
 $date3bits = explode('-', $_POST['date_3']);
 $firstdate = mktime(0,0,0,$date3bits[1],$date3bits[2],$date3bits[0]);
 $date4bits = explode('-', $_POST['date_4']);
 $lastdate = mktime(0,0,0,$date4bits[1],$date4bits[2],$date4bits[0]);
 $today = time();
  if (($today > $firstdate) || ($firstdate > $lastdate)){
    return  "You cannot book before today and the last date cannot be before the  first date";}
  else if ($_POST['text_1'] != $POST['text_2'])
 {
  return "Your email entries do not match";
  }
?>

I have written it in PHP because I cannot get a javascript version to work...my own incompetance !
While I have the dates in the mktime form I would like to calculate the length as
$length = ($lastdate - $firstdate)/ (60*60*24) + 1; //the length of hire  

But having got that I would like to put it together with the other form elements into the database.
Q 1. How do I do that. ?
Q 2. Should I really use javascript ?? if so how?

I am making slow progress from a posiition of total ignorance.
--
Dave
Max_admin 03 Apr, 2009
Hi Dave,

I think we worked in this solution using JS here before ? why did you abandon the JS one ?

Max
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
daffy 03 Apr, 2009
Thanks Max and
Thanks bob regarding my previous post.

I do not have difficulty in manipulating PHP or manipulating Javascript but I have big holes in my knowledge.
Max
you say use datepieces = $('datefield').value.split("-"); as a bit of mootools.
I assume I replace 'datefield' with 'date_3' and later with 'date_4'

So that this is the equivlent, in javascript , of explode('-',$_POST['date_3']); in etc in PHP

Now I can use in Javascript Date1 = new date(datepices[1],datepieces[0] , etc
and I can do Javascript arithmetic with the two dates.

BUT where in the chronoform do I put this Javascript ? And how is it picked up ? In the place marked 'javascript' or in the place marked 'Validation' ?

If I get a result var length = xx;
can I at that point set it as the value of a hidden field ? and thus get this length stored in the database.
[I have to use this length together with a lookup table in the database to get a 'cost' to record].
How does all this relate to global $row_jos_chronoforms_MyForm;
This is an array I assume, can I just add my Length into this array somehow and adjust the table to suit ?

I notice that if I have two hidden fields hidden_1 and hidden_2 in my form I only get one column made in the MakeTable of the form... I have overcome that by deleting that column and making the expected columns using phpMyadmin.

Sorry to be so lengthy but I hate using something I don't understand, But I like Chronoforms so I have to try to understand its workings !!
--
Dave
Max_admin 04 Apr, 2009
Hi Dave,

no problems, lets take it step by step!

you managed to complete the code, this code should be inside a function and then you call this function when some event happens!

so add the function in the JS box of Chronoforms then call it through some event triggered by one of your form normal HTML elements!

show me your final JS code + HTML code and I will show you how if you didn't already managed top finish it!

Regards
Max
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
daffy 04 Apr, 2009
Thanks Max you are very patient !
Here is my attempt at a javascript function

function sortdates()
{
$datepieces3 = $('date_3').value.split; //note date fields are in Y-m-d format
$datepieces4 = $('date_4').value.split;
var date1 = new date($datepieces3[0],$datepieces3[1]-1,$datepieces3[2]);
var date2 = new date($datepieces4[0],$datepieces4[1]-1,$datepieces4[2]);
var today = new date();
if (today > date1) {return -1;
} else {
if(date1 > date2){return-2;
} else {
var oneday =  1000*60*60*24;
var length = (date2 - date1)/oneday + 1;  // counts as one day if dates are same day
return length;
}


So I expect there are errors
but how can I use the error signals -1 and -2 produced and
How can I put the value of 'length' as a hidden field in the form ?

--
Dave
Max_admin 07 Apr, 2009
Hi Dave,

put this function in the JS box and call it using a button click or onChange event or so!

I suggest you add this line below the length line in your code:

var length = (date2 - date1)/oneday + 1;
alert(length);
return length;


this will show you if everything runs fine, use Firebug to see if any errors were reported!

Let me know!

Regards
Max
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
This topic is locked and no more replies can be posted.