getting undefined index in php when sending data from ajax

28 Views Asked by At

I need to send ajax data to the PHP POST method, but I get undefined index "name". I have tried including dataType: json in ajax and json_encode().

In console, I am getting the whole HTML code when logging success: function(data){}.

<!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
    <?php 
        echo $_POST["name"];   
    ?>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script type="text/javascript">
    var name="KO";
    $.ajax({
        type:"POST",
        url: 'ajax.php',   
        data:{ "name": name},    
        success: function(data){
        console.log(data);
        }     
    });
</script>
</body>
</html>
0

There are 0 best solutions below