Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Form Submission on drop down onchange event

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ranchers,

Following is my problem:
There is a drop down menu. and onchange event a javascript function is called.
In the javascript function, I am doing 3 things:
1)alerting "before submit"
2)submitting the form
3)alerting "after submit"



But both the alerts get printed and then the form gets submitted.
What I want is:
1) Print the alert -- "Before Submit"
2) Submit the form (Thus reload the page)
3) Print the alert -- "After Submit"


Can you please guide me in achieving this?

Thanks a lot
 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The form is submitted and it continues on. It is a asynchronous action.

Also the form submission causes the page to exit and anything after the submit() call is not guaranteed to run.

Eric
 
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use AJAX to send over your request to the server and receive and process the response.
Alternatively, when the page is loaded, you must pick the new value in JSP code itself somehow.
 
Matt Thomassan
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@ James

I am picking the new value in the JSP using EL expression.
But the problem is, the new value has to be used in the javascript function. I have to pass this new value some how to a predefined javascript function.

Is there any way around except AJAX?

Thanks
 
Eric Pascarello
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ajax or post back the page and re render the whole document with that piece.

Eric
 
Matt Thomassan
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@ Eric.

Sorry for posting the question in multiple forums.

Can you give some example on post back?

Thank you for your guidance.

Thanks
 
Eric Pascarello
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is like any classic web programming where you put the information on the page that you need.

Are you afraid to go the Ajax route? It is really simple to implement if you pick up a library.

Eric
 
James Ward
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I have to pass this new value some how to a predefined javascript function.



Not sure if i understood your query properly but,
You can take value in JSP, and put it against a JavaScript variable.

Pseudo-code:
 
Matt Thomassan
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@ James

Yes, we can use scriptlets or el expressions to retrieve values from a model.

Ok assume that, in your example, we are getting the string "whatever" from a java service after we submit the form.
I then have to pass this string "whatever" to a js function.
Assume myFunction1() is js function called after onchange event of a drop down.

Eg:
function myFunction1(){
this.formName.submit() // this will submit the form, a service gets called, service puts the string "whatever" into a model, and then we use EL to retrieve it eg: var result=${model.returnValue}
var result=${model.returnValue}
loadFile(result) //loadFile is another js function, ie. predefined in the api and its argument is the latest string retrieved from the service. Now the problem is after saying this.formName.submit(), the control never returns to the myFunction1()
}

Hope I have made the problem clear. I know we could do this using AJAX but we do not want to use AJAX.

Please let me know if you think there is another way of solving this problem.

Thanks for taking the effort to solve it.

Any suggestions are welcome.

Thanks
 
Sheriff
Posts: 67634
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Matt, please use code tags when posting code.
 
Eric Pascarello
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A post back causes the page to reload. It is impossible for it to do what you want it to do.

The postback is like clearing a whiteboard. You need to start all over from scratch. So on the onload event of the page that loads after the postback, you continue with the code.

Eric
 
Bear Bibeault
Sheriff
Posts: 67634
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or use Ajax to avoid the whiteboarding.
 
The human mind is a dangerous plaything. This tiny ad is pretty safe:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic