jpenna
jpenna

Reputation: 9161

Send POST request via AJAX to same page and get value using $_POST

This is puzzling me and I can't find anything telling what is wrong...

I'm sending a value to a function, and using AJAX to send this value through a POST request to the same page. Then, on success, I'm loading a .php file in this page and I want to retrieve the posted value using $_POST['campId'].

function post(value) {

   $.ajax({
       type: 'POST',
       data: {'campId': value},
       success: function () {
           navigateTo.push.newCampaign();
       }
   });
}

$.each(jsonObj, function (key, value) {
    $(value).on('click', function () {
         post(value);
    })
});

Then on newCampaign.php (the page I'm loading), I'm testing if the POST is working:

console.log(<?php print($_POST['campId']); ?>); //This don't show anything
<?php var_dump($_POST); ?> // and this brings an empty array: "array(0) {}"

What am I doing wrong? Why can't I get the posted value?

Upvotes: 0

Views: 1001

Answers (1)

Naresh Kumar
Naresh Kumar

Reputation: 561

As you have mentioned that you are posting the value on the same page. Now if you want to get post values on any other page, then post the values on that respective page For eg. Add this attribute in ajax code url: "newCampaign.php" and then test for the post values on newCampaign page

Upvotes: 1

Related Questions