# Submitting a form conditionally

**URL:** <https://www.ruby-forum.com/t/submitting-a-form-conditionally/77003>\
**Category:** Rails\
**Created:** [November 23, 2006, 10:24am UTC](https://www.ruby-forum.com/t/submitting-a-form-conditionally/77003 "2006-11-23T10:24:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dec6dc/32.png) [@sma](https://www.ruby-forum.com/u/sma)\
**Post date:** [November 23, 2006, 10:24am UTC](https://www.ruby-forum.com/t/submitting-a-form-conditionally/77003/1 "2006-11-23T10:24:08Z")

</div>

I am looking for some implementation ideas.

A view has a dropdown. After selecting a value in the dropdown and  
clicking a submit button, the form gets submitted and calls a method in  
the view’s controller. This method would display a table of data in the  
same view depending upon the selection made in the drop down. This  
works fine.

There is another case where in the dropdown in the view can have a value  
preselected by using some cookies set on the browser. So now, even  
without clicking the submit button, the above mentioned method in the  
controller should be called and a table of data should be displayed  
according to the default value selected in the dropdown. How can this be  
achieved?

---

<div class="post-metadata">

**Author:** ![sma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dec6dc/32.png) [@sma](https://www.ruby-forum.com/u/sma)\
**Post date:** [November 23, 2006, 11:09am UTC](https://www.ruby-forum.com/t/submitting-a-form-conditionally/77003/2 "2006-11-23T11:09:23Z")

</div>

> without clicking the submit button, the above mentioned method in the  
> controller should be called and a table of data should be displayed  
> according to the default value selected in the dropdown. How can this be  
> achieved?

Shr,

Given you are looking for idea, here is a few:

- Have your controller check for the cookie, and run the required code  
just as if the page had been submitted and return the desired result

- If it is an AJAX submit, then have an onload event which checks for  
the cookie and does the submission via javascript once the page is  
loaded

- Least preferred (I can’t think of a scenario where I would actually  
recommend this, but just in case): Have javascript check the cookie on  
load, change the value of the select, and then submit the form.

Hope that has helped

---

<div class="post-metadata">

**Author:** ![sma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dec6dc/32.png) [@sma](https://www.ruby-forum.com/u/sma)\
**Post date:** [November 23, 2006, 12:22pm UTC](https://www.ruby-forum.com/t/submitting-a-form-conditionally/77003/3 "2006-11-23T12:22:30Z")

</div>

> - Least preferred (I can’t think of a scenario where I would actually  
> recommend this, but just in case): Have javascript check the cookie on  
> load, change the value of the select, and then submit the form.

Thanks Glenn.  
But the question is, how can the cookie value be accessed inside the  
javascript function?
