Loading popup with ajax

I have a jsp page that needs to load a popup using ajax. The content of the page is determined by the form filled in by the user.

Something like that:

JavaScript:

ajax('getPage.action', 'content_id', 'form_id');

      

foo.jsp:

<div id="content_id"></div>

<form id="form_id">
 ...
</form>

      

Java / spring:

@RequestMapping("getPage.action")
MyController extends AbstractCommandController {
  RealDto dto = (RealDto)command;
  ...
  return new ModelAndView("foo", data);
}

      

The hardest part for me is how easy it is to submit the form data as an ajax call. Can I use jQuery here? The form changes dynamically, so it would be enough to bother the list of all form fields.

Would it help to use Springs XT (which I never had)?

+1


a source to share


3 answers


Yes, you can use serialize to trivially transform the form to submit data.

$("#form1").submit(function() {
    $.get("/desiredURL", $("#form1").serialize(), function(response) {
        // send response data to a popup
    }
}

      



You can use get or post to send data.

For the popup, I like facebox , but there are tons of options.

+2


a source


jQuery form plugin helps you to easily convert a regular form to Ajax. You only need one line of code:



$("#myform").ajaxForm(
   {beforeSubmit: validate, success: showPopup} );

      

+2


a source


I don't know about jQuery, but for a prototype it's easy:

new Ajax.Request('getPage.action', {
    parameters: $('form_id').serialize(true),
    onSuccess: someMethod
);

      

Check out the Prototype API Docs .

This page has the same information for jQuery: http://docs.jquery.com/Ajax

0


a source







All Articles