简体   繁体   中英

Jquery Form on submit show success message

I have a form that uses Jquery to show a message for

*field required error message

I am trying to get it to show a success message if the form is submitted.

The form submits as long as the req fields are filled in.

Does anyone know how I can modify this code to show the "success" div if all the "req" fields are filled out?

Thanks

    $(function() {
       function validateform() {
          var valid = true;
          $(".req").css("border","1px solid #ccc");
          $(".req").each(function() {
             if ( $(this).val() == "" || $(this).val().replace(/\s/g, '').length == 0 ) {
                $(this).css("border","1px solided");$(".required").css("display","block");
                valid = false;
             }
          });
          return valid;
       }    
       $("#submit").click( function() {
          $('#myform').submit( validateform );
          $('$name').submit();
       });  
   });
           submitHandler: function(form){
                  $(form).ajaxSubmit({
    target: '#preview', 
    success: function() {  
    $('#form id').slideDown('slow'),
<!-- RESET THE FORM FIELDS AFTER SUBMIT STARTS HERE-->
    $("#form")[0].reset();
<!--RESET THE FORM FIELDS AFTER SUBMIT  ENDS HERE--->
    } 

}); 

                }

There are two simple ways that will allow you to render a success message. You can either use ajax with the callback success function, or if you want a full post, you you can check at the top of your file if a certain POST was set, and if so, render a success message.

Here is an example of checking POST:

if(isset($_POST['name attribute posting'])) {
    $util->showSuccessMessage();
    //OR echo "<div class='popup'></div>"
}

And here is an example of using Ajax's success callback function:

function submitForm() {
    $.ajax({
        url : 'this_file.php',
        type: 'POST',
        success : showSuccessMessage   //function call
    })
}
    $(function() {
           function validateform() {
              var valid = true;
              $(".req").css("border","1px solid #ccc");
              $(".req").each(function() {
                 if ( $(this).val() == "" || $(this).val().replace(/\s/g, '').length == 0 ) {
                    $(this).css("border","1px solided");
                    $(".required").css("display","block");
                    valid = false;
                 }

              });
              return valid;
           }    
           $("#submit").click( function() {
              $('#myform').submit(function()

               {
               if( validateform)
              {
                $('$name').submit();
             }
         } );

           });  
       });

reference submit

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM