Clearing a form

Hi all,

I am trying to clear a form once the info is being submitted to the
database. Basically what I have is a form that makes use of the
form_remote_tag which adds a list of credit cards to a list and then
adds that info to a database. What I want to happen is that once the
submit button is hit the form clears so that the user can enter another
credit card if they so choose. what I have tried is adding an :onclick
to my submit_tag helper:

<%= submit_tag "Add Credit Card Information, :onclick =>
‘clear_credit_card_form()’ %>

which called a JavaScript method that bascially went like:

document.getElementById( “exp_date” ).value = " ";

to just clear the form, which it did. However, it cleared the form
before the POST so nothing got through to the database and an exception
was thrown. Anyone have any hints and how I could handle this. Thanks,

~S

Shandy N. wrote:

Hi all,

I am trying to clear a form once the info is being submitted to the
database. Basically what I have is a form that makes use of the
form_remote_tag which adds a list of credit cards to a list and then
adds that info to a database. What I want to happen is that once the
submit button is hit the form clears so that the user can enter another
credit card if they so choose. what I have tried is adding an :onclick
to my submit_tag helper:

<%= submit_tag "Add Credit Card Information, :onclick =>
‘clear_credit_card_form()’ %>

which called a JavaScript method that bascially went like:

document.getElementById( “exp_date” ).value = " ";

to just clear the form, which it did. However, it cleared the form
before the POST so nothing got through to the database and an exception
was thrown. Anyone have any hints and how I could handle this. Thanks,

~S

Hello

Are you using rjs? If so, i think you can use
page[‘form_id’].reset
in your create.rjs

I’m a noobie at this, so this might seem like a dumb question, but how
do you add an id to the form tag? What I have is:

<%= form_remote_tag :update => “credit_card_table”, :url => { :action =>
:add_credit_card, :id => @user } %>

if I do something like:

<%= form_remote_tag :update => “credit_card_table”, :id =>
“credit_card_form”, :url => { :action => :add_credit_card, :id => @user
} %>

the raw html doesn’t show an id attirbute and thus my .rjs file does
nothing. Another thing is what do I call the .rjs file? If the file that
has the form is edit_form.rhtml should I name my rjs file edit_form.rjs?
I’m just a little confused on how the code in the .rjs file get excuted.
Thanks,

~S

Im not sure what’s going on. I have a web apps book for Ruby on Rails
which describes the same things that you have been suggesting as far as
the .rjs file goes but it is not working. I have matched the method name
with the .rjs file and have given all the html elements the proper id’s,
but it’s still not working. I don’t think it is excuting the .rjs file,
but I don’t see why. I’ll keep messing with. Thanks for your help. It’s
all about learning new things and I have learned a lot even if it’s not
cooperating.

~S

Shandy N. wrote:

I’m a noobie at this, so this might seem like a dumb question, but how
do you add an id to the form tag? What I have is:

<%= form_remote_tag :update => “credit_card_table”, :url => { :action =>
:add_credit_card, :id => @user } %>

if I do something like:

<%= form_remote_tag :update => “credit_card_table”, :id =>
“credit_card_form”, :url => { :action => :add_credit_card, :id => @user
} %>

the raw html doesn’t show an id attirbute and thus my .rjs file does
nothing. Another thing is what do I call the .rjs file? If the file that
has the form is edit_form.rhtml should I name my rjs file edit_form.rjs?
I’m just a little confused on how the code in the .rjs file get excuted.
Thanks,

~S

Try

<%= form_remote_tag
:update => “credit_card_table”,
:url => { :action => :add_credit_card, :id => @user},
:html => { :id = ‘form_id’ }%>

By the way check out form_remote_for, i think this helper fits better
your needs.(http://api.rubyonrails.org/)

You should name your rjs file after the method you are trying to execute
in your controller. So create method will have create.rjs.

SHAZAM - I figured it out - well not really, I just figured out a new
way to do it. So here it is; in my form_remote_tag I added:

:complete=>'document.getElementById(“exp_date”).value = “”;

The problem taht I had before was that I was using this exact same
JavaScript to clear the form, but it cleared it before it posted the
info, making is usless, plus it crashed on top of that. I imagine what
:complete does is post first then execute the JavaScript. But that’s how
I got it to work, now I’m going on break!

~Shandy