Arkady Rodestve
Arkady Rodestve

Reputation: 147

help with ajax/jquery form (submit data)

I tried to do some form (just submit data) . Application I'm write in rails, but i think it doesnt matter.

i have this js code:

    function closeForm(){
            var answer_text = $("#answer_text").val();

                if (answer_text != ''){
                    $(".messageSent").show("slow");
                    setTimeout('$(".messageSent").hide();$(".slide-out-div").slideUp("slow")', 1000);

            }
            else {
                alert('All fielsds are required!');
            }
        }

my form in rails

<span class="messageSent">Спасибо за ответ! Нам важно ваше мнение!</span>
<% form_for :home, :url => { :action => "create" }  do |f| %>
                <p><%= text_area(:answer, :text, :cols => 50, :rows => 10) %></p>


                    <p><%= submit_tag "Answer!", :class => "submit", :onclick => "closeForm()" %></p> 

                 <% end %> 

Its okei, when I press submit and left textarea empty alert comes (that want I need), but when i press "OK" button, then page is realoading, but i dont want!! I want to say on page if i left some fields empty.

How i can figure this?

Please help , i 'm stuck with this about 4 hours..

UPDA: unfortunately it doesn't help :( i think there a problem when i click on submit (form action /home/

create) :S? mby some conflicts with this function closeForm()?

UPD2: QUESTIONS IS CLOSED! all is done!

Upvotes: 1

Views: 371

Answers (3)

patel.milanb
patel.milanb

Reputation: 5992

if you dont want to reload the page then you can disable the submit button event using

        $(':submit').click(function(e)
{
            e.preventDefault();
}

this will prevent the form to be submit on server and you can check all the validation ..

Upvotes: 0

dnuttle
dnuttle

Reputation: 3830

I presume this function is called when the form is submitted. Change it to return false when you don't want the form to submit.

Upvotes: 0

Paul
Paul

Reputation: 5576

in your else block try:

 alert('All fielsds are required!');
 return false;

Upvotes: 1

Related Questions