Uncaught ReferenceError: postRefreshPage is not defined

0

I have a problem calling the postRefreshPage () function on a button that means you should go back to the menu page and send the user's data so that the session stays started.

This is the button:

<button  class="btn btn-warning btn-sm" aria-label="Right Align" style="float: right;" onclick="postRefreshPage();">
<span class="glyphicon glyphicon-menu-hamburger" aria-hidden="true"></span>
&nbsp; Regresar al menu
</button>

And this is the function:

function postRefreshPage(){
        var theForm, newInput1, newInput2;
        // Se crea el <form>
        theForm = document.createElement('form');
        theForm.action = 'pruebas/checklogin.php';
        theForm.method = 'post';
        // Se crean los <input>s en el form y les damos nombre y valor
        newInput1 = document.createElement('input');
        newInput1.type = 'hidden';
        newInput1.name = 'username';
        newInput1.value = "<?php echo $_SESSION['user']?>";
        newInput2 = document.createElement('input');
        newInput2.type = 'hidden';
        newInput2.name = 'password';
        newInput2.value = "<?php echo $_SESSION['pass']?>";
        // Lo juntamos todo...
        theForm.appendChild(newInput1);
        theForm.appendChild(newInput2);
        // ...y ahora al DOM...
        document.getElementById('hidden_form_container').appendChild(theForm);
        // ...y lo mandamos
        theForm.submit();
    }

And he throws me this error:

Uncaught ReferenceError: postRefreshPage is not defined
at HTMLButtonElement.onclick

This page is on a server and it is where I get the error, but when I ran it in the local everything worked fine.

Thank you in advance for your answers

    
asked by Carlos Roberto Luna Ochoa 18.06.2018 в 15:54
source

1 answer

0

Instead of using the onclick attribute, you'd better use a% EventListener of JavaScript

For example;

function funcionEjemplo()
{
alert("Click");
}

document.getElementById("ejemplo").addEventListener("click", funcionEjemplo);
<button id="ejemplo">Soy un botón</button>

I leave you the documentation of w3schools of the addEventListener () since it would be interesting to have a look!

For your case, I leave you the jsfiddle of your particular case, but with the concept of the previous example I assume that it will be clear to you as fix it!

Greetings!

    
answered by 18.06.2018 в 17:19