Richlewis
Richlewis

Reputation: 15384

Use jquery to check which form is completed and then submit using one button

I have two forms that make a separate requests to different apis. What i am trying to achieve is to maybe have jquery check which form has been completed and then submit the relevant form. Each request goes through its specific controller. the forms are like this

<div class="container margin50">
<div class="row">
<div class="span6 offset3 cf formBackground">
<h1>CoverArt Finder</h1>

<h3>Search Movies</h3>
<%= form_tag main_results_path, :method => "get" %>
<%= text_field_tag 'search', nil, :placeholder => 'Enter Film Name Here.....' %>

<h1>OR<h1>

<h3>Search Albums</h3>
<%= form_tag album_album_results_path, :method => "get" %>
<%= text_field_tag 'search', nil, :placeholder => 'Enter Artist Name here.....' %>
<%= submit_tag "search" %>

I’m relatively new to Jquery but have been presented with this as a possible solution

on submitButton click:

if formA.someValue != null
post / submit formA
else if formB.someValue != null
post / submit formB

Only issue is im not sure how to convert this into Jquery and how to get it to work with a rails form..

Can anyone offer some advice or a solution

Thanks

Upvotes: 0

Views: 177

Answers (1)

Rafay
Rafay

Reputation: 31043

you can use the jquery validate plugin

$(document).ready(function(){  
    $("form").validate();

    $("form").submit(function(e){
    e.preventDefault();
        if($(this).valid()){
            alert("form is valid");
        }        
        else{
                    alert("form is invalid");
        }
    });
});

here is the demo http://jsfiddle.net/BcRfj/

Upvotes: 1

Related Questions