RonnyRaap
RonnyRaap

Reputation: 9

Ajax post not received by php

I have written a simple code. In order to avoid flooding a JSON server, i want to break up the JSON response in pieces. So my jquery code should be parsing one variable ("page") to the php page that handles the JSON Oauth Request. On success, it should append the DIV with the latest responses.

My code should be working, except for the fact that my ajax post is not being received by my php file.

Here goes

archief.html

$("#klik").click(function() {
console.log("fire away");
page = page + 1;
$("#archief").load("trytocombinenewageandgettagsendates.php");
 console.log(page);              
                $.ajax({
                    type: 'POST',
                    url: "trytocombinenewageandgettagsendates.php",
                   data:  page,
                    success: function() {

                            console.log(page);

                          $.get("trytocombinenewageandgettagsendates.php", function(archief) {
                                $('#archief').append(archief);
                            });

                    },
                    error: function(err) {
                            alert(err.responseText);
                    } 
                });
                return false;
       });

The php file doesn't receive anything.

var_dump($_POST);

gives me array(0) { }.

Very strange, i'd really appreciate the help!

Upvotes: 0

Views: 92

Answers (2)

RonnyRaap
RonnyRaap

Reputation: 9

Thank you for your help and not letting me post "this still doens't work" posts :)

I made the mistake of loading the "unConsulted" php file [$.get("trytocombinenewageandgettagsendates.php"] upon success. Instead, i append the response of the PHP.

The working code below:

$("#klik").click(function() {
console.log("fire away");
page = page + 1;

//$("#archief").load("trytocombinenewageandgettagsendates.php");
 console.log(page);              
                $.ajax({
                    type: 'POST',
                    url: "trytocombinenewageandgettagsendates.php",
                   data:  { 'page': page },

                    success: function(response){

                    $("#archief").append(response);  

                    },

                    error: function(err) {
                            alert(err.responseText);
                    } 
                });
                return false;

Upvotes: 0

jeroen
jeroen

Reputation: 91734

You are sending a string instead of key-value pairs. If you want to use $_POST you need to send key-value pairs:

...
$.ajax({
  type: 'POST',
  url: "trytocombinenewageandgettagsendates.php",
  data:  { 'page': page },
  success: function() {
     ...

If you send a single value or string, you would need to read the raw input.

Also, you are sending 2 GET requests and 1 POST request to the same file. Is that intentional? Note that only the POST request will have the $_POST variable set.

Upvotes: 3

Related Questions