green_arrow
green_arrow

Reputation: 1277

Ajax determine the id of a form

I have multiple forms on one page and I am using ajax in my CodeIgniter app to determine which forms get displayed after each form submit. Anyways I have labelled each form with an id so I can check which form is submitted and I am using ajax to rename the form however I need a default form name and I'm not sure how I do this?

$.ajax({                  
   url:$(this).attr('action'),
   type:'POST',
   // this is where I have added a function to put in a default response                
   data:function(respond) {
      if(respond.result == 'false') {
         var form = "form#part-one";
      } else {
         var form = respond.form;
      }
      $(form).serialize(),
   }
   dataType:'json',
   success:function(respond) {
      if(respond.result == 'false') {
         $('#error_messages').html(respond.errors);                 
      } else {
         $('#error_messages').html('');
      }
   }
});

Upvotes: 1

Views: 64

Answers (1)

Thom
Thom

Reputation: 263

I am not exactly clear on what you are trying to do, but if you need to pass a variable from the form I suggest this.

$('form').submit(function(e) {
   e.preventDefault();
   var formId = $(this).attr('data-id');
   // ajax goes here
});

<form name="common_name" data-id="2">
<input type="submit" value="Go">
</form>

On any variables you add to HTML tags I suggest you always use "data-" in them so they are HTML compliant.

Hope this helps.

Upvotes: 1

Related Questions