Undefined index: action when sending ajax data to PHP

Asked

Viewed 567 times

0

i am having problems sending the parameters of my ajax to PHP, is giving me the error Undefined index: acao in my file log.php

my ajax takes the username and password of the html form and sends it to the log.php file, in this file I instate the querysDB class and call the login function and pass the parameters to it, and then send the $ok variable to my ajax.

Log file.php

 <?php
 include_once("querysDB.php");
 include_once("bancoDB.php");

 $querys=new querys();
 $acao=$_POST['acao'];

switch ($acao) {
case 'logando':
$usuario= $_POST['usuario'];
$senha= $_POST['senha'];
if(!empty($usuario) && $usuario!='' && !empty($senha) && $usuario!=''){
  echo json_encode($querys->logar($conexcao,$usuario,$senha));
}else{

}
break;

default:
break;
   }
?>

js login file.

var log = {

init: function(){

},

entrar: function() {
   $.ajax({
     type: "POST",
     url: "logar.php",

     data:{
       acao:'logando',
       usuario: $("#usuario").val(),
       senha:   $("#senha").val()
     },

     dataType: "json",

     success: function(json){
       if(json.result== true){
          alert(json.ok);
       }
     },
     error: function(){
       alert("Erro ao enviar dados");
     }
   });

  }
}

querysDB.php file

class querys{

function logar($conexcao,$usuario,$senha){
try {
       $query=mysqli_query($conexcao,"SELECT Nome,Senha FROM usuarios");
       $dados=$query;
       $arrayResultado=array();

while($resultado=mysqli_fetch_array($dados)){

     $arrayResultado['nome']=$resultado['Nome'];
     $arrayResultado['Senha']=$resultado['Senha'];

}
 $ok='';
 for($i=0; $i<sizeof($arrayResultado); $i++){

    if($arrayResultado[$i]==$usuario && $arrayResultado[$i]==$senha){

       $ok=true;
    }else{

      $ok=false;
    }
  }
  $dadosjson['result']=true;

  $dadosjson['ok']=$ok;

  return $dadosjson;

   } catch (Exception $e) {
        echo $e;

   }
  }
}

2 answers

0

The mistake Undefined index of course. The "action" index is not defined.

Check that the $_POST['action'] variable was actually passed to ajax.

  • So my problem is in the js file, because I deleted it, but even so when I open the page and go to the console it is still there.

0


Do two tests, the first:

data:{
   acao: "logando", // nota que eu mudei para aspas duplas
   usuario: $("#usuario").val(),
   senha:   $("#senha").val()
 }

And the second:

data:{
   "acao": "logando", // quando uso JSON, sempre aspas duplas
   "usuario": $("#usuario").val(),
   "senha":   $("#senha").val()
 }
  • opa blz Thiago, I’ve been looking and my js file is not updating, I mean, I make the changes in my saved file you are changes, but then I open the console of my browser and open my.js file and see that they are not with the modifications I made. My files are all in the same directory. I don’t know what can be

  • It worked Thiago, that’s right I put double quotes and it worked.

Browser other questions tagged

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