AJAX does not pass values to PHP

0

Code of my main page:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Salão de Beleza – Madame Nora</title>
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="classes/jquery-2.1.1.js" ></script>
<script type="text/javascript" src="classes/jquery-2.1.1.min.js"></script>

<script type="text/javascript"> 
    $(function(){
        $("#btnDesativar").click(function(event) {             
            var aux = confirm('Deseja Realmente Desativar Conta?');
            if(aux == true){                                
                 var acao = "1";
                  var url = "includes/conexaoIndex.php";
                  xmlHttp.onreadystatechange = stateChanged;
                  xmlHttp.open("POST",url,true);
                  xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
                  xmlHttp.send("acao="+acao);
            }
        });     
    });     
</script>

</head>
<?php include "/includes/conexaoIndex.php"; ?>
<body>
<form id="loginUsuario" name="loginUsuario" action="" >
 <button type="submit" id="btnDesativar" name="btnDesativar" >Desativar conta</button>
 </form>
 </body>
 </html>

And on my other page, I only have this to test:

<?php

if (isset($_POST['acao']))
{ 
echo "aqui";}

?>

In my browser it looks like this:

http://localhost/Projeto/teste.php?btnDesativar=

Why does not it work?

    
asked by anonymous 06.11.2014 / 15:31

4 answers

1

Friend you need to put &

xmlHttp.send("&acao=" + acao);
    
06.11.2014 / 16:13
1

You can use the $ .ajax function of jQuery and the serialize () method that already formats the form data for submission. Example:

HTML:

<form>
    <!-- Não esquecendo de colocar o atributo 'name', pois é o key value -->
    <input type="text" name="usuario" />
    <input type="password" name="senha" />
    <button type="button">Enviar</button>
</form>

jQuery:

$.ajax({
  type: 'post',
  url: 'arquivo.php',
  data: $('form').serialize(),
  success: function (data) { ... }
});

The serialize () method would form this style:

usuario=joao&senha=abcdefg
    
06.11.2014 / 17:23
0

Why not try the jQuery + AJAX native function? Here is a simple example:

$.ajax({
                url         : 'login.php' , //Caminho do Arquivo PHP
                dataType    : 'json'    , //Tipo de retorno
                data        : { //Enviar dados
                    usuario : 'douglasdreer' ,
                    senha   : 'admin'
                } , // Enviar alguma 
                success : function(result){     //Tratativa de sucesso               
                    //Neste exemplo, vai retornar um TRUE / FALSE;

                    if(result){
                        alert('Logado com sucesso!');
                    } else {
                        alert('Erro ao fazer login, verifique os parâmetros');
                    }
                } ,
                error : function(jqXHR, textStatus, errorThrown){ //Tratativa de Erro
                    console.error(errorThrown);
                }
            });

In PHP

$params = $_RESQUEST;
   // Verificações 
   $json = array('status' => TRUE);
   return json_encode($login);

You can check more details on the site below:

  

jQuery Site (ENG):    link

    
06.11.2014 / 17:07
0

Change the type of your button

<button type="submit" id="btnDesativar" name="btnDesativar" >Desativar conta</button>

for

<button type="button" id="btnDesativar" name="btnDesativar" >Desativar conta</button>

Using the type="submit" it sends the form data to the address given in the action="..." attribute inside the form tag (when you do not enter an address) same page. Using the type="button" it does not send anything, causing the click action to be implemented by you when you call $ ("# btnDisable"). Click ();

As you are using JQuery I recommend using ajax in this way using the $ .submit () and $ .ajax () method:

jQUery Submit

jQuery Ajax

$("#loginUsuario").submit(function(event) {
  var aux = confirm('Deseja Realmente Desativar Conta?');
  if (aux == true) {
    var url = "includes/conexaoIndex.php";
    $.ajax({
      type: "POST",
      url: url,
      data: {
        acao: "1"
      },
      success: function(res) {
        console.log(res); //resposta gerada pela pagina requisitada
      }

    });
  }
  event.preventDefault();
});
<form id="loginUsuario" name="loginUsuario" action="">
  <button type="submit" id="btnDesativar" name="btnDesativar">Desativar conta</button>
</form>
    
06.11.2014 / 16:49