I´m working on a academic php/javascript web project and I´m having some problems. In this project´s arquitecture, data is always sent to the server by POST using 'hidden forms'. This forms are built by the followings functions:
function crearform(name, method){
var formu = document.createElement('form');
document.body.appendChild(formu);
formu.name = name;
formu.method = method;
formu.action = 'index.php';
}
function insertacampo(form ,name, value){
formulario = form;
var input = document.createElement('input');
input.type = 'hidden';
input.name = name;
input.value = value;
formulario.appendChild(input);
}
function enviaform(form){
form.submit();
}
A example use:
<a class="btn-get-started i18n-cancelar" id="btn-cancel" type="button" onclick="
crearform('formenviar','post');
insertacampo(document.formenviar,'document_id', '<?php echo $this->document['document_id'] ?>');
insertacampo(document.formenviar,'controller','ImpDoc');
insertacampo(document.formenviar,'action','show');
enviaform(document.formenviar);">
Return
</a>
This works perfectly fine! but for the next step I added a stack so the application can store all the POSTs received. In this way, for the return action I just have to get the last POST of the stack and I already have the params 'controller', 'action' ... availables.
And... here is the problem. The last POST stored on the stack is accessible through a php variable.
var_dump output: Last POST stored
array(2) { ["controller"]=> string(6) "Portal" ["action"]=> string(8) "_default" }
I created a javascript function that receives the array with the POST values and adds them to a 'hidden form'.
function multiple_addfield(form, params) {
for(let key in params) {
let input = document.createElement('input');
input.type = 'hidden';
input.name = key;
input.value = params[key];
form.appendChild(input);
}
}
And built the form like this:
<a class="btn-get-started i18n-back" type="button" onclick="
crearform('formenviar','post');
multiple_addfield(document.formenviar, <?php echo json_encode($this->previousShow); ?>);
enviaform(document.formenviar);">
Volver
</a>
But when I click on the button it doesn´t work. In the firefox console I receive this error:
Uncaught SyntaxError: missing ) after argument list
And in the chrome console:
Uncaught SyntaxError: Unexpected end of input
Inspecting the button element in Chrome:
<a class="btn-get-started i18n-back" type="button" onclick="
crearform('formenviar','post');
multiple_addfield(document.formenviar, {" controller":"portal","action":"_default"});="" enviaform(document.formenviar);"="">Volver</a>
Someone could help me?
Thanks!!