Search code examples
phparraysassociative-arrayaccess-token

How to access data from API response


I have received the following response from an API.

[{
   "BookId":"32c03594-1ecb-4f97-8453-5b28a03d26d9",
   "BookName":"Book1",
   "Bookstatus":3,
   "Country":"AU"
},
{
   "BookId":"51d16696-b98a-4b3b-ac67-f36559cff70b",
   "BookName":"Book2",
   "Bookstatus":3,
   "Country":"AU"
},
{
   "BookId":"7b557a75-bf5e-4c29-9f31-43a7fee77520",
   "BookName":"Book3",
   "Bookstatus":3,
   "Country":"AU"
},
{
   "BookId":"c945ade5-d540-4378-9979-3842a1da396b",
   "BookName":"Book4",
   "Bookstatus":3,
   "Country":"AU"
},
{
   "BookId":"814869e2-e5af-48bc-a6da-28f272366955",
   "BookName":"Book5",
   "Bookstatus":3,
   "Country":"AU"
}]

I have a webform with drop down box to choose the book you want.

What I want to happen, is when you click submit you are sent to another page, and on that page I want to be able to access the BookId, BookName and access token from the first page via $_POST. I have no trouble with the BookName but can't work out how to get the BookId and access token to go with it.

This is what I have so far:

Please note: the access token was obtained with $_GET in the head of the page and assigned to $access_token variable.

<body>
<?php
    $array=json_decode($response, true);
    $arr_len = count($array);//length of the array = 5
    $for_len = $arr_len - 1;//length - 1
?>
<h1>CashBooks</h1>
<form action="getcoa.php" method="post">
    <select name="books">
        <?php
            for($i=0; $i<=$for_len; $i++){
                $bookname = $array[$i]["BookName"];
                echo '<option value="' . $bookname . '" name="' . $bookname . '">' . 
                $bookname . '</option>';
            };
        ?>
    </select>
    <input type="hidden" name="<?php $bookname; ?>">
    <input type="hidden" name="<?php $access_token; ?>">
    <input type="submit" value="Get Chart of Accounts">
</form>
</body>

With the BookId, I haven't figured out how to deal with that yet, so any suggestions would be great. And what I'm trying with the access token doesn't work. When I click submit with things how they are, I get this error on the "getcoa.php page":

"Notice: Undefined index: access_token in C:\wamp\www\getcoa.php on line 6"

This is line 6:

$access_token = $_POST['access_token'];

FYI. The token part is solved.

But I dont think I have been clear on what I'm trying to accheive with BookId. On this page, the only thing the "USER" will see is a drop down menu with a list of BookNames, from which they select ONE, and click a submit button... On the next page, I will make a call to an API endpoint, but I need the BookId to call on that endpoint.

So the BookId for the selected BookName is needed to be somehow posted to the next page, so I can use $_POST on that page to get the BookId and use it to access the endpoint desired, without the end user knowing "BookId" exists in anyway.

Hopefully I have been more clear on what I'm trying to achieve. Sorry if I wasn't to start with.


Solution

  • If I have understood correctly then you are trying to send various parameters to a PHP script for processing depending upon which item from a dropdown menu is selected? Whilst there are a finite number of attributes an option tag can take you can of course use a dataset attribute - or, as in the code below, several.

    So, with the help of some simple javascript, the hidden fields are populated when the select menu changes - the values stored in the hidden fields will be the ones sent to the PHP script when the form is submitted.

    Previously the hidden fields had their names derived from the contents of the original source data ( the values ) - this would make processing at the server tricky as you would not necessarily know the names to use in the PHP logic tests etc - hence they are assigned names that correspond to the keys from the source data - that way you know to process fields called bookid,bookname & access-token

    <?php
    
    
        /* for test porpoises only */
        $access_token=uniqid();
    
    
        $response='[{
               "BookId":"32c03594-1ecb-4f97-8453-5b28a03d26d9",
               "BookName":"Book1",
               "Bookstatus":3,
               "Country":"AU"
            },
            {
               "BookId":"51d16696-b98a-4b3b-ac67-f36559cff70b",
               "BookName":"Book2",
               "Bookstatus":3,
               "Country":"AU"
            },
            {
               "BookId":"7b557a75-bf5e-4c29-9f31-43a7fee77520",
               "BookName":"Book3",
               "Bookstatus":3,
               "Country":"AU"
            },
            {
               "BookId":"c945ade5-d540-4378-9979-3842a1da396b",
               "BookName":"Book4",
               "Bookstatus":3,
               "Country":"AU"
            },
            {
               "BookId":"814869e2-e5af-48bc-a6da-28f272366955",
               "BookName":"Book5",
               "Bookstatus":3,
               "Country":"AU"
            }]';
    ?>
    
    <html>
        <head>
            <title>Sending values</title>
            <script>
                function getelement( name ){
                    return document.querySelectorAll('form input[name="'+name+'"]')[0];
                }
                function setvalues( e ){
                    var n=e.options[ e.options.selectedIndex ];
                    getelement('bookname').value=n.value;
                    getelement('access-token').value=n.dataset.token;
                    getelement('bookid').value=n.dataset.id;
                }
            </script>
        </head>
        <body>
            <h1>CashBooks</h1>
            <form action="getcoa.php" method="post">
                <select name="books" onchange="setvalues(this)">
                    <optgroup label='Books'>
                        <option disabled='' hidden='hidden' selected=true>Please Select
                    <?php
    
                        $json=json_decode( $response );
    
                        foreach( $json as $obj ){
                            echo "<option value='{$obj->BookName}' data-id='{$obj->BookId}' data-token='{$access_token}' data-status='{$obj->Bookstatus}'>{$obj->BookName}";
                        }
                    ?>
                    </optgroup>
                </select>
    
                <!-- empty, hidden input fields to be populated when dropdown changes -->
                <input type="hidden" name="bookid">
                <input type="hidden" name="bookname">
                <input type="hidden" name="access-token">
    
                <input type="submit" value="Get Chart of Accounts">
            </form>
        </body>
    </html>