Problem passing value to PHP variable with Ajax

-1

I know it must be a frequent problem and everything, but I searched in several places and could not fix my problem:

In the project I'm working on, there's a javascript function in which a variable called codigo gets a certain value.

So when a button is clicked, the value of the codigo variable must be passed to a PHP variable for insertion into the database.

The problem is that I can not do this transfer with the values.

Here is the part where the button is

<input  type="submit" name="btnEnviar" onclick="ConfirmarAssinatura();" class="btn btn-primary btn-lg" value="Confirmar Assinatura"/>

Here's the part in Javascript:

    function ConfirmarAssinatura(){


    $.ajax({

      url:'tela-planos.php',
      type:"POST",
      cache: false,
      data: { 'cod': codigo },
      success: function (){
        alert (codigo)
      },
      erro: function(result){
        alert('errou')
      }

    });


  }

And here's the part of the php code where you'll do the action:

if (isset($_REQUEST['cod'])){

// executa o resto do código.

}

The problem is that it is not working. Can anyone help me?

    
asked by anonymous 12.01.2018 / 00:15

1 answer

0

The variable code was not defined. Now she is getting the value of her input, which is Confirm Signature right now. In your PHP code you will have something like:

echo $_POST['cod'];

This will print on the Confirm Signature

function ConfirmarAssinatura(){
    var codigo = $('#myinput').val();
    console.log(codigo);
    $.ajax({
        url:'tela-planos.php',
        type:"POST",
        cache: false,
        data: { 'cod': codigo },
        success: function (data){
            alert (data)
        },
        erro: function(result){
            alert('errou')
        }
    });
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><inputtype="submit" name="btnEnviar" onclick="ConfirmarAssinatura();" class="btn btn-primary btn-lg" value="Confirmar Assinatura" id="myinput"/>
    
12.01.2018 / 00:36