Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Submitting date along with form

Status
Not open for further replies.

crazybeans128

Programmer
Aug 5, 2003
79
US
I have a form, and I want to submit it. Easy enough, right? Well before it submits, i want the button to gather the date and send it along. I currently have a text field in the form with a button next to it that updates the date. Then i can click the submit button and it will send those all through. But i want it to send the date and time along with just by clicking the button. So, any suggestions?

 
you'll be getting whatever date the user has their machine set to. why not get the date server-side from your server?


-jeff
try { succeed(); } catch(E) { tryAgain(); } finally { rtfm(); }
i like your sleeves...they're real big
 

I am thinking you have something like this at the moment:
Code:
<input name="myDate" value="" />
<input type="button" value="Set Date" onclick="setTheDate()" />
<input type="submit" value="Submit Form" />

It doesn't matter that we haven't defined setTheDate() -- since you have already said you can do this part and that it currently works.

If your code looked like the one above, then you could change it to do what you want using just the single click (when you submit).
Code:
<input name="myDate" value="" />
<input type="button" value="Set Date" onclick="setTheDate()" />
<input type="[COLOR=red]button[/color]" value="Submit Form" [COLOR=red]onclick="setTheDate();this.form.submit();" [/color] />

Of course it is probably better if you do it like this so you can do other things before you submit the form:
Code:
<input name="myDate" value="" />
<input type="button" value="Set Date" onclick="setTheDate()" />
<input type="submit" value="Submit Form" [COLOR=red]onclick="return preSubmitCheck()" [/color] />

<script type="text/javascript">
function preSubmitCheck() {
  // put the date into the text area
  setTheDate();
  // do some validation maybe - return false to stop form submit
  return true;
}
</script>

Or... you can post the relevant HTML and JS code and we can steer you from there :)

Jeff

 

sounds like the answer to your situation is using hidden fields...upon submitting the form, you run a quick script that sets the value of the hidden field dynamically, then submits the form with the new, updated hidden field value.

something like this:

Code:
<script>
function showValues() {
name=document.getElementById('testName').value;
theDate=document.getElementById('testDate').value;
alert("You are submitting "+name+" as the name and "+theDate+" as the date.");
}
function setDateX() {
now=new Date();
month=now.getMonth();
day=now.getDate();
year=now.getYear();
dateX=month+'/'+day+'/'+year;
document.getElementById('testDate').value=dateX;
testForm.submit();
}
</script>
<form action='javascript:showValues();' method=post id=testForm>
Enter name: <input type=text id=testName>&nbsp;<input type=button value=submit onclick=setDateX();>
<input type=hidden value='' id=testDate>
</form>

this effectively sets the current date to pass through the form...you would change the action='' to the location of your script, i just used the alert so you could see what was being passed.

hope that helps.

- g
 

oops, didn't see that you wanted the time, as well...

Code:
<script>
function showValues() {
name=document.getElementById('testName').value;
theDate=document.getElementById('testDate').value;
theTime=document.getElementById('testTime').value;
alert("You are submitting "+name+" as the name, "+theDate+" as the date and "+theTime+" as the time.");
}
function setDateX() {
now=new Date();
hour=now.getHours();
minutes=now.getMinutes();
month=now.getMonth();
day=now.getDate();
year=now.getYear();
dateX=month+'/'+day+'/'+year;
timeX=hour+':'+minutes;alert(timeX);
document.getElementById('testDate').value=dateX;
document.getElementById('testTime').value=timeX;
testForm.submit();
}
</script>
<form action='javascript:showValues();' method=post id=testForm>
Enter name: <input type=text id=testName>&nbsp;<input type=button value=submit onclick=setDateX();>
<input type=hidden value='' id=testDate>
<input type=hidden value='' id=testTime>
</form>

hope that helps.

- g
 
So i currently like the second idea the best, the one that runs one function, then the other. but someone else spoke of getting the server time. how would i go about gathering the server time?

 

use server side scripting...i prefer perl and to get the server time is fairly easy...

do you use another SS language?

- g
 
yeah, i am incorporating this into a PHP script. What i'm doing is making a posting page. It will have a few fields that get sent to the PHP script, and one of those fields is the date and time.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top