简体   繁体   中英

php not receiving ajax data

Here's my java script code

    $(document).ready(function () {
var getOption = $("input:radio[name='profit']");
getOption.click(function(){
if (this.value == 'amount') {
    $('.graph_per').hide();
    $('.graph_amt').show(); 
    }
else if(this.value == 'percentage') {
    $('.graph_amt').hide();
    $('.graph_per').show(); 
    }           
    });
    var get="";
$.ajax({  
    type: 'POST',  
    url: 'localhost/testp/admin.php', 
    data: {get:"amount"},
    success: function( response )    {
       console.log( response );
    }
 });
});

when i post the get variable in php it shows the error : undefined index 'get'. how can fix it & my this js file is stored in different folder . the php file

<?php
echo $_POST["get"];
?>

Following is the tested correct code:-

$.ajax({  
    type: 'POST',  
    url: 'admin.php', 
    data: { get: "amount" },
    success: function( response ) {
        console.log( response );
    }
});

Problem is the path of the file "admin.php". If "admin.php" and the JS file are in the same folder, then above code is fine. If admin.php is outside the folder in which your js file is, then change "admin.php" to "../admin.php". Here "../" is for one directory level back of the current folder. Change it accordingly if "admin.php" is two or three level back of the folder, in which your JS file is.

try:

$.ajax({  
    type: 'POST',  
    url: 'admin.php', 
    data: { "get": "amount" },
    success: function( response ) {
       console.log( response );
    }
 });

PHP

echo $_POST["get"];

From the code you added as comment, i see that you are doing:

..
 data: {get="amount"}
..

so change that to

 data: {"get" : "amount"}
$.ajax({  
    type: 'POST',  
    url: 'localhost/testp/admin.php', 
    data: { get: "amount" },
    success: function( response ) {
        console.log( response );
    }
});

Check the URL path.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM