Forum: Ruby on Rails submit_tag and input type="button"

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Michael W. (Guest)
on 2008-10-08 12:51
Hi there,

It's 1.47 am and I am kinda tired of searching for answers now. I hope
someone would respond.

I have a javascript to validate date and display error message. I also
have an ajax form that returns the value of the form provided the dates
are in the correct format.

I notice if I use submit_tag onclick, the form displays the error
message AND submits the form.  On the other hand, if I use input
type="button," the form displays the error properly first without
submitting the form, but when it submits the form, it's no longer ajax
anymore (the result is displayed on a separate refreshed page).

My question: how to use submit_tag and prevents from submission if js
validation returns false?



<% form_remote_tag :update => 'practiceResult',
                                                       :loading  =>
"$('indicator1').show()",
                                                       :complete =>
"$('indicator1').hide()",
                                                       :success =>
"set_blank('practice')",
                                                       :url =>
{:controller => 'profiles', :action => 'add_practice'} do %>
                          <div id='practiceForm'>
                            <%= render :partial =>
'profiles/practice_form', :objects => @profiles_practice %>
                            <%= hidden_field_tag 'practice_type',
'practice' %>
                            <div id="btn_practice" align="center">
                              <input type="button" name="commit"
value="Add practice" class="btnSubmit" onClick="validate_date(this.form,
this.form.practice_start_date.value,
this.form.practice_end_date.value);"
/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</div>
                              <%= submit_tag "Add (ruby)",
:onclick=>"validate_date(this.form, this.form.practice_start_date.value,
this.form.practice_end_date.value);" %>
                          </div>
                        <% end %>

Thanks.

Milton
This topic is locked and can not be replied to.