Sagewire Logo

Adding Date/Time to an HTML form? - Request_Information_b.html (0/1)

6 Message(s) by 2 Author(s) originally posted in javascript


From: David Date:   Tuesday, June 19, 2007
I'm trying to add a date and time to an html form, but I'm having a
bit of trouble getting it working. Any suggestions?

Thanks!
~ David (merlin001_at_gmail_dot_com)


From: Scott Bryce Date:   Wednesday, June 20, 2007
wrote in message:

I'm trying to add a date and time to an html form, but I'm having a
bit of trouble getting it working. Any suggestions?



Post your code , or a link to your site. People can not debug code they
can not see.


From: David Date:   Wednesday, June 20, 2007
Sorry, I'm still learning newsgroups. Okay, the text follows. I'm just trying
to get a date/time as a field in the submitted form.

<html><head><title>Request Information</title>
<script type="text/JAVAscript">
var currentHour = "";
var currentMinute = "";
var currentSeconds = "";
var DateAndTime = "";
var AMPM = "";
var currentTime = "";
var currentDate = "";
var currentMonth = "";
var newMonth = "";
var currentDay = "";
var currentYear = "";
function getCurrentTime() {
DateAndTime = new Date();
currentDate = getCurrentDate();
currentHour = DateAndTime.getHours();
if (currentHour >= 12)
{ AMPM = "PM"; currentHour = currentHour - 12; }
else if (currentHour == 0)
{ AMPM = "AM"; currentHour = 12; }
else
{ AMPM = "AM"; }
currentMinute = DateAndTime.getMinutes();
currentSeconds = DateAndTime.getSeconds();
if (currentSeconds < 10)
{ currentSeconds = "0" + currentSeconds; }
currentTime = currentHour + ":" + currentMinute + ":" + currentSeconds +
" " + AMPM;
return dateAndTime; }

function getCurrentDate() {
currentMonth = DateAndTime.getMonth();
currentDay = DateAndTime.getDay();
currentYear = DateAndTime.getFullYear();
switch (currentMonth) {
case 0: { newMonth = "January"; break; }
case 1: { newMonth = "February"; break; }
case 2: { newMonth = "March"; break; }
case 3: { newMonth = "April"; break; }
case 4: { newMonth = "May"; break; }
case 5: { newMonth = "June"; break; }
case 6: { newMonth = "July"; break; }
case 7: { newMonth = "August"; break; }
case 8: { newMonth = "September"; break; }
case 9: { newMonth = "October"; break; }
case 10: { newMonth = "November"; break; }
case 11: { newMonth = "December"; break; } }
thisDate = newMonth + " " + currentDay + ", " + currentYear;
return thisDate; }
</script>

</head>
<body bgcolor="black" text="white">
<h1 align="center">Request Information</h1>
<form method="post" action="mailto:person@xxxxxxxxxxx">
<h3>Contact Information:</h3>
First name:<input type="text" name="firstname"><br />
Last name:<input type="text" name="lastname"><br />
Company:<input type="text" name="company"><br />
Address 1:<input type="text" name="address1"><br />
Address 2:<input type="text" name="address2"><br />
City:<input type="text" name="city">
Zip Code:<input type="text" name="zipcode"><br />
Phone Number:<input type="text" name="phone"><br />
E-Mail Address:<input type="text" name="email"><br /><br />
<input type="hidden" name="dateTime" value="getCurrentTime()">
<input type="submit" value="Submit" />
<input type="reset" value="Reset" />
</form>
</body>
</html>--
--------------------------------- --- -- -
Posted with NewsLeecher v3.9 Beta 1
Web @xxxxxxxxxxx http://www.newsleecher.com/?usenet
------------------- ----- ---- -- -


From: Scott Bryce Date:   Wednesday, June 20, 2007
wrote in message:

Sorry, I'm still learning newsgroups. Okay, the text follows. I'm just trying
to get a date/time as a field in the submitted form.



<code snipped>

I'm not a JAVAScript guy, but I think I found your problem. It appears
that variable names are case sensitive in JAVAscript.

This line:

return dateAndTime; }



throws an undefined variable error.

Also, the line:

<input type="hidden" name="dateTime" value="getCurrentTime()">

isn't calling the getCurrentTime() function, but setting the value of
the hidden object to the literal "getCurrentTime()".

I think you need to call getCurrentTime using an onload event in the
body tag, and have the getCurrentTime function set the value of the
hidden input tag.

so, replace

> return dateAndTime; }

with

document.forms[0].dateTime.value = DateAndTime;

and change your body tag:

<body bgcolor="black" text="white" onLoad="getCurrentTime()">

I also notice that you are combining HTML and XHTML in the same
document. Pick one (HTML is preferred) and stick with it.


From: David Date:   Wednesday, June 20, 2007
Sweet! That works great! I'm actually still getting my feet wet with
HTML/CSS/JAVAscript editing, so forgove the XHTML/HTML blend. I will keep to only
one.

Thanks again!
~ David--
--------------------------------- --- -- -
Posted with NewsLeecher v3.9 Beta 1
Web @xxxxxxxxxxx http://www.newsleecher.com/?usenet
------------------- ----- ---- -- -


From: Scott Bryce Date:   Wednesday, June 20, 2007
wrote in message:

Sweet! That works great!



I'm glad. I do very little JAVAScript anymore, so I'm glad I was able to
help.> I'm actually still getting my feet wet
with HTML/CSS/JAVAscript editing, so forgove the XHTML/HTML blend.



Let me introduce you to an HTML validator. You'll find it frustrating
at first, but eventually you'll learn to appreciate it. It is sort of
like a metronome for a piano student.

http://validator.w3.org/

XHTML is a dead end. Learn to write HTML 4.01 strict.



Next Message: "Internal" use only form?



Programming | Sports | Autos

copyright 2006
Valid XHTML 1.0 Transitional