I'm using https://jqueryvalidation.org/ plugin and https://craftpip.github.io/jquery-confirm plugin.
I've this code of script:
$("#form1").validate({
submitHandler: function(form) {
$.confirm({
title: 'Confirm',
content: 'Are you sure?',
buttons: {
ok: {
text: "OK",
btnClass: 'btn-success',
action: function () {
form.submit();
}
},
cancel: {
text: "Annulla",
action: function () {
}
}
}
});
}
);
and the form:
<form id="form1" name="form1" action="index.php" method="post">
<input type="text" name="var1" value="1">
<input type="text" name="var2" value="2">
<input type="text" name="var3" value="3">
<button type="submit" class="btn btn-info" value="edit" name="action">EDIT</button>
</form>
the page itself index.php:
if ( isset($_POST['action']) && $_POST['action'] == "edit" ) {
echo "EDITED";
exit();
}
var_dump($_POST);
The problem is that button submit is not passed!! I don't know why. My var_dump says there are all 3 inputs type="text" but not the button.
I tried removing jquery-confirm and it's ok, all inputs and button type submit are passed:
$("#form1").validate({
submitHandler: function(form) {
form.submit();
}
);
I don't know how to solve. I don't know how to post an example with jsfiddle using $_POST and PHP.
Thats because submit()
does not include the submit button. You'd have to push the value of the button manually.
An example on how this might be done:
$('document').ready(function() {
$("#form1").validate({
submitHandler: function(form) {
$.confirm({
title: 'Confirm',
content: 'Are you sure?',
buttons: {
ok: {
text: "OK",
btnClass: 'btn-success',
action: function () {
let variables = $('#form1').serializeArray();
variables.push({name: $('#form1 button').attr('name'), value: $('#form1 button').attr('value')})
// Perform custom XHR here ($.ajax) with `variables`
}
},
cancel: {
text: "Annulla",
action: function () {
}
}
}
});
}
});
});
https://jsfiddle.net/n2gwjvqd/2/
PS: it makes sense to cache jquery selectors so
variables.push({name: $('#form1 button').attr('name'), value: $('#form1 button').attr('value')})
might become
let button = $('#form1 button');
variables.push({name: button.attr('name'), value: button.attr('value')})
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.