Using Jquery Validator and Sweetalert for Form Submission Confirmation Box











up vote
0
down vote

favorite












I am trying to do the following.



I have 3 buttons, "Continue", "Delete", "New". All of these are type "submit" buttons.



When the form is posted, the value of these buttons is used to determine what happens on the backend.



I want a Sweetalert popup to confirm if the user wants to delete, then when the OK button is hit, the form should progress and submit to the backend.



The issue I currently have is that there is no way to get the value of the submit button to the server side when I incorporate Sweetalert to show when I hit the submit button.



Scenario A) For example, if I remove the "e.preventDefault" from the callback, the alert popups for a brief second but the form submits, not giving the user to confirm or cancel the deletion. This is not correct behaviour, but the "submit" button value does indeed get posted to the server. So to prevent the quick popup issue, I add "e.preventDefault" to stop the normal behaviour of "submit" button.



Scenario B) However, when I have the code as below (i.e. "e.preventDefault" added to the code), the form does not submit after validation. Instead I have to run "$('#myForm').submit()". But when I do this, the value of the "submit" button does not get posted to the server.



If someone could kindly guide me on how I can use jQuery Validate and Sweetalert, it would be much appreciated.



An example of the set up looks like this:



HTML:



<script src="https://cdn.jsdelivr.net/npm/jquery-validation@1.17.0/dist/jquery.validate.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/2.1.2/sweetalert.min.js"></script>


<form id="myForm" action="/postHere" method="post">
<!-- Options-->
<div class="row-select">
<label for="options">My Options</label>
<select name="options" id="options">
<option disabled="true" selected="selected">Select an Option</option>
<option value="a">a</option>
<option value="b">b</option>
</select>
</div>

<!-- continue details form -->
<div>
<input type="submit" id="continue" name="submitButton" value="Continue this Option">
</div>

<!-- delete details -->
<div>
<input class="delete" type="submit" id="delete" name="submitButton" value="Delete this Option">
</div>

<!-- new -->
<div class="row-spaced">
<input type="submit" id="submitButton" name="submitButton" value="Neither" formnovalidate>
</div>
</form>


Javascript:



function myFunction() {
var continueOption = document.getElementById('continue');
var deleteOption = document.getElementById('delete');
var neither = document.getElementById('new');

$('#myForm').validate({
ignore:'',
rules: {
options: "required"
},

});

// AIM IS TO SHOW A CONFIRMATION BOX IF DELETE IS CLICKED
if(deleteOption) {
deleteOption.addEventListener("click", function(e) {
e.preventDefault();
swal({
title: "Are you sure?",
text: "Once deleted, you will not be able to recover this imaginary file!",
buttons: true,
dangerMode: true,
})
.then((willDelete) => {
if (willDelete) {
console.log("here");
$('#myForm').submit();
}
});
});
}
}

myFunction();


Backend Illustration:



router.post("/postHERE", (req, res) => {
if (req.body.submitButton == "Continue this Option") {
// Do something CONTINUE
}
else if (req.body.submitButton == "Delete this Option") {
// Do something DELETE
}
else {
// Do something NEW
}


}



JSFiddle:



JSFiddle Example



EDIT:



For the backend I am using Expressjs. I have added this in and fixed a type in the form method to be post.










share|improve this question
























  • What do you mean you want "the value of the submit button" to be sent to the server? Typically, you would use your server-side code to get the unique name of the submit button from the POST array. This has absolutely nothing to do with client-side JavaScript, popups or form validation. (And you would use POST when sending data to the server, not GET.)
    – Sparky
    Nov 11 at 16:16












  • The jQuery Validate plugin only knows it's triggered by a submit button, however, there is nothing in this plugin that identifies which submit button is used. Again, you would need to look for the name of the button in the POST array on the server side, so every button should have a unique name, not all of them called submitButton.
    – Sparky
    Nov 11 at 16:46












  • Thanks - what I meant is that i am using ExpressJS and want the value of the submit button to make it there. The submit button has a value that gets sent to the server. I parse this and it ends up in req.body as "req.body.submitButton". I then have a bit of logic that runs based on what value the "req.body.submitButton" has.
    – Jin Tao
    Nov 11 at 16:57












  • "The submit button has a value that gets sent to the server." ~ if the value is already sent to the server, then what is the problem? Your question is very unclear.
    – Sparky
    Nov 11 at 18:01










  • If you want to use the jQuery Validate plugin, then you absolutely should not be using e.preventDefault(). The validation plugin already takes care of this part automatically.
    – Sparky
    Nov 11 at 18:03















up vote
0
down vote

favorite












I am trying to do the following.



I have 3 buttons, "Continue", "Delete", "New". All of these are type "submit" buttons.



When the form is posted, the value of these buttons is used to determine what happens on the backend.



I want a Sweetalert popup to confirm if the user wants to delete, then when the OK button is hit, the form should progress and submit to the backend.



The issue I currently have is that there is no way to get the value of the submit button to the server side when I incorporate Sweetalert to show when I hit the submit button.



Scenario A) For example, if I remove the "e.preventDefault" from the callback, the alert popups for a brief second but the form submits, not giving the user to confirm or cancel the deletion. This is not correct behaviour, but the "submit" button value does indeed get posted to the server. So to prevent the quick popup issue, I add "e.preventDefault" to stop the normal behaviour of "submit" button.



Scenario B) However, when I have the code as below (i.e. "e.preventDefault" added to the code), the form does not submit after validation. Instead I have to run "$('#myForm').submit()". But when I do this, the value of the "submit" button does not get posted to the server.



If someone could kindly guide me on how I can use jQuery Validate and Sweetalert, it would be much appreciated.



An example of the set up looks like this:



HTML:



<script src="https://cdn.jsdelivr.net/npm/jquery-validation@1.17.0/dist/jquery.validate.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/2.1.2/sweetalert.min.js"></script>


<form id="myForm" action="/postHere" method="post">
<!-- Options-->
<div class="row-select">
<label for="options">My Options</label>
<select name="options" id="options">
<option disabled="true" selected="selected">Select an Option</option>
<option value="a">a</option>
<option value="b">b</option>
</select>
</div>

<!-- continue details form -->
<div>
<input type="submit" id="continue" name="submitButton" value="Continue this Option">
</div>

<!-- delete details -->
<div>
<input class="delete" type="submit" id="delete" name="submitButton" value="Delete this Option">
</div>

<!-- new -->
<div class="row-spaced">
<input type="submit" id="submitButton" name="submitButton" value="Neither" formnovalidate>
</div>
</form>


Javascript:



function myFunction() {
var continueOption = document.getElementById('continue');
var deleteOption = document.getElementById('delete');
var neither = document.getElementById('new');

$('#myForm').validate({
ignore:'',
rules: {
options: "required"
},

});

// AIM IS TO SHOW A CONFIRMATION BOX IF DELETE IS CLICKED
if(deleteOption) {
deleteOption.addEventListener("click", function(e) {
e.preventDefault();
swal({
title: "Are you sure?",
text: "Once deleted, you will not be able to recover this imaginary file!",
buttons: true,
dangerMode: true,
})
.then((willDelete) => {
if (willDelete) {
console.log("here");
$('#myForm').submit();
}
});
});
}
}

myFunction();


Backend Illustration:



router.post("/postHERE", (req, res) => {
if (req.body.submitButton == "Continue this Option") {
// Do something CONTINUE
}
else if (req.body.submitButton == "Delete this Option") {
// Do something DELETE
}
else {
// Do something NEW
}


}



JSFiddle:



JSFiddle Example



EDIT:



For the backend I am using Expressjs. I have added this in and fixed a type in the form method to be post.










share|improve this question
























  • What do you mean you want "the value of the submit button" to be sent to the server? Typically, you would use your server-side code to get the unique name of the submit button from the POST array. This has absolutely nothing to do with client-side JavaScript, popups or form validation. (And you would use POST when sending data to the server, not GET.)
    – Sparky
    Nov 11 at 16:16












  • The jQuery Validate plugin only knows it's triggered by a submit button, however, there is nothing in this plugin that identifies which submit button is used. Again, you would need to look for the name of the button in the POST array on the server side, so every button should have a unique name, not all of them called submitButton.
    – Sparky
    Nov 11 at 16:46












  • Thanks - what I meant is that i am using ExpressJS and want the value of the submit button to make it there. The submit button has a value that gets sent to the server. I parse this and it ends up in req.body as "req.body.submitButton". I then have a bit of logic that runs based on what value the "req.body.submitButton" has.
    – Jin Tao
    Nov 11 at 16:57












  • "The submit button has a value that gets sent to the server." ~ if the value is already sent to the server, then what is the problem? Your question is very unclear.
    – Sparky
    Nov 11 at 18:01










  • If you want to use the jQuery Validate plugin, then you absolutely should not be using e.preventDefault(). The validation plugin already takes care of this part automatically.
    – Sparky
    Nov 11 at 18:03













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am trying to do the following.



I have 3 buttons, "Continue", "Delete", "New". All of these are type "submit" buttons.



When the form is posted, the value of these buttons is used to determine what happens on the backend.



I want a Sweetalert popup to confirm if the user wants to delete, then when the OK button is hit, the form should progress and submit to the backend.



The issue I currently have is that there is no way to get the value of the submit button to the server side when I incorporate Sweetalert to show when I hit the submit button.



Scenario A) For example, if I remove the "e.preventDefault" from the callback, the alert popups for a brief second but the form submits, not giving the user to confirm or cancel the deletion. This is not correct behaviour, but the "submit" button value does indeed get posted to the server. So to prevent the quick popup issue, I add "e.preventDefault" to stop the normal behaviour of "submit" button.



Scenario B) However, when I have the code as below (i.e. "e.preventDefault" added to the code), the form does not submit after validation. Instead I have to run "$('#myForm').submit()". But when I do this, the value of the "submit" button does not get posted to the server.



If someone could kindly guide me on how I can use jQuery Validate and Sweetalert, it would be much appreciated.



An example of the set up looks like this:



HTML:



<script src="https://cdn.jsdelivr.net/npm/jquery-validation@1.17.0/dist/jquery.validate.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/2.1.2/sweetalert.min.js"></script>


<form id="myForm" action="/postHere" method="post">
<!-- Options-->
<div class="row-select">
<label for="options">My Options</label>
<select name="options" id="options">
<option disabled="true" selected="selected">Select an Option</option>
<option value="a">a</option>
<option value="b">b</option>
</select>
</div>

<!-- continue details form -->
<div>
<input type="submit" id="continue" name="submitButton" value="Continue this Option">
</div>

<!-- delete details -->
<div>
<input class="delete" type="submit" id="delete" name="submitButton" value="Delete this Option">
</div>

<!-- new -->
<div class="row-spaced">
<input type="submit" id="submitButton" name="submitButton" value="Neither" formnovalidate>
</div>
</form>


Javascript:



function myFunction() {
var continueOption = document.getElementById('continue');
var deleteOption = document.getElementById('delete');
var neither = document.getElementById('new');

$('#myForm').validate({
ignore:'',
rules: {
options: "required"
},

});

// AIM IS TO SHOW A CONFIRMATION BOX IF DELETE IS CLICKED
if(deleteOption) {
deleteOption.addEventListener("click", function(e) {
e.preventDefault();
swal({
title: "Are you sure?",
text: "Once deleted, you will not be able to recover this imaginary file!",
buttons: true,
dangerMode: true,
})
.then((willDelete) => {
if (willDelete) {
console.log("here");
$('#myForm').submit();
}
});
});
}
}

myFunction();


Backend Illustration:



router.post("/postHERE", (req, res) => {
if (req.body.submitButton == "Continue this Option") {
// Do something CONTINUE
}
else if (req.body.submitButton == "Delete this Option") {
// Do something DELETE
}
else {
// Do something NEW
}


}



JSFiddle:



JSFiddle Example



EDIT:



For the backend I am using Expressjs. I have added this in and fixed a type in the form method to be post.










share|improve this question















I am trying to do the following.



I have 3 buttons, "Continue", "Delete", "New". All of these are type "submit" buttons.



When the form is posted, the value of these buttons is used to determine what happens on the backend.



I want a Sweetalert popup to confirm if the user wants to delete, then when the OK button is hit, the form should progress and submit to the backend.



The issue I currently have is that there is no way to get the value of the submit button to the server side when I incorporate Sweetalert to show when I hit the submit button.



Scenario A) For example, if I remove the "e.preventDefault" from the callback, the alert popups for a brief second but the form submits, not giving the user to confirm or cancel the deletion. This is not correct behaviour, but the "submit" button value does indeed get posted to the server. So to prevent the quick popup issue, I add "e.preventDefault" to stop the normal behaviour of "submit" button.



Scenario B) However, when I have the code as below (i.e. "e.preventDefault" added to the code), the form does not submit after validation. Instead I have to run "$('#myForm').submit()". But when I do this, the value of the "submit" button does not get posted to the server.



If someone could kindly guide me on how I can use jQuery Validate and Sweetalert, it would be much appreciated.



An example of the set up looks like this:



HTML:



<script src="https://cdn.jsdelivr.net/npm/jquery-validation@1.17.0/dist/jquery.validate.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/2.1.2/sweetalert.min.js"></script>


<form id="myForm" action="/postHere" method="post">
<!-- Options-->
<div class="row-select">
<label for="options">My Options</label>
<select name="options" id="options">
<option disabled="true" selected="selected">Select an Option</option>
<option value="a">a</option>
<option value="b">b</option>
</select>
</div>

<!-- continue details form -->
<div>
<input type="submit" id="continue" name="submitButton" value="Continue this Option">
</div>

<!-- delete details -->
<div>
<input class="delete" type="submit" id="delete" name="submitButton" value="Delete this Option">
</div>

<!-- new -->
<div class="row-spaced">
<input type="submit" id="submitButton" name="submitButton" value="Neither" formnovalidate>
</div>
</form>


Javascript:



function myFunction() {
var continueOption = document.getElementById('continue');
var deleteOption = document.getElementById('delete');
var neither = document.getElementById('new');

$('#myForm').validate({
ignore:'',
rules: {
options: "required"
},

});

// AIM IS TO SHOW A CONFIRMATION BOX IF DELETE IS CLICKED
if(deleteOption) {
deleteOption.addEventListener("click", function(e) {
e.preventDefault();
swal({
title: "Are you sure?",
text: "Once deleted, you will not be able to recover this imaginary file!",
buttons: true,
dangerMode: true,
})
.then((willDelete) => {
if (willDelete) {
console.log("here");
$('#myForm').submit();
}
});
});
}
}

myFunction();


Backend Illustration:



router.post("/postHERE", (req, res) => {
if (req.body.submitButton == "Continue this Option") {
// Do something CONTINUE
}
else if (req.body.submitButton == "Delete this Option") {
// Do something DELETE
}
else {
// Do something NEW
}


}



JSFiddle:



JSFiddle Example



EDIT:



For the backend I am using Expressjs. I have added this in and fixed a type in the form method to be post.







javascript jquery jquery-validate sweetalert






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 17:06

























asked Nov 11 at 15:42









Jin Tao

11




11












  • What do you mean you want "the value of the submit button" to be sent to the server? Typically, you would use your server-side code to get the unique name of the submit button from the POST array. This has absolutely nothing to do with client-side JavaScript, popups or form validation. (And you would use POST when sending data to the server, not GET.)
    – Sparky
    Nov 11 at 16:16












  • The jQuery Validate plugin only knows it's triggered by a submit button, however, there is nothing in this plugin that identifies which submit button is used. Again, you would need to look for the name of the button in the POST array on the server side, so every button should have a unique name, not all of them called submitButton.
    – Sparky
    Nov 11 at 16:46












  • Thanks - what I meant is that i am using ExpressJS and want the value of the submit button to make it there. The submit button has a value that gets sent to the server. I parse this and it ends up in req.body as "req.body.submitButton". I then have a bit of logic that runs based on what value the "req.body.submitButton" has.
    – Jin Tao
    Nov 11 at 16:57












  • "The submit button has a value that gets sent to the server." ~ if the value is already sent to the server, then what is the problem? Your question is very unclear.
    – Sparky
    Nov 11 at 18:01










  • If you want to use the jQuery Validate plugin, then you absolutely should not be using e.preventDefault(). The validation plugin already takes care of this part automatically.
    – Sparky
    Nov 11 at 18:03


















  • What do you mean you want "the value of the submit button" to be sent to the server? Typically, you would use your server-side code to get the unique name of the submit button from the POST array. This has absolutely nothing to do with client-side JavaScript, popups or form validation. (And you would use POST when sending data to the server, not GET.)
    – Sparky
    Nov 11 at 16:16












  • The jQuery Validate plugin only knows it's triggered by a submit button, however, there is nothing in this plugin that identifies which submit button is used. Again, you would need to look for the name of the button in the POST array on the server side, so every button should have a unique name, not all of them called submitButton.
    – Sparky
    Nov 11 at 16:46












  • Thanks - what I meant is that i am using ExpressJS and want the value of the submit button to make it there. The submit button has a value that gets sent to the server. I parse this and it ends up in req.body as "req.body.submitButton". I then have a bit of logic that runs based on what value the "req.body.submitButton" has.
    – Jin Tao
    Nov 11 at 16:57












  • "The submit button has a value that gets sent to the server." ~ if the value is already sent to the server, then what is the problem? Your question is very unclear.
    – Sparky
    Nov 11 at 18:01










  • If you want to use the jQuery Validate plugin, then you absolutely should not be using e.preventDefault(). The validation plugin already takes care of this part automatically.
    – Sparky
    Nov 11 at 18:03
















What do you mean you want "the value of the submit button" to be sent to the server? Typically, you would use your server-side code to get the unique name of the submit button from the POST array. This has absolutely nothing to do with client-side JavaScript, popups or form validation. (And you would use POST when sending data to the server, not GET.)
– Sparky
Nov 11 at 16:16






What do you mean you want "the value of the submit button" to be sent to the server? Typically, you would use your server-side code to get the unique name of the submit button from the POST array. This has absolutely nothing to do with client-side JavaScript, popups or form validation. (And you would use POST when sending data to the server, not GET.)
– Sparky
Nov 11 at 16:16














The jQuery Validate plugin only knows it's triggered by a submit button, however, there is nothing in this plugin that identifies which submit button is used. Again, you would need to look for the name of the button in the POST array on the server side, so every button should have a unique name, not all of them called submitButton.
– Sparky
Nov 11 at 16:46






The jQuery Validate plugin only knows it's triggered by a submit button, however, there is nothing in this plugin that identifies which submit button is used. Again, you would need to look for the name of the button in the POST array on the server side, so every button should have a unique name, not all of them called submitButton.
– Sparky
Nov 11 at 16:46














Thanks - what I meant is that i am using ExpressJS and want the value of the submit button to make it there. The submit button has a value that gets sent to the server. I parse this and it ends up in req.body as "req.body.submitButton". I then have a bit of logic that runs based on what value the "req.body.submitButton" has.
– Jin Tao
Nov 11 at 16:57






Thanks - what I meant is that i am using ExpressJS and want the value of the submit button to make it there. The submit button has a value that gets sent to the server. I parse this and it ends up in req.body as "req.body.submitButton". I then have a bit of logic that runs based on what value the "req.body.submitButton" has.
– Jin Tao
Nov 11 at 16:57














"The submit button has a value that gets sent to the server." ~ if the value is already sent to the server, then what is the problem? Your question is very unclear.
– Sparky
Nov 11 at 18:01




"The submit button has a value that gets sent to the server." ~ if the value is already sent to the server, then what is the problem? Your question is very unclear.
– Sparky
Nov 11 at 18:01












If you want to use the jQuery Validate plugin, then you absolutely should not be using e.preventDefault(). The validation plugin already takes care of this part automatically.
– Sparky
Nov 11 at 18:03




If you want to use the jQuery Validate plugin, then you absolutely should not be using e.preventDefault(). The validation plugin already takes care of this part automatically.
– Sparky
Nov 11 at 18:03

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53250357%2fusing-jquery-validator-and-sweetalert-for-form-submission-confirmation-box%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53250357%2fusing-jquery-validator-and-sweetalert-for-form-submission-confirmation-box%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Florida Star v. B. J. F.

Danny Elfman

Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues