I have My request parameters in the URL of current page. When I click on submit in the same page, parameters lost got lost in the new request. How to retain the request parameters even after submitting the form?
You can read the query params from the url and add them as a hidden fields into the form you are submitting. This will send the query string params along with the form.
Send the request parameters as form's GET params
Have the server set the parameters in a cookie so it is available in every page.
Put it in browser's local storage.
If submitting the form via POST, you can include request parameters in the form's action attribute, eg
<form method="post" action="action?id=123&foo=bar">
<input type="text" name="baz">
<input type="submit">
</form>
What you are asking for is state management. It can be done in several ways
HttpSession
Cookies
Hidden fields
URL rewriting.
Hidden fields is an easy way to do it; although the HTML can get more lengthy. I personally prefer HttpSession.
Related
I am working on example.com, when some one logged on my site I would also want to check his authentication on abc.com, abc.com accepts POST request to check authentication.
I was thinking to use an iframe which auto submit a request to abc.com (along with exaple.com ) and set a cookie in abc.com domain too.
Please suggest me if there is any better way to handle this?
Could some one reply with an example how to submit an iframe with post params?
Thanks in advance!
Not sure that iFrame is the best practice here, but you can render a form inside your iFrame and submit it onload.
Of course you will need to pass some data to the form, so you can pass it via the src url with query string params and grab it inside the iFrame.
For post request you will need to set the form (inside the iFrame) as method="post"
You can set a form's target attribute to submit to an iframe:
<form name="frm1" id="frm1" action="http://someurl.com" method="post" target="myiframe">
You may also consider just using an ajax call with jQuery instead and set the httpmethod to "post", which is what I would do.
So i am running a JX Browser which allows content to be shown in an iframe. When someone logs into my ticketing software i want to post their username and password to another form in an iframe.
Basically the iframe contains a page in which i want them to be automatically logged in to. The login credentials for the ticketing software and the page are the same all i want to do is pass that credential to an iframe in which there is username and password field.
Like in jquery you can get .val of the what every is submitted and just send it to another form field.. i want to do that..
What is the best practice?
You can set target attribute on a form to point it to an <iframe> by name.
For example:
<form action="process_login_url" method="post" target="iframelogin" id="loginform">
<input name="login" type="hidden" value="login" />
<input name="password" type="hidden" value="p4ssw0rd" />
</form>
<iframe name="iframelogin">
<script>
// automatically post the form
document.getElementById('loginform').submit()
</script>
You need to pre-populate the hidden fields with user login and password, then the form is automatically sent to the iframe. action on your form has to be the other login processing url, not the login form itself (if these are separate). So basically the action attribute of the external form. Names of the input fields also have to match the target form's names.
Example: http://jsfiddle.net/297suggf/2/ - note the iframe has no src attribute, it loads the url from action on the form with the POST data passed with the request. (It's some random website that allows testing POSTs).
I don't think it's a great idea, and it may not work if there's a protection on the receiving end (like a CSRF token), but youu may give it a shot if it's a last resort type situation
I have a submit input in a form with an onclick method. The onclick method correctly adjusts the action of the form and allows the form to submit (submission is handled naturally, not through a javascript submit). What I need to do is add a http request header (X-Requested-With = XMLHttpRequest to be exact). Is there a way to ensure the form post is sent with that header? The post cannot be submitted via javascript using the form.submit() method.
According to this answer what you're asking for is impossible in its current form. However you could modify where the form submits too. For example POST to:
www.mysite.com/XMLHttpRequest or www.mysite.com/NormalRequest
so that the server understands the context of the request.
I have a page with a regular form:
<form method="post" action="pro.asp">
<input type="text">
<input type="submit">
</form>
when it gets to the pro.asp page it will do some server side actions like sending mail and then I need that page to send all the parameters in the post request to another website
I don't want to have another form filled auto and auto submit with javascript script
Is there a solution for that?
Do you have to make a POST to the other website?
The simplest thing to do may be to, when you are finished your own form processing, send the values to the other page as a GET.
To do that all you would need to do is end your processing with a response.redirect.
Response.Redirect ("http://somewebsite.com?value=X")
I have a use case where i have to post the form data on a particular link and then forward/redirect the request to another link.
For e.g. In my page i have 3 textfields a,b,c and a submit button. On click of Submit i will post the request to say "http://www.abc.com/example" and then redirect the request to "http://www.def.com".
So for the end user after submitting the request he/she will see "http://www.def.com" and will not come to know that what happened in between.
We do not care what the response is from server where request is posted.
Any help/directions in implementing this use case will be highly appreciated.
Thanks.
Regards,
Mayank
Just specify the submit URL in form's action.
<form action="http://www.abc.com/example" method="post">
and let the code behind that URL redirect the request to the desired URL after postprocessing the request; the following example assumes that it's using the Servlet API:
response.sendRedirect("http://www.def.com");
No need for JavaScript here which would not work anyway on JS-disabled clients.
It would be preferable for performance to submit the form to your original server and then redirect to the other URL.
If you can't do this, then you could submit the request via AJAX request and on completion (success or error) change the window.location to the other URL.