Problem passing value to PHP variable with Ajax

Asked

Viewed 238 times

-1

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

In the project I am working on there is a javascript function in which a variable called codigo receives a certain value.

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

The problem is I can’t make that transfer with the values.

Here’s the part where the button is

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

Here is 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 is the part of php code where you will perform the action:

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

// executa o resto do código.

}

The problem is it’s not working. Can someone help me?

  • 1

    Where is defined codigo in JS? Is any warning displayed? Which? in PHP, what is the return of var_dump($_POST)?

  • "The problem is it’s not working"... this phrase is too generic and broad. Say what is not working?

  • @Andersoncarloswoss the variable codigo is set at the beginning in the js script. It is a global variable. Alert appears in success. What I find strange, appears the Success message but does not fall in the if of PHP

  • @dvd The above comment says what is happening

  • When and as the variable codigo is defined?

1 answer

0

The variable code was not set. Now she is taking the value of her input, which at the moment is Confirm Subscription. In your PHP code you will have something like:

echo $_POST['cod'];

This will print on the screen Confirm Subscription

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>
<input  type="submit" name="btnEnviar" onclick="ConfirmarAssinatura();" class="btn btn-primary btn-lg" value="Confirmar Assinatura" id="myinput"/>

  • The variable codigo is declared at the beginning of the JS code. It is a global variable. Ajax displays the Success Alert, but the PHP if does not work. That’s what’s confusing me.

  • That if is not necessary. Do the following, remove all the code from your PHP file and just make a print_r($_POST); and look what appears in your Alert. Note that I changed your ajax request by adding the return variable in the Success function. Update this in your code before you take the test above.

  • It shows nothing. It displays the Success message but nothing happens, it seems that it is not sending the values or calling the page placed on url

  • the.php screen-plans file is in the same directory where you are calling this function?

Browser other questions tagged

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