Prevent buttons sending a form
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>
“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
Doesn't work...
And how to make btn-file, btn-clear to do their usual function?
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***************
Not working. I think that metro-dialog.js is needed in change, because even Choose file button sends a data.
Hi. Set js global variable METRO_AUTO_REINIT to true.
Sorry, but where? In metro-global.js? This isn't included in my HTML
in the head (or footer) of main page
<script src="metro.min.js"></script> <script> METRO_AUTO_REINIT = true; </script>
btn-clear and btn-file started to work! but ajax sending...
- set form action to action="javascript:void(0)"
- form action url write in $.ajax({url: ....})
Thank you very much! It works!
I got this from another user but try adding type="button" inside the