# Get value of select whch change using ajax after form submit

**URL:** <https://www.ruby-forum.com/t/get-value-of-select-whch-change-using-ajax-after-form-submit/176994>\
**Category:** Rails\
**Created:** [October 9, 2009, 2:27pm UTC](https://www.ruby-forum.com/t/get-value-of-select-whch-change-using-ajax-after-form-submit/176994 "2009-10-09T14:27:27Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![salil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c89c15/32.png) [@salil](https://www.ruby-forum.com/u/salil)\
**Post date:** [October 9, 2009, 2:27pm UTC](https://www.ruby-forum.com/t/get-value-of-select-whch-change-using-ajax-after-form-submit/176994/1 "2009-10-09T14:27:27Z")

</div>

Hi All,  
I have a problem while updating select list using ajax and then submit  
it.  
what i have done is to change the drop down of a city when i changed  
the state.but when i am submitting the form i am not able to get the  
value of city.  
Following is my code for the same.

edit.html.erb  
\<% form\_for :user\_info , :url=\>{:controller=\>“users”,  
:action=\>“update\_contact\_details”,:id=\>params[:id] } do |f| %\>

```
 <% @state= UsaCity.find_by_sql('select DISTINCT state from

```

usa\_cities’) %\>

```
       <td >State</td>

       <td> <%= select_tag 'user_info[state]',

```

options\_from\_collection\_for\_select(@state, :state, :state,  
@user\_info.state) %\>

 ![](https://test.ruby-forum.com/images/loading.gif)
  
  
  
\<%= observe\_field :user\_info\_state, :url  
=\>{:controller=\>“logins”,:action =\>‘select\_city’, :id=\>‘1’}, :update=\>  
‘city’ , :with =\> “’  
state=’+document.getElementById(‘user\_info\_state’).value+’&type=contact\_details’”,  
:on=\>‘click’ %\> 

```
    </tr>
   <tr id="city">

    <td >City</td>

   <td><%= collection_select :user_info, :city, @cities ||

```

Array.new, :city, :city, :prompt=\>true %\>

```
    </tr>

```

\<% end %\>

controller method  
def edit\_city  
@cities = UsaCity.find(:all, :conditions=\>[“state=?”,  
params[:state]])  
@type = params[:type]  
render :update do |page|  
page.replace\_html ‘city’, :partial =\> ‘logins/select\_city’,  
:object=\>[@cities, @type], :layout =\> false  
end  
end

\_select\_city.html.erb

City1345 \<%= collection\_select :user\_info, :city, @cities || Array.new, :city, :city, :prompt=\>true %\>  

Regards,  
Salil G.

---

<div class="post-metadata">

**Author:** ![salil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c89c15/32.png) [@salil](https://www.ruby-forum.com/u/salil)\
**Post date:** [October 9, 2009, 3:06pm UTC](https://www.ruby-forum.com/t/get-value-of-select-whch-change-using-ajax-after-form-submit/176994/2 "2009-10-09T15:06:38Z")

</div>

Hi All,

Sorry for this but it’s not working properly because i use form\_for tag  
inside the

 tag.  
i only remove form from inside the table and keep outside the table tag  
and now everything is works like a magic. I hope this will help someone  
to save his/her 1 hr or 2.

Regards,

Salil G.
