Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doing a POST in jsp without leaving the page and displaying a success text

 
Vivek K Jain
Greenhorn
Posts: 19
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok i have found a thread on coderanch corresponding my query but this is an extension to the solution provided there.

Firstly i want to know what are the possible ways of doing it(form submission without leaving the page..).

Secondly i have tried using AJAX(as replied by Bear sir there) for doing that , & i did get success in inserting value in database but i want to store some more information to same database by using the arguments provided in the url parameter.how could i pass more arguments to the url parameter of open() of the XmlHttpRequestObject ?
here what i did : i tried two syntax
1.

2.

tell me which one is correct because using the no.2 syntax i am getting only the first argument (content) value to my PostResAjax servlet (which is controlling database insertion), other field in the db are written as "undefined".Do i need to also encode uri components?(i didn't for the content field though it had spaces)?? if so how?
Also, here content,cname,userid all are ids defined in my jsp, how can i pass arguments to the ajax calling function(the onclick func for the post button) with EL syntax like ${userid} etc.

please correct me if there's something conceptual wrong like inserting in database using ajax or something like that.
i just want to simulate an "add comment" type of feature as provided in most blogspots.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65229
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This has nothing at all to do with JSP, so it's been moved to a more appropriate forum.

Building up URL to Ajax "by hand" is not recommended. The values need to be encoded, and there are other nuances.

It is highly recommended to use jQuery to make Ajax calls. It will take care of all those problems and vastly simplify the code.
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no difference between 1 and 2!, You just changed string concatenation. The name of the serverside resource implies you want a Post request, but you are sending a get. You are not encoding the url valies
 
Vivek K Jain
Greenhorn
Posts: 19
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

id=writeRes is my button which make comment field visible i.e. the textarea id=cresponse nd rest is i hope clear...
this is what i did sir using your suggestion of jquery...everything is working fine upto $.ajax() function it is simply i think not invoking my servlet..(PostResAjax)...why?
do i have to have some returning code in servlet which returns back me to the jsp page again.
also is the encoding part which both of you above mentioned is being handled by jquery?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic