PHP function in the action of an input text

0

Is it possible to call a php function within the action parameter of my form?

    <!--FILTRO FLUTUANTE -->
<div id="mws-themer">
    <div id="mws-themer-hide"></div>
    <div id="mws-themer-content">
        <div class="mws-themer-section">
            <form action="<?php myFunction() ?>"  name="myForm" id="myForm" style="padding: 0; margin: 0;" method="POST">
                <input type="text" name="edtMFIR" id="edtMFIR" value="" class="mws-textinput error">
            </form>
        </div>
        <div class="mws-themer-separator"></div>
        <div class="mws-themer-section">
            <button type="button" onclick="submit()" class="mws-button red small" id="mws-themer-sendfilterPCD">Filtrar</button>
        </div>
    </div>
</div>    
    
asked by anonymous 20.09.2017 / 22:14

1 answer

0

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
}
    
20.09.2017 / 22:51