tags:

views:

113

answers:

2

I'll start off by saying I'm new to jQuery but I am really enjoying it. I'm also new to stackoverflow and really loving it!!

The problem: I've created a sub-form with jQuery so that a user may add, then select this information from a dropdown list if it is not already available. I'm unable to POST this data with .ajax(), so that the user can continue to fill out other information on the main form without having to start over.

Sub-Form:

   $(function() {
      $("#add").live('click', function(event) {
      $(this).addClass("selected").parent().append('<div class="messagepop"><p id="close"><img src="img/close.png"></p></img><form id="addgroup" method="POST" action="add_group.php"><p><label for="group">New Group Description:</label><input type="text" size="30" name="grouping" id="grouping" /></p><p><label for="asset_type">Asset Type:</label><select name="asset" id="asset" ><option>Building</option><option>Equipment</option></select></p><p><input type="submit" value="Add Group" name="group_submit" class="group_submit"/></form><div id="result"></div></div>');
      $(".messagepop").show()
      $("#group").focus();
      return false;
  });

  $("#close").live('click', function() {
   $(".messagepop").hide();
   $("#add").removeClass("selected");
   return false;
  });
 });

And here is where I'm attempting to process it:

$(function () {
$('#addgroup').submit(function() {
  $.ajax({
   type: $(this).attr('method'),
   url: $(this).attr('action'),
   data: $(this).serialize(),
   success: function(responseText) {
    $('#result').html(responseText);
    }
   });
    return false;
 });
});

I've even attempted to create a simple alert instead of processing the information and this also does not work. Instead the form sumbits and refreshes the page as normal. Can anyone help me understand what I am missing or doing wrong? Thank you!

New attempt:

$("#add").live('click', function(event) {
   var form = $("<form>").html("<input type='submit' value='Submit'/>").submit(function(){
   $.post("add_group.php", {grouping: "Building, asset: "STUFF"});
   $(".newgroup").append(form);
   return false;
});

Final code

$(function() {
    var id = 1
    $("#add").live('click', function(event){
        if($(".addgroup,").length == 0){
            $("#test").append('<div class="addgroup"><label for="newGroup">New Group:</label><input type="text" class="newgroup" id="' + ++id + '" /><input type="submit" value="Add Group" name="group_submit" class="group_submit"/></div>');
            $("#add").attr("src","img/add_close.png");
        }else{
            $("#add").attr("src","img/add.png");
            $(".addgroup").remove();}
        return false;
    });

});

$(function(){
    $(".group_submit").live('click',function(event){
        $.ajax({
            type: "POST",
            url: "add_group.php",
            data: {new_group: $(".newgroup").val(), asset: $("#group option:selected").text()},
            success: function(){}
        });
        $(".addgroup").remove();
        $('#subgroup').load('group.php', {'asset': $("#group option:selected").text()});
        return false;
    });
});
A: 

If the form is submitting and refreshing as normal, the jquery isn't kicking in (the refresh means it's posting the form normally).

I for some reason (maybe others haven't) found that $(document).ready(function() { works much better than $(function() { ...

Also, the groups that you're adding should have a definitive id: on #add click, count up a counter (form++) and add that to the id (#addGroup_+form) and then target that straight away in the function that added it:

 $("#group").focus();
 $("#addGroup_"+form).submit(function() {
jamie-wilson
The problem I found in the OP is that the `form` was created dynamically... normal binding of `submit` won't work...
Reigel
var form = $("<form>").html("<input type='submit' value='Submit' />").submit(function() { alert("SUBMIT"); })); $("div").append(form);By creating a form element, as apposed to just dumping form code onto the page, it seems to work
jamie-wilson
@jamie: in that case, you are also adding `submit` handler dynamically... ;)
Reigel
@jamie: play around it here, http://jsfiddle.net/ZQyCR/ :)
Reigel
A: 

try using .live()

$(function () {
 $('#addgroup').live('submit',function() {
  $.ajax({
   type: $(this).attr('method'),
   url: $(this).attr('action'),
   data: $(this).serialize(),
   success: function(responseText) {
    $('#result').html(responseText);
    }
   });
    return false;
 });
});

and make sure addgroup has no duplicate id... you may use it as class if it has to be duplicated...

Reigel