aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes Save button with Ajax Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Save button with Ajax" Watch "Save button with Ajax" New topic
Author

Save button with Ajax

Jayasri Alaparthi
Ranch Hand

Joined: Aug 14, 2006
Posts: 67
Hi Friends,
I am using struts MVC framework, where the data is taken from dao in action and kept in form for save process but the page is refreshing but, I want the save button to be an ajax call can some one guide me as how to proceed.


with regards,
Bhargavi.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61761
    
  67

Do you already know Ajax? Your post is too vague to give any concrete advice.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Jayasri Alaparthi
Ranch Hand

Joined: Aug 14, 2006
Posts: 67
Hi ,
I know basics of Ajax, so can you guide me as how to write save button.

with regards.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61761
    
  67

You haven't provided many details. What type of button? If it's a submit button, you can hijack the form submission. If it's not, then an event handler for click can be established on the button.
Jayasri Alaparthi
Ranch Hand

Joined: Aug 14, 2006
Posts: 67
The save button which I need to write ajax functionality has to submit the form. Can you give any url or sample for this.
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
First hit from a Google search for "submit form ajax": http://www.captain.at/howto-ajax-form-post-request.php

- Brent
Jayasri Alaparthi
Ranch Hand

Joined: Aug 14, 2006
Posts: 67
Thanks Brent for giving me idea as how to proceed.

with regards,
Mehul Wani
Greenhorn

Joined: Mar 18, 2008
Posts: 18
Hi Bhargavi,

Submitting a form using AJAX is easy.

Only thing is you will need to read all the form field values manually(since you are using AJAX and not submitting the form) using
document.getElementById(fieldName).value and appending name and value for each field to the AJAX url.

Note: document.getElementById(fieldName).value doesn't work in Firefox
instead you can use document.forms[0].fieldName

This all can be using a javascript function and calling that function on the onclick event of button.
Hope this helps.
[ March 19, 2008: Message edited by: Bear Bibeault ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61761
    
  67

document.getElementById(fieldName).value doesn't work in Firefox
Of course it does. As long as you correctly pass the element id to the function.
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
Note: document.getElementById(fieldName).value doesn't work in Firefox

You are probably confused by what is basically a bug in IE. If IE does not find an element with the given id it will look for an element with a name that matches the given id. Other browsers implement the getElementById method correctly.

- Brent
Mehul Wani
Greenhorn

Joined: Mar 18, 2008
Posts: 18
Brent,

I tried it on Firefox v1.5. I have javascript code having lot of document.getElementById(). Now, when I open an url in Firefox and parallely open the javascript console, I see a lot of errors and warnings on the console.

If instead of document.getElementById() one uses document.forms[0].fieldName
works fine with firefox.

Thanks for the reply and correct me if I'm wrong.
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
...or you could add id attributes to your elements.

- Brent
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Save button with Ajax