This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes HTML, CSS and JavaScript and the fly likes How to Submit data in Ajax Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "How to Submit data in Ajax" Watch "How to Submit data in Ajax" New topic
Author

How to Submit data in Ajax

Rajendra Prakash
Ranch Hand

Joined: Sep 10, 2009
Posts: 293
Hi Friends , am newbie to ajax. For example just consider one account registration -
A form contains 8 text fields with submit button. In this form second texbox contains "username " . On right side of text box there is a label box . On clilck action i need to determine user is available or not. Is it possible on clicking label or should i click
submit button.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

So all that you need to know is whether there's a value in the username field or not when a checkbox is clicked?? If yes then this has nothing to do with struts and I'll move it to the HTML and javascript forum where you might get a better reply.

If I'm understanding it right, then its not very tough to do. On the onclick event of the checkbox, you need to check the value property of the textbox using its ID. Like if the username textbox has the ID user_name, then the onclick even will just check


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Rajendra Prakash
Ranch Hand

Joined: Sep 10, 2009
Posts: 293
I mean onclicking checkbox(action) can i send request to server or should i click submit button to send request to server.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

I still can't understand what you want to do. You want to send an Ajax request, now whether you send it on click of a checkbox or a button depends upon what you want to do. submit button by default posts back the page instead of making an Ajax call. What exactly do you want to do is not clear...
Rajendra Prakash
Ranch Hand

Joined: Sep 10, 2009
Posts: 293
can i send a request to server on clicking check box. is it possible
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

can i send a request to server on clicking check box. is it possible

Yes it is possible using the onclick event of the checkbox...
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60765
    
  65

Please take the time to choose the correct forum for your posts. This forum is for questions on Servlets. For more information, please read this.

This post has been moved to a more appropriate forum.

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Anbarasu Aladiyan
Ranch Hand

Joined: Jun 02, 2009
Posts: 182

Hi,

1. On click event of labels call a java script function
2. In that java script function, create an XMLHttpRequest object
3. Create a post string that needs to send to servlet calss. In your case "user name"
4. By using XMLHttpRequest object's 'open','setRequestHeader','send' methods send the request to the servlet
5. Do your business logic in servlet and send the output to java script by write/print/JSON techniques.(in your case 'true/false' or exists/available)
6. In java script check for readyState(ie readyState ==4) then use XMLHttpRequest object.responseText to get the result that sent from servlet.

Regards,
Anbarasu


A.A.Anbarasu
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How to Submit data in Ajax
 
Similar Threads
How to use struts2 submit tag as button without submitting the form ?
JSF 2.0 - replacing JS Alert Box with what?
doubt in update
Three forms in jsp page
Form submission using hyperlinks