It's not possible, as I explained in a similar post:
PHP is usually server-side and in the case of websites, it is always server-side, ie PHP generates the HTML sends as an internet response back to you and then your browser renders the HTML response on the screen, or whether the PHP function has already been processed:
TosummarizeallwebtechnologywillworkundertheHTTPprotocol,whichhasrequisitionsandanswers.
Workaround
TheworkaroundistocreateaPOSTcallwithifaftertheformissubmitted,eg
<formaction="paginadestino.php" name="myForm" id="myForm" method="POST">
<input type="text" name="edtMFIR" id="edtMFIR" value="" class="mws-textinput error">
</form>
And in the paginestino.php (which can be a .php or just a dynamic page with querystring ?foo=baz
) add the IF:
<?php
if (!empty($_POST['edtMFIR'])) {
myFunction();
}
If you want not to switch pages just by using Ajax (which changes the whole approach), for example:
<form action="paginadestino.php" name="myForm" id="myForm" method="POST">
<input type="text" name="edtMFIR" id="edtMFIR" value="" class="mws-textinput error">
</form>
<script>
(function () {
var form = document.getElementById("myForm");
form.addEventListener("submit", function (e) {
e.preventDefault();
var oReq = new XMLHttpRequest();
oReq.open(this.method, this.url, true);
oReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
//Função assíncrona que aguarda a resposta
oReq.onreadystatechange = function()
{
if (oReq.readyState === 4) {
if (oReq.status => 200 && oReq < 300) {
alert(oReq.responseText); //Pega a resposta do servidor
} else {
alert(oReq.status); //Pega o código de erro da resposta do lado do servidor
}
}
};
oReq.send(new FormData(this)); //Envia os campos do formulário
});
})();
</script>
And in PHP it will also have to be:
<?php
if (!empty($_POST['edtMFIR'])) {
myFunction();
}
Then within the "IFs" you can do whatever you want with JavaScript, just adjust :
if (oReq.status => 200 && oReq < 300) {
//Faz algo com JavaScript se a resposta HTTP for "OK"
} else {
//Faz algo com JavaScript se tiver ocorrido algum erro
}