PHP function in input text action

Asked

Viewed 797 times

4

It is possible to call a php function inside 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>    
  • 2

    It is not possible. PHP is a server-side language, what you are trying to do you will get with Javascript for example, which acts on the client side, ie in the view/page that is presented to the user.

1 answer

4

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, or PHP generates HTML sends as a response via internet back to you and then your browser renders the HTML response on the screen, ie the PHP function has already been processed:

HTTP

To summarize all web technology will work under the HTTP protocol, which has requests and responses.

Workaround

The alternative solution is to create a POST call with if after the form is sent, example:

<form action="paginadestino.php" name="myForm" id="myForm" method="POST">
    <input type="text" name="edtMFIR" id="edtMFIR" value="" class="mws-textinput error">
</form>

And in paginadestino.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 wish to don’t change pages only 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();
}

So 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
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.