1

I have a front-page.php template which lists the 5 latest posts along with some custom post types in between. When one clicks on the more button at the bottom it needs to ajax load more posts.

So I created a loop-home.php file according to this tutorial I couldn't use this code by default because I have a nested loop on the first load of homepage and this script messes it up.

So I wrote a new query to ajax OLDER posts only. I collected the post ids of the posts already present when the page loads and stored it in an array.

I now need to use this array in the query of loop-home.php I am using this method because offset does not work with pagination and I need to pass these IDs to post__not_in parameter in the query.

Method 1: I tried (in loop-home.php)

$exempt = $_REQUEST['exemptArray'];

But it returns NULL.

Method 2:(Using Ajax)

I tried (in front-page.php)-

<script>
var exemptArray = '<?php echo json_encode($exemptions); ?>';
</script>

Then I went to script.js and added- //create json object from the var array

var jsonArray = JSON.stringify(exemptArray);

var dataToPost = { 'jsonArray':jsonArray };

//send POST data to PHP and handle response
$.ajax({
  type: 'POST',
  url: loopHome, //stored path in variable. working.
  data: dataToPost,
  success: function (data) {

    console.log('I have already sent it.'); //I am getting this log. Working.

  }
});

Then in loop-home.php-

$exempt = json_decode($_POST['jsonArray']);

Still getting NULL on var_dump($exempt)

What am I doing wrong?

11
  • If you run loop-home.php seperate it will pop null cause you are not posting any data to it. Apr 18, 2014 at 9:56
  • @Tyranicangel I don't understand what you are saying... Apr 18, 2014 at 10:03
  • The javascript posts to the loop-home.php.So try console.log(data) and give a var_dump($exempt) in loop-home.php. Apr 18, 2014 at 10:05
  • While collecting those values instead of saving into variable, why dont you try to save into the sessions which is very easy to access for later purposes.
    – krishna
    Apr 18, 2014 at 10:24
  • Have you tried globalising the array? global $array Apr 18, 2014 at 10:34

1 Answer 1

0

Try this

You JS:

$.ajax({
    type:'POST',
    url:'foo.php',
    data:{myArr:exemptArray},
    dataType:'JSON',
    success:function(result){
        console.log(result);
    },
    error:function(data){
        console.log(JSON.stringify(result));
    }
});

Your php page:

$input=$_POST['myArr'];
$exempt=json_encode($input);

In your console you will get an array. [object object] is a reference to an associative array.So in the above code result is same as exemptArray.Try something like this you will be able to.I could not find your array composition anywhere so i used exemtarray itself.

8
  • I think we are getting really close- Here's what I did and what I am getting- pastebin.com/y1UCjB8v So POST is not happening? Apr 20, 2014 at 2:05
  • I tried manually entering the post ids in the query array just to see the var dump of $exempt=json_encode($input); I am getting string(4) "null" Does that make any sense to you...? Apr 20, 2014 at 2:09
  • If it helps my array is [81,69,42,34] Not an associative array right? Apr 20, 2014 at 2:13
  • You can send and receive any array.It is not just limited to associative.I have added error function into ajax.If dataType is JSON you should not get a string in console.log. Apr 20, 2014 at 4:56
  • 1
    First thing never generate the total html code on the server end.Use your php page to just query the database and send response back in form of JSON objects.Use your front end javascript/jquery or whatever to display the data onto your html.And in your case I would strongly suggest using "Angularjs" for front end.If you dont want to use angular just your your existing jquery.In your ajax you did post post the exempt array to the php page.Only the get variables are assigned.So the server is responding with the same data twice. Apr 20, 2014 at 16:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.