Forums

Prefilling a field with today's date

Robmcp 03 Feb, 2009
Hi can anyone offer a complete newbie some information on prefilling a form with a date? (today in this instance)

I have a site, Joomla 1.0, not live yet + Chronoman 2.3.9 (?)

I found some Javascript that appears to fit the bill, and works as a standalone piece of code, in that when you view it in a browser the field is nicely filled with the date in dd/mm/yyyy format.

However when I paste the script, minus the script tags into the Form Code Tab/ Form Javascript area, it does not work.
The code enclosed in the <script> tags is :
var mydate = new Date()
var theyear = mydate.getYear()
if (theyear < 1000)
theyear += 1900
var theday = mydate.getDay()
var themonth = mydate.getMonth() + 1
if (themonth < 10)
themonth = "0" + themonth
var theday = mydate.getDate()
if (theday < 10)
theday = "0" + theday

var dd_part = themonth
var mm_part = theday
var yyyy_part = theyear
document.application_form.dateofregistration.value = dd_part + "/" + mm_part + "/" + yyyy_part

]And the form code is
<form name="application_form">
<label for="dateofregistration" class="left">Date Today (DD/MM/YYYY):</label>
<input type="text" name="dateofregistration" size=11>
</form>

Can anyone tell me if I'm missing something in the way the script should go into the Chronoforms admin area? I don't think it is syta, because the above works standalone. But having been wrong before on allsorts........
Thanks in advance.
Robmcp
GreyHead 03 Feb, 2009
Hi Robmcp,

In the Form HTML set the value of the field
<input type='text' . . . value='<?php echo date('d/m/Y'); ?>' . . ./>

Bob

PS I think I have the correct format string but if not you'll find it in the PHP manual.
Robmcp 03 Feb, 2009
Thank you, I will try that. (As you may have guessed I'm not a PHP person) Can you tell me what the purpose of the Javascript area is for in Chronoforms, if not for this sort of thing?
RobMcp?
Max_admin 03 Feb, 2009
Hi,

to add the JS code you will need to have it ready and dependent on your new form, for example, in the code there is : application_form, this should be changed to your new form name and ChronoContact_ before it : ChronoContact_formname

so, to save everybody's time here, Bob suggested the easy and fast way to do it, the better way too because the JS may fail sometimes or may be disabled at some browsers!

Cheers
Max
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
GreyHead 04 Feb, 2009
Hi Robmcp,

JavaScript operates in the browser - and because every browser is slightly different and every webpage loads different combinations of scripts JavaScript is always less reliable than using PHP on the server where you have control over the environment.

That said, JavaScript is invaluable for interaction with the user without sending the page back to the server - so for validation, interaction, etc JavaScript is the way to go and, yes, you can add it in the JavaScript box (there are other, perhaps better ways of doing so though).

In this case, there is no interaction involved, and you know the date before you serve the page to the browser so it's much simpler and more reliable to set it with the little PHP snippet than to do it with JavaScript.

Hope that helps a bit.

Bob
This topic is locked and no more replies can be posted.