Ajax changes the php variable

advertisements

I've got this variable $type and I want it to be month or year. It should be changed by pressing a div.

I've tried creating an onclick event with an ajax call.

The ajax call and the variable are in the same script (index.php)

Inside the onclick function:

var curr_class = $(this).attr('class');

$.ajax({
    type: "POST",
    url: "index.php",
    data: {
        type: curr_class
    },
    dataType: 'text',
    success: function(data) {
        // Test what is returned from the server
        alert(data);
    }
});

But the alert returns the whole html page. When I console.log the data (create a var data = { type:curr_class }) and console.log *that data* it returnstype = month` (which is correct)

while I just want it to return month or year

So on top of the page I can call

if(empty($_POST['type'])){
     $type = 'month';
} else {
     $type = $_POST['type'];
}

and change the PHP variable so I can use it in the rest of my script.

But how can I accomplish this?

With kind regards,


as you are sending request to the same page so as a result full page is return .You will have to send it to another page and from that page return the type variable

if(empty($_POST['type'])){
 $type = 'month';
 } else {
   $type = $_POST['type'];
   echo $type;

keep this code in separate file and make an ajax call to that page