访问获取数据并在同一页面上发布数据

I am fairly new to posting and getting and such like, but am trying to learn it and now I have this problem. I am using PHP to collect data from a text file, sending this via a URL with Ajax to Javascript. So this is done without page refresh (and works). But then on the same page, I am also posting form data with Javascript on the click of radio buttons (which also submits the form), to be accessible to me somehow (e.g. saved in a database or echoed to the page). Because the page refreshes on submitting the form, I'm thinking that it shouldn't be such a problem?

So far I have only tried to add the code below to post, but it's not working, because as soon as I add more PHP code to my document, the page crashes (becomes blank). I guess the echo part does not work because of a previous echo in the PHP code, but how do I solve it?

I can also add that the PHP code that I had before I tried adding this snippet below needs to be ended with a die for the page to function properly. So I have trouble knowing where and how to put any more PHP code.

The submitting of the form data seems to work though, because the page is refreshed on click of the radio buttons.

<?php
    if ( isset($_POST['image'])
    {
        $image = $_POST[ 'image' ]; // 'image' is the name of the radio buttons
        echo "$image<br>";
        //Will result in "image1" or "image2", etc - whatever "value" you assigned to the radio buttons
    }
?>

<script>
$(document).ready(function(){
    // Bind to change event for all radio buttons
    $('input[type="radio"]').on('change', function() {
        // Submit the form ("this" refers to the radio button)
        $(this).closest('form').submit();
    });
});
</script>

This is the PHP code that I want to add something to to access the posted form data.

<?php
    if ( isset($_GET['firstItem']) && isset($_GET['numItems']) )
    {
        $firstItem = $_GET['firstItem'];
        $numItems = $_GET['numItems'];

        $subtestNumber = 7;
        $filename = sprintf("subtests/%d.txt", $subtestNumber);

        $fileHandle = fopen($filename, "rt");

        for ($numLinesToSkip=0; $numLinesToSkip<$firstItem; $numLinesToSkip++)
            fgets($fileHandle);

        $results = array();
        for ($itemCount=0; $itemCount<$numItems; $itemCount++)
        {
            $curLine = fgets($fileHandle);
            $curLine = trim($curLine);
            array_push($results, $curLine);
        }
        fclose($fileHandle);

        echo json_encode($results);
        die;   // stop execution now - dont output the html below

    }
?>