This SweetAlert works successfully upon the Form submitting:
$('#upload-form').on('submit',function(e){
e.preventDefault();
var form = $('form');
swal.fire({
title: "Are you sure?",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "Yes, submit it!",
closeOnConfirm: false
}, function(isConfirm){
console.log("sdfsf");
if (isConfirm) form.submit();
});
});
however, it doesnât display for longer than two seconds, before the Form automatically submits.
The SweetAlert is supposed to stay displayed and allow the confirm button to be selected to proceed.
(this is different than my other posting, in that this code is not in the BeforeSend function - thanks for the replies).
Any assistance is welcomedâŚ
(It was suggested to make the Submit button a button that opens the alert, and make the Formâs submit-btn button the swal confirmButtonâŚbut Iâm not sure how to do that (but Iâve tried))
So⌠on form submit, you create a confirm dialog⌠that⌠submits the formâŚ
âŚ
I feel like this is going to go around in circlesâŚ
Thanks for your message.
upon Form submit, a confirm dialog box appears, I wouldnât say that the dialog code submits the Form, because itâs not displayed long enough for someone to click the displayed dialog box choices, either 'Yes, submit it" or âCancelâ.
it was suggested trying having the submit button not submit the form, have it be a regular button, not a form submit button, so then, when that regular button is clicked, make it SweetAlert Swal.fire, and then make the âyes, submit itâ be the submit button. But I havenât been able to get that correct, in my attempts at switching those buttons.
I look forward to any guidance
⌠i would say it does? Cause you wrote those exact words into it?
Thatâs not whats making it submit now, but itâs going to be a problem in the immediate future.
From what your previous thread said, iâm guessing youâve still got code lingering around listening to the original form submit and its firing on this one. Impossible to tell without seeing the full page.
1 Like
Thanks again for your reply.
Hereâs the full code, if you could take a glance over it, that would be great.
The JS weâre discussing is around line 285
and the submit button for the form is around line 378.
I look forward to your comments.
so⌠#upload-form is a div.
a div will never fire an onsubmit event⌠at least, it shouldnt be.
Your form is submitting itself, before the alert can intervene. Trigger off of (and prevent default from) the actual form.
Or dont even make the button in the form an actual submit button, and have the button execute the alert and submit. Which is probably the better route.
<button type="submit" id="submit-btn"
Remove type.
$('#upload-form').on('btn-submit',function(e){
âbtn-submitâ isnt an event type, so this code will never fire.
change to
$('#submit-btn').on('click',function(e){
var form = $('form');
Too vague - youâve got multiple forms on the page, so this is going to find-and-fire all of them.
var form = $('.pt_upld_page_frm');
1 Like
Many thanks for your kind assistance.
I have made the suggested changes:
$('#submit-btn').on('click',function(e){
e.preventDefault();
var form = $('.pt_upld_page_frm');
swal.fire({
title: "Are you sure?",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "Yes, submit it!",
closeOnConfirm: false
}, function(isConfirm){
console.log("sdfsf");
if (isConfirm) form.submit();
});
});
and:
<button id="submit-btn" class="btn btn-main setting-panel-mdbtn" disabled>< etc....
and upon proceeding with an upload, the dialog box displays when the Submit button is clicked, and it successfully stays open until the dialog box button choice âCancelâ is selected, and the dialog box closes/disappears. Thatâs a big improvement. Much thanks.
However, when the other dialog box button choice âYes, submit it!â is selected the dialog box closes/disappears, and the Form isnât submitted. Upon clicking the Submit button again, the dialog box opens again.
I look forward to your comments and any help with getting the âYes, submit it!â button to submit the Form.
so I cant find an option on the SweetAlert page that matches âcloseOnConfirmâ, so somethingâs wrong in the demo youâre using as an example.
swal.fire should return a Promise. And based on their example âA confirm dialog, with a function attached to the âConfirmâ-buttonâ on their website, what you should do here:
is instead end the fire and tack onto the PromiseâŚsoâŚ
}).then((result) => {
if (result.isConfirmed) { $('.pt_upld_page_frm').submit(); }
});
To be clear, youâre using sweetalert2, right?
Thanks, sweetalert2 I believe so.
Per your suggestion:
$('#submit-btn').on('click',function(e){
e.preventDefault();
var form = $('.pt_upld_page_frm');
//var form = $('form');
swal.fire({
title: "Are you sure?",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "Yes, submit it!",
closeOnConfirm: false
}).then((result) => {
if (result.isConfirmed) { $('.pt_upld_page_frm').submit(); }
});
});
âyes, Submit it!â now closes the dialog box and directs back to the select a file to upload page,
but the file is not uploaded.
As you may recall, towards the bottom of the file youâll see:
$('#upload-form form').ajaxForm({
url: '{{LINK aj/ffmpeg-submit}}?hash=' + $('.main_session').val(),
beforeSend: function() {
$('#submit-btn').attr('disabled', true);
$('#submit-btn').val("{{LANG please_wait}}");
},
success: function(data) {
if (data.status == 200) {
//window.location.href = data.link;
window.location.href = '{{LINK home}}';
}
where it shows when the file is actually uploaded the redirect is to the home page.
So, somehow the sweetAlert isnât uploading and redirecting properly.
Any ideas are welcomed.
well since youâre taking control of the submit flow anyway, iâd just use ajaxSubmit in the then instead of setting up the form with ajaxForm and then relying on the secondary flow of submitting.
I greatly appreciate your reply and suggestion, I just donât know how to do that from a code standpoint. Not sure how to âuse ajaxSubmit in the thenâ. Iâd be grateful for any additional guidance with that coding
So the same place you learned to use ajaxForm, you would look at ajaxSubmit.
Much appreciated for the clue, but that is unclear to me on how to accomplish what youâre saying
closeOnConfirm: false
}).then((result) => {
//if (result.isConfirmed) { $('.pt_upld_page_frm').submit(); }
if (result.isConfirmed) { $('.upload-form').submit(); }
I tried this without success (no submission)
any additional guidance is appreciated
take the options in your ajaxForm code, put it into an ajaxSubmit function instead, put that inside your button handler.