andy mccullough
andy mccullough

Reputation: 9601

Submitting fields from multiple forms to one Grails controller

I am implementing search functionality in my grails app, I currently have a small number of search fields in one form, which submits to a search function in a grails controller, and renders the results in search.gsp I now have a second section in my page, that will be another collection of fields that will now also be used in the search function, though these fields are not contained within the first form, and can't be due to the positioning on the page, therefore submitting the form simply isnt enough.

I have tried manually calling the search function in the grails controller using ajax after collecting up all the fields, but this is only returning an html response and not actually navigating the user to search.gsp

What is the best way to handle this, as I feel ajax isnt the best way due to being asynchronous?

edit...

What I have is something like -

<g:form name="form1" method="POST" url="[action:'search', controller:'someController]">
...some fields
</g:form>

<g:form name="form2" method="POST" url="[action:'search', controller:'someController]">
...some more fields
</g:form>

<div class="button">Click me</div>

When I click the button, I want to do some processing on the items in order to build up a tidy payload, structured in a way that the search service can easily consume the data, and finally send the the data to the search function in the controller, to finally render search.gsp with the results.

Upvotes: 0

Views: 857

Answers (1)

Kiesel
Kiesel

Reputation: 33

A) You could just ignore the forms and get the data from the input fields directly and send it with an ajax request like:

$(function(){

   $('.button').click(function(){

     var field1 = $('#inputfield_1_of_form_1').val();
     var field2 = $('#inputfield_1_of_form_2').val();

     //send fields to server
     $.post("path", { field1: field1, field2:field2 }, function(data){
       //do something with the return data from the server
     }    

   });

})

You can then redirect the user (and send the search result as attribute) with

window.open('url to open','window name','attribute1,attribute2') 

B) Or you could first redirect the user and give the form fields as attributes to the search.gsp page and there handle the search.

C) Or you could just add hidden fields in the form tied to the submit button and write whatever is written into the fields in your first form directly in the hidden fields of the second form with the .change() method of jquery.

Upvotes: 1

Related Questions