Skip to content

Commit

Permalink
fix(form): double post broken
Browse files Browse the repository at this point in the history
  • Loading branch information
btry committed May 12, 2023
1 parent 252ef20 commit 07b8a1a
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
2 changes: 1 addition & 1 deletion ajax/formanswer.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
die();
}

if (!isset($_POST['submit_formcreator']) || !isset($_POST['plugin_formcreator_forms_id'])) {
if (!isset($_POST['add']) || !isset($_POST['plugin_formcreator_forms_id'])) {
http_response_code(500);
die();
}
Expand Down
11 changes: 7 additions & 4 deletions js/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -865,7 +865,7 @@ var plugin_formcreator = new function() {
}
}

$('[name="submit_formcreator"]').toggle(submitButtonToShow == true);
$('#plugin_formcreator_form.plugin_formcreator_form button[name="add"]').toggle(submitButtonToShow == true);
});
};

Expand Down Expand Up @@ -1353,10 +1353,10 @@ var plugin_formcreator = new function() {
});
};

this.submitUserForm = function () {
this.submitUserForm = function (event) {
var form = document.querySelector('form[role="form"][data-itemtype]');
var data = new FormData(form);
data.append('submit_formcreator', '');
data.append('add', '');
$.post({
url: formcreatorRootDoc + '/ajax/formanswer.php',
processData: false,
Expand Down Expand Up @@ -1393,11 +1393,14 @@ var plugin_formcreator = new function() {
initMessagesAfterRedirectToasts();
}
});
event.preventDefault();
blockFormSubmit($(form), event);
return false;
};

this.submitUserFormByKeyPress = function (event) {
var keyPressed = event.keyCode || event.which;
if (keyPressed === 13 && $('[name="submit_formcreator"]').is(':hidden')) {
if (keyPressed === 13 && $('#plugin_formcreator_form.plugin_formcreator_form button[name="add"]').is(':hidden')) {
event.preventDefault();
return false;
}
Expand Down
4 changes: 2 additions & 2 deletions templates/pages/userform.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@

<div class="asset">
{% set formName = 'plugin_formcreator_form' %}
<form name="{{ formName }}" method="post" role="form" enctype="multipart/form-data" class="plugin_formcreator_form" action="javascript:plugin_formcreator.submitUserForm()" onkeypress="return plugin_formcreator.submitUserFormByKeyPress(event);" id="plugin_formcreator_form" data-itemtype="PluginFormcreatorForm" data-id="{{ item.fields['id'] }}" data-submit-once="true">
<form name="{{ formName }}" method="post" role="form" enctype="multipart/form-data" class="plugin_formcreator_form" onsubmit="return plugin_formcreator.submitUserForm(event)" onkeypress="return plugin_formcreator.submitUserFormByKeyPress(event);" id="plugin_formcreator_form" data-itemtype="PluginFormcreatorForm" data-id="{{ item.fields['id'] }}" data-submit-once="true">
<h1 class='form-title'>
{{ call('Glpi\\Toolbox\\Sanitizer::unsanitize', [__(item.fields['name'], options.domain)]) }}
<i class="fas fa-print" style="cursor: pointer;" onclick="window.print();"></i>
Expand Down Expand Up @@ -137,7 +137,7 @@
<div class="center">
{{ call('Html::submit', [
__('Send'),
{'name': 'submit_formcreator'}
{'name': 'add'}
])|raw }}
</div>
{{ fields.hiddenField('plugin_formcreator_forms_id', item.fields['id']) }}
Expand Down

0 comments on commit 07b8a1a

Please sign in to comment.