Reputation: 35
Please have a look at the below code
<script>
$(document).ready(function(){
$("form").submit(function(){
var text = document.getElementsByName('text')[0].value;
var action = $("form")[0].action;
action = action+"?date="+new Date();
alert(action);
});
});
</script>
</head>
<body>
<form name="test" method="post" action="B">
<input name="text" type="text">
<input type="submit" value="Submit">
</form>
I am trying to attach the time of the form being submitted to the URL, before it is being sent to the servlet. But evrytime in my servlet, the time is not showing and in the browser URL also the date
variable is not displayed.
My servlet code is below for the reference
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try
{
String print=request.getParameter("date");
out.print(print);
} finally
{
out.close();
}
}
How can I send the time of the form being submitted to URL either via URL or via any other method?
Upvotes: 2
Views: 2846
Reputation:
You can simply add a hidden field to capture date:
var putDate = function(form) {
form.date.value = new Date().toString();
};
<form name="test" method="post" action="B" onsubmit="putDate(this);">
<input name="text" type="text" />
<input name="date" type="hidden" />
<input type="submit" value="Submit">
</form>
Upvotes: 0
Reputation: 1075209
These lines:
var action = $("form")[0].action;
action = action+"?date="+new Date();
...just update your action
variable. That variable has no ongoing connection to the form's action
property.
You also don't need to look up the form again, it's this
in your submit
callback.
To update the form's action
property, you'd have to assign to it:
this.action = this.action + "?" + encodeURIComponent(new Date());
Note that that will send the date in a browser-dependent format, which is not likely to be useful. Instead, perhaps:
this.action = this.action + "?" + encodeURIComponent(new Date().toISOString());
Separately, you're doing this in a submit
handler. It may be worth double-checking that that is early enough on your various target browsers. If not, do it on the submit
button's click
instead:
$("form input[type=submit]").on("click", function() {
this.form.action = this.form.action + "?" + encodeURIComponent(new Date().toISOString());
});
Or, as an alternative to appending it to the URL, you could have a hidden field in the form:
<input name="date" type="hidden">
...which you fill in from the submit
handler:
$(this).find("input[name=date]").val(new Date().toISOString());
Side note: All information sent from the client can be spoofed, so take the date/time you receive with a grain of salt. In particular, you can't rely on it to know when the form was actually sent. To know that, use code on the server that records the date/time it was received, which will only be milliseconds later.
Upvotes: 3
Reputation: 175916
After adding the correct ID to the form add a hidden input;
<input id="clientDate" name="clientDate" type="hidden">
and use:
$("#form").submit(function(){
$("#clientDate").val(new Date());
});
Upvotes: 0