Run PHP function with onclick()

Asked

Viewed 167 times

-1

Hello, I have a function that should be executed only by clicking the filter button, but the page already opens with the search, with the function executed. Onclick is already on the filter button.

Here is the function:

function LoadFiltro(buscanova){

    var nr_cliente              = $('select[name=nr_cliente]').val();
    var nr_gc1                  = $('select[name=nr_gc1]').val();
    var nr_status               = $('select[name=nr_status]').val();
    var nr_empresa              = $("select[name='nr_empresa']").val();
    var nr_potencial_fechamento = $("select[name='nr_potencial_fechamento']").val();
    var dt_previsao_fechamento1 = $("input[name='dt_fechamento_ini']").val();
    var dt_previsao_fechamento2 = $("input[name='dt_fechamento_fim']").val();

   $.post('../App_Cad/ControlPreVendas.php', 
        {action: 'load', 
        type: 'filtro_controle', 
        nr_cliente: nr_cliente, 
        nr_gc1: nr_gc1, 
        nr_status: nr_status, 
        nr_empresa: nr_empresa, 
        nr_potencial_fechamento: nr_potencial_fechamento,
        dt_previsao_fechamento1: dt_previsao_fechamento1, 
        dt_previsao_fechamento2: dt_previsao_fechamento2,
        buscanova: buscanova},
            function(data){
                $('#resp_filtro').html(data);
            }
   );   

And here’s the button:

<input type="button" value="Filtrar" class="botao" onClick="LoadFiltro(true);"/>

I’m opening my body like this:

<body onLoad="LoadFiltro(false);">

2 answers

0

Good afternoon Felipe!

Maybe I’m not the most recommended person to answer your question, but I think it’s impossible to make javascript run function in php directly... I hope someone can tell if I’m wrong or right. Try passing a variable like Submit/post to the same page.

Example:

index php.

<?
$execute = $_POST["execute"];

function teste() {
  ...
}

if (!empty($execute)) {
  teste();
}

<form method="POST" action="index.php">
  <input hidden type="text" name="execute" value="1">
  <input type="submit" value="Testar">
</form>

?>

-1


I managed to solve and get the result I wanted.

What was done to work was: I removed the onload="Loadfiltro(false) attribute from the body and it worked.

Thank you very much.

Browser other questions tagged

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