Calling method in the action of a form that invokes which page should be redirected

Asked

Viewed 144 times

1

When entering a value in the input text, I should consult the bank to check if it exists. If so, I should redirect to formDetails.php. If not, I should redirect to formRegister.php.

I’ve been working on it for a few hours and I can’t find a solution. HTML and Javascript are in the same file.

<!--FILTRO FLUTUANTE -->
    <div id="mws-themer">
        <div id="mws-themer-hide"></div>
        <div id="mws-themer-content">
            <div class="mws-themer-section">
                <form action="" 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>    

    <script>

        function submit() {

            var fornDetails = "fornDetails.php";
            var fornRegister = "fornRegister.php";

            var result = "<?php paginaDestino($_POST["edtMFIR"]); ?>";
            alert(result);

            if (result) {
                myForm.action = fornRegister;

            } else {
                myForm.action = fornDetails;
            }
            myForm.submit();

        }
    </script>

Function where I do the bank check:

function paginaDestino($edtMFIR){

$conexao = new bancodedados();
if(!empty($edtMFIR)){
    $conexao->setSQL("SELECT * FROM tab_aro_pcd_riscos WHERE aro_riscos_mfir=".$edtMFIR." LIMIT 1");
$res_a = $conexao->Consultar();
    $RES_MFIR = mysql_fetch_array($res_a);

    if(empty($RES_MFIR)){
        return false;
    }else{
        return true;
    }
}

}

Can someone help me ?

2 answers

0


I fixed the various errors I found, probably work that way.

<?php 

function paginaDestino($edtMFIR){

$conexao = new bancodedados();
if(!empty($edtMFIR)){
    $conexao->setSQL("SELECT * FROM tab_aro_pcd_riscos WHERE aro_riscos_mfir=".$edtMFIR." LIMIT 1");
$res_a = $conexao->Consultar();
    $RES_MFIR = mysql_fetch_array($res_a);

    if(empty($RES_MFIR)){
        header('Location: fornRegister.php');//não seria form?
    }else{
        header('Location: fornDetails.php');
    }
}

}

if(isset($_POST['edtMFIR'])){paginaDestino($_POST['edtMFIR'])}

?>

<!--FILTRO FLUTUANTE -->
    <div id="mws-themer">
        <div id="mws-themer-hide"></div>
        <div id="mws-themer-content">
            <div class="mws-themer-section">
                <form action="" 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>    

    <script>

        function submit() {

            var myForm = document.getElementById('myForm');

            myForm.submit();

        }
    </script>
  • Man, you saved my life. Could you explain to me what the hell you were really up to ?

  • Kk ai yes, you were going into a looping, checking the php variable in javascript without it being set, in the example above I let php do all the work.

0

I did this recently using Jquery and Ajax, the function that Voce looks for is . keyup my example :

// Essa função verifica se o email ja existe e desabilita o botão 
function verif_email(email,nome) {
    var html = '<br>Você é o : \"' + nome + '\" ? <a href="#">Sim</a> / <a href="#">Não</a>';
    //essa condição é para caso a pagina ja carregue com um email no campo
    $('#email').ready(function() {
        if ($('#email').val() == email){
            $('#btn1').prop('disabled', true);
            $('#btn1').html("Email ja cadastrado");
            $('#texto').html(html);
        }else{
            $('#texto').hide();
        }
    });

    //essa condição é para ele checar cada vez que digitar
    $('#email').keyup(function() {
        if ($('#email').val() == email){
            $('#btn1').prop('disabled', true);
            $('#btn1').html("Email ja cadastrado");
            $('#texto').show();
            $('#texto').html(html);
        }else{
            $('#btn1').show();
            $('#btn1').html("Salvar");
            $('#btn1').prop('disabled', false);
            $('#alert').hide();
            $('#texto').hide();
        }
    });
}
function load_json() {
$.ajax({
    url : "json.json",
    success : function(result) {
        verif_email(result["email"],result["nome"]);
        setTimeout(function(){load_json();}, 100);
    }
});

}

hope I’ve helped.

Browser other questions tagged

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