How to take Jquery value and pass to PHP

Asked

Viewed 5,101 times

3

I know the question may have already been asked, but in no case solved the problem I have, I need to take the value of a Jquery variable and pass 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 variable PHP thus:

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

But I’m not getting it, the result is like this:

inserir a descrição da imagem aqui

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

  • 3

    Suggestion for reading: http://answall.com/a/43739/129

  • You can make a simple window.location.href = "url.php?EtapaForm=" + Numero. But the recommended is to use ajax.

1 answer

3


A mess would be to pass 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 it:

$variavel = $_POST['variavel'];

Browser other questions tagged

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