views:

18

answers:

1

Somehow, a simple form I have to sign up for a mailing list (which adds an email to a Google Apps Group, using gdatav2rubyclientlib) seems to fail occasionally. The form is submitted via POST through jQuery, and the Sinatra app (running on nginx/Passenger) handles the POST and emails me a confirmation. Occasionally, I get emails that "undefined" signed up. Somehow, the value the user types in is getting lost, but I'm not sure how or why. . .

Here's the HAML form from which the POST originates:

%li.sidebar-section
  %span#mailing-list-slug
    get on our mailing list
    <br/>
  %form{:id => "mailing_list_signup", :method => "post", :action => "/mailinglist/signup/the.grapevine"}
    %input{:type => 'text', :name => 'email', :value => "enter your email&hellip;", :id => 'mailing-list-signup'}
    %input{:id => "mailing_list_signup_button", :type => 'submit', :value => 'sign up'}
    #thanks
      thanks!  you're in!

Here's the Javascript that makes the POST: $('#mailing_list_signup_button').click(function () { var email_address = $('input#mailing-list-signup').val();

  $.ajax({  
    type: "POST",  
    url: "/mailinglist/signup/the.grapevine",
    data: "email=" + email_address,
    success: function() {
      $('#thanks').show('slow');
    }
  });

  var email_address = $('input#email').val();

  $.post("/mailinglist/signup/the.grapevine", 
    $('form#mailing_list_signup').serialize(), function(result){alert(email_address);});
    return false; });
  });

Here's my route handler for the POST request

post '/mailinglist/signup/:list' do
  adminuser = "ADMINUSERNAME"
  password  = "PASSWORD"
  email = params[:email]
  mailing_list = params[:list]
  myapps = ProvisioningApi.new(adminuser,password)
  myapps.add_member_to_group(email, mailing_list)
  gmail(:to => 'MYEMAIL', :subject => "HEY HEY GUESS WHAT, #{params[:email]} JUST SIGNED UP FOR #{params[:list]}!", :body => "Totally not joking--")
end

Any thoughts would be appreciated!

A: 

I would add validation to the email address field on the server side at least, but you can do initial verification on the client side as well (using a regex or jQuery Validation).

If the email field does not contain a valid email address, do not submit the request to add the member.

calvinf
Yeah, that makes sense. But I'm still confused about why I'm getting emailed the value "undefined" No matter what they put in, I should just be getting the value they enter, right?
aresnick
If a JavaScript variable is not defined it will return "undefined". http://www.w3schools.com/jsref/jsref_undefined.asp
calvinf