Metro-UI-CSS icon indicating copy to clipboard operation
Metro-UI-CSS copied to clipboard

Prevent buttons sending a form

Open dkova opened this issue 12 years ago • 12 comments

Good day. I have a form in the Metro Dialog with a file input. When I click btn-file (btn-close,...), form is submiting. How to prevent it? Because I want to choose a file, and I use an AJAX submitting.

  $("#loginform").submit(function(e)
  {
      var postData = $(this).serializeArray();
      var formURL = $(this).attr("action");
      $.ajax(
      {
          url : formURL,
          type: "POST",
          data : postData,
          success:function(response)
          {
          response = $.trim(response);
          document.cookie = 'hash=' + response;
          window.location.reload();
          }
      });
      e.preventDefault();
      e.unbind();
  });
      $("#wlogin").click(function(){
        var content = '<? include '/templates/wlogin.php';?>';
        $.Dialog({
        overlay: true,
        shadow: true,
        flat: true,
        icon: '<i class="icon-enter"></i>',
        title: '<?=$LoginButton;?>',
        content: content,
        padding: 10
        });
        });

wlogin.php

<form id="loginform" action="/include/proc/login.php" method="post">' +
        '<label><?=$Login;?></label>' +
        '<div class="input-control text"><input type="text" name="login"><button class="btn-clear"></button></div> ' +
        '<label><?=$Password;?></label>' +
        '<div class="input-control password"><input type="password" name="password"><button class="btn-reveal"></button></div> ' +
        '<div class="input-control checkbox"><label><input type="checkbox" name="remember" checked/><span class="check"></span>Check me out</label></div>'+
        '<div class="form-actions">' +
        '<input class="button primary" type="submit"/>'+
        '</div>'+
        '</form>

dkova avatar Nov 01 '14 11:11 dkova

“e.preventDefault();” should be called first. $("#loginform").submit(function(e) { e.preventDefault(); e.unbind();

  var postData = $(this).serializeArray();
  var formURL = $(this).attr("action");
  $.ajax(
  {
      url : formURL,
      type: "POST",
      data : postData,
      success:function(response)
      {
      response = $.trim(response);
      document.cookie = 'hash=' + response;
      window.location.reload();
      }
  });
    });
  $("#wlogin").click(function(){
    var content = '<? include '/templates/wlogin.php';?>';
    $.Dialog({
    overlay: true,
    shadow: true,
    flat: true,
    icon: '<i class="icon-enter"></i>',
    title: '<?=$LoginButton;?>',
    content: content,
    padding: 10
    });
    });

wlogin.php

' + '' + '
' + '' + '
' + '
'+ '
' + ''+ '
'+ '

bruno4eva avatar Nov 01 '14 11:11 bruno4eva

Doesn't work...

dkova avatar Nov 01 '14 12:11 dkova

And how to make btn-file, btn-clear to do their usual function?

dkova avatar Nov 01 '14 12:11 dkova

use a button to submit forms like this

$('#submit').on('click', function (e) {

      e.preventDefault(); ** prevent automatic submission **
  $('#loader-gif').show() **if any**;
      $.ajax({

        type: 'post',
        url: 'form_process.phpl' **action path**,
        data: $('form').serialize() ** picks data in the forms **,
        success: function () {
              $('#loader-gif').hide() ** if used a loader hide it**;
          $('#form_status').fadeIn() ** status as in “”login successfully. Click to continue**;
          $('form').trigger("reset") ** empty form values **;
        }
      });


    });

**remove all “__” from jquery codes. its only for comment***************

bruno4eva avatar Nov 01 '14 13:11 bruno4eva

Not working. I think that metro-dialog.js is needed in change, because even Choose file button sends a data.

dkova avatar Nov 01 '14 13:11 dkova

Hi. Set js global variable METRO_AUTO_REINIT to true.

olton avatar Nov 01 '14 14:11 olton

Sorry, but where? In metro-global.js? This isn't included in my HTML

dkova avatar Nov 01 '14 14:11 dkova

in the head (or footer) of main page

<script src="metro.min.js"></script> <script> METRO_AUTO_REINIT = true; </script>

olton avatar Nov 01 '14 14:11 olton

btn-clear and btn-file started to work! but ajax sending...

dkova avatar Nov 01 '14 14:11 dkova

  1. set form action to action="javascript:void(0)"
  2. form action url write in $.ajax({url: ....})

olton avatar Nov 01 '14 14:11 olton

Thank you very much! It works!

dkova avatar Nov 01 '14 14:11 dkova

I got this from another user but try adding type="button" inside the