using validate with serialize error

Discussion in 'jQuery' started by ploppy, Jun 28, 2013.

  1. #1
    I am looking at using jquery validation script and although the implementation seems quite simple, I am stuck at getting this to work using the serialize function. I know from readings docs and googling that you have use .valid event, but I am not sure how I would use this with my code. Could somebody show me the correct way to use this. Thanks

    edit: I have included the validate code and also the .valid, but still it isn't working. Instead of processing the php the page just refreshes when i click on add box.

    jquery code

     // function to connect to php and process form values
     
        $('BA_boxform').validate();
     
        $(function(){       
                $('#BA_boxform input:submit').on('click', function () {
                if($('BA_boxform').valid()) {
                var formdata = $('#BA_boxform').serialize() + '&submit=' + $(this).val();
     
                //alert(formdata);
                $.ajax({
                  type: "POST",
                  url: "/domain/admin/requests/boxes/boxesadd.php",
                  data: formdata,
                  dataType: 'json',
                  success: function(msg){
                      if(typeof msg.boxerrortext !== "undefined" && msg.boxerrortext == "You need to input a box")
                          {
                            $("#BA_addbox").html(msg.boxerrortext);
                          }
                      else
                          {
                            $("#BA_addbox").html("You have successfully added box " + msg.box + " to the archive.");
                          }
                      //$("#confirm_department").hide();
     
                      /*
                      var $dialog = $('<div id="dialog"></div>')
                      .html('Your intake was successfully submitted and will be viewable in the reporting area.<br /><br />Thank you.');
                      $dialog.dialog({
                      autoOpen: true,
                      modal: true,
                      title: 'Box intake submission successfull',
                      width: 400,
                      height: 200,
                      draggable: false,
                      resizable: false,
                      buttons: {
                      Close: function() {
                      $( this ).dialog( "close" );
                      }
                      }
                      });
                      */
                      //alert(msg);
                      //console.log(msg);
                      //$("#BA_addbox").html(msg.box);
     
                      //$("#formImage .col_1 li").show();
                      //$("#BA_boxform").get(0).reset();
                      //$("#boxaddform").hide();
                  }
            });
          }
            return false;
        });
    });
     
        // end php processing
        // End function to submit box intake form
    Code (markup):

     
    ploppy, Jun 28, 2013 IP
  2. HuggyStudios

    HuggyStudios Well-Known Member

    Messages:
    724
    Likes Received:
    20
    Best Answers:
    26
    Trophy Points:
    165
    #2
    Can you post your markup (the html code) so I can test it and see what's happening.

    Also, you have commented out a load of code which would cause an error and stop it working.

    Glen
     
    HuggyStudios, Jun 28, 2013 IP
  3. ekim941

    ekim941 Member

    Messages:
    74
    Likes Received:
    7
    Best Answers:
    7
    Trophy Points:
    33
    #3
    For starters, you forgot the hash sign a few times, the first time is in your very first line of code:
    $('BA_boxform').validate(); should be $('#BA_boxform').validate();
    Same thing on line 7.

    Proofreading code requires going over ever character. Depending on the editor you're using, you can often highlight the name of an ID or variable and every occurrence of it will be highlighted (if it is an exact match).
     
    ekim941, Jul 3, 2013 IP