How to get JQuery value and pass to PHP [duplicate]

2

I know the question may have already been asked, but in no case has it solved the problem I have, I need to get the value of a JQuery variable and pass it to one in PHP, the variable in JQuery is being created like this:

// Gerando números para cada ciclo do processo  
if( $("#LinkDocumento").val() != "" && $("#Etapa").val() == 1)  {
    var Numero = Math.floor(Math.random() * 11);                
} else if ( $("#LinkDocumento").val() != "" && $("#Etapa").val() == 2) {
    var Numero = Math.floor(Math.random() * 11);
} else {
    var Numero = Math.floor(Math.random() * 11);
}

And I tried to play the value in a PHP variable like this:

 $EtapaForm = "<script>document.write(Numero)</script>";

But I'm not getting it, the result looks like this:

The variable I am trying to retrieve will be passed as a parameter to another form that is being fetched through the variable #LinkDocumento , that is, this variable brings me a path from the bank.

    
asked by anonymous 12.05.2015 / 18:15

1 answer

2

One issue would be to switch to php via ajax.

Try this:

function passarVariavelJQueryParaPHP(valor){ 
    $.ajax({
        type      : 'post', 
        url       : 'teste.php', 
        data      : 'variavel ='+ valor, 
        dataType  : 'html', 
        error: function(xhr) {
            $("div#error").html('Erro ao passar variavel: ' + xhr.responseText);
        }
      }); 
});

And in the test.php file do:

$variavel = $_POST['variavel'];
    
12.05.2015 / 18:54