0

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>
react_or_angluar
  • 1,568
  • 2
  • 13
  • 20
  • When the page first loads `$_POST` contains no data, as the page is loaded via GET. Therefore trying to access a POST value which doesn't exist results in the error you see. Do a check to see if there is anything within `$_POST` first – Rory McCrossan Oct 13 '20 at 16:17
  • @RoryMcCrossan I have tried that already. That returns no result. – Sajedul Noman Oct 13 '20 at 16:42

0 Answers0