-1

I need an array of arrays. I have a ProcessWire page with pages and subpages and fields on those pages and subpages. I packed this information in an array of arrays like so…

<?php

    $allarticles = $pages->find("template=article"); // find by template of the last level
    $catalogue = array();
    $i = 0;

    foreach ($allarticles as $onearticle) {
        $temp = array ( // create a temporary array for the lowest level
            'id' => $onearticle->id,
            'title' => $onearticle->title,
            'coverimage' => $onearticle->cover->url,
            'url' => $onearticle->url,
            'author' => $onearticle->author,
            'date' => $onearticle->date,
            'year' => $onearticle->parent->parent->title,
            'issue' => $onearticle->parent->title,
            'offline' => $onearticle->offline,
            'body' => $onearticle->body
        );
        $catalogue[$i] = $temp; // add the results of each foreach (i.e. an array) to the main array at position $i 
        $i++; // go to the next position of the main array 

    };

    print_r ($catalogue);

?>

This seems to kind of work.

The code is in test.php which is in the root folder.

Now I'm trying to request this php-file via AJAX.

<script type="text/javascript">

    var request = new XMLHttpRequest();
    if (request) {
        request.onreadystatechange = ReloadRequest;
        request.open("GET", "test.php", true);
        request.send(null);
    } 

    function ReloadRequest() {
        request.readyState;
        var str = request.responseText;
        document.getElementById("boxedcontent").innerHTML = (str);
    }

</script>

This script is at the bottom of the template file that is applied to a specific page.

The js-code works when it requests an html file but it doesn't work with a php file. I receive an "(Internal Server Error) the server responded with a status of 500 ".

Thanks for help!

  • Start by getting PHP to pass the data as JSON. Javascript does not understand the output from a `PHP print_r()` – RiggsFolly May 12 '20 at 13:29
  • Is that ALL the PHP file? I ask because you dont instantiate `pages` anywhere – RiggsFolly May 12 '20 at 13:31
  • the 500 might be related with your server or the processwire, I am trying to answer testing your code, it works fine. – Adi Prasetyo May 12 '20 at 13:51
  • the CMS-framework prepends and appends php code. Also, all the pages are basically objects which you can return with the API. So `$pages` is pre-defined and so are the methods `->parent` and `->find("selector")` and more. – user2537250 May 12 '20 at 13:54
  • check or include your server log? – Adi Prasetyo May 12 '20 at 14:07
  • server log is always **call to a member function find() on null in path/to/test.php**. I tried to use different methods, still gives me a similar error. – user2537250 May 12 '20 at 16:12

1 Answers1

0

First in your php file convert print_r($catalouge) to echo json_encode($catalouge); so you php script will return understandable format to javascript.

Second replace your javascript code with the following:

fetch('test.php')
.then( response=>response.text())
.then(data=> {
    document.getElementById("boxedcontent").innerHTML = data;

 });

Fetch api is very easy and powerfull javascript tool to make ajax requests.

Hardood
  • 503
  • 1
  • 4
  • 14