Ajax Undefined index

Asked

Viewed 331 times

-5

Hello, could someone please help me? I have been to several websites trying to solve this problem.

The js code works perfectly, and the $("#links").load( "buscas.php" ); loads, but the PHP code always gives the error:

Notice: Undefined index: buscas in.

In my opinion js is not able to send the data to the.php searches, but the reason, I have no idea.

I don’t know what else to do, I even shortened the code and I left this fool down here and I still got the same mistake.

Js Code

$(document).ready(function(){

    $("button").click(function(){

        botao = $(this).html();

        $.ajax({
            type:"POST",
            dataType:"HTML",
            url:"buscas.php",
            data: {tipo: botao},
            success: function(data){
            $("#links").load( "buscas.php" );

            }
        }); 

    });

});

PHP code (searches.php)

if(isset($_POST['tipo'])){

    $tipo = $_POST['tipo'];

    echo "Botao ".$tipo." precionado";

}
else{
    echo "Variavel nao definida";
}

<!DOCTYPE html> 
<html> 
  <head> 
   <script src="js/jquery-3.0.0.min.js">

   </script> <script src="js/busca.js"> </script> 

   <title>Links</title> 

  </head> 

   <body>
 <button id='botao'>botao1</button><br>
 <button id='botao'>botao2</button><br>

      <div id="linksPainel">

         <div id="links"> 

         </div> 

      </div>

   </body> 

</html>

@Williamnovak That was the only code I didn’t show. Sorry, I thought it wouldn’t matter.


<?php 
    require "dbPDO.php";

    if(isset($_POST['tipo'])){

        $tipo = $_POST['tipo'];

        $busca = $conect->prepare("SELECT * FROM link WHERE tipo = '".$tipo."' ORDER BY id DESC ");
        $busca->execute();

        echo "<h2>$tipo</h2>";
        while ($row = $busca->fetch(PDO::FETCH_ASSOC)) {
            # code...
            $nome = $row['nome'];
            $link = $row['link'];
            echo "<a href='$link' target='_blank'>$nome</a><br>";

        }
    }else{
        echo "Variavel nao definida";
    }

?>

There you go!

  • Put the rest of the code, why Notice: Undefined index: buscas in. means that you are at some point calling a variable or index in a array that does not exist and this is interfering with the outcome that expects.

  • This is all the code, the Notice: Undefined index: buscas in acontece se eu remover o if(isset($_POST['type'])) que verifies if a variable was defined. If I leave, it enters the Else "echo "Variable not defined";" Ie the variable is not really set sedo, but I do not find the problem. I left this simple code to try to find the problem, but I couldn’t.

1 answer

1

Your javascript is doing 2 posts. Is that right? Not to mention that in the second he is not sending anything to buscas.php (where the error must occur). If the idea is to receive the result of buscas.php according to the received parameters, try something like

$.post("buscas.php", {tipo: botao}, function(data){
    //Resultado após o post
});

If you want to use GET:

$.get("buscas.php", {tipo: botao}, function(data){
    //Resultado após o get
});

If you want to use the load method:

$("#links").load( "buscas.php", {tipo: botao} );

Browser other questions tagged

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