user1477731
user1477731

Reputation: 21

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"];
?>

Upvotes: 0

Views: 1822

Answers (3)

Adarsh Raj
Adarsh Raj

Reputation: 301

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

Check the URL path.

Upvotes: 0

The Real Coder
The Real Coder

Reputation: 138

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.

Upvotes: 1

Sudhir Bastakoti
Sudhir Bastakoti

Reputation: 100175

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"}

Upvotes: 0

Related Questions