0

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.

  • Where you want to use BookId. I can not see this value being used in your code. – Himanshu Upadhyay Jun 21 '17 at 14:00
  • You are dynamically setting your input name attributes: ``. Is this intentional? Or do you mean to set a `value` attribute? – Kisaragi Jun 21 '17 at 14:16
  • Where is `$access_token` defined / generated / obtained? – Professor Abronsius Jun 21 '17 at 15:00
  • @HimanshuUpadhyay: I have not added yet because I have not worked out how... That one of the questions I'm asking. As I said, I want to be able to send the BookId to the next page, just like I'm doing with BookName, but without the user knowing it is happening. The user just selects the bookname and clicks "Get Chart of Accounts"... But on the next page, I need BookId in the GET request to do this. But I dont wan't users to see or know BookId exists. – user8114890 Jun 21 '17 at 15:08
  • @Kisaragi.. it is dynamic because the user could select any cashbook. If I make it static then it wont work unless they choose what I want them to. I have no idea what BookName they will choose, so it must be dynamic. – user8114890 Jun 21 '17 at 15:11
  • @RamRaider... I mentioned this already. It is in the of the page. – user8114890 Jun 21 '17 at 15:13
  • With regards to your comment about `bookid` - take a look at the code I posted below ~ a hidden field ( 3 hidden fields ) will be populated via javascript and the bookid will be sent when the form is actually submitted – Professor Abronsius Jun 21 '17 at 16:07
  • I understand your concept, what I pointed out was your implementation, setting the `name` attribute instead of the `value` attribute. – Kisaragi Jun 21 '17 at 17:41

3 Answers3

0

Multiple problems found in your script:

In the select box loop, see how to write a select box

https://www.tutorialspoint.com/html/html_select_tag.htm

Fault:

echo '<option value="' . $bookname . '" name="' . $bookname . '">' . 
$bookname . '</option>';

It must be something like, and there is no name property required in the options

echo '<option value="' . $bookname . '">' . $bookname . '</option>';

:: Doubts you need to pass the bookid instead of bookname

In the input boxes, you haven't echo the variables,

Instead of

<input type="hidden" name="<?php $bookname; ?>">
<input type="hidden" name="<?php $access_token; ?>">

You need

<input type="hidden" name="bookname" value="<?php echo $bookname; ?>">
<input type="hidden" name="access_token" value="<?php echo $access_token; ?>">
fortune
  • 3,361
  • 1
  • 20
  • 30
  • you got me my access tokens into the next page! Big thanks for that. – user8114890 Jun 21 '17 at 15:28
  • Regarding tag is outside the for loop, there is no way to know what name is needed on that tag. The user will have multiple choices from the drop down menu. So the BookName and BookId that need to be passed to the next page will be one out of multiple choices. Maybe I need to use a switch statement or something but just having hard time getting my head around it. But I'm really greatfull you help me get the token over to next page. Your a legend mate. – user8114890 Jun 21 '17 at 15:32
  • $_POST['books'] will yield you the selected book from the form. And if you need multiple books to be selected, use multiple keyword with the select tag. Please refer https://stackoverflow.com/questions/2407284/how-to-get-multiple-selected-values-of-select-box-in-php – fortune Jun 21 '17 at 17:34
0
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>

<?php 
$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"}]';

$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 foreach ($array as $key => $value) { ?>
         <option value='{"<?php echo $value['BookName'] ?>":"<?php echo $value['BookId']; ?>"}'><?php echo $value['BookName'] ?></option>
    <?php } ?>
    </select>
    <input type="hidden" name="<?php echo $bookname; ?>">
    <input type="hidden" name="<?php echo $access_token; ?>">
    <input type="submit" value="Get Chart of Accounts">
</form>

</body>
</html>

And on page getcoa.php can do this json_decode($_POST['books']);

to.shi
  • 94
  • 3
0

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>
Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46
  • Thank you so much for that. I've used javascript in the past for simple client side form validation, and am using it on this project to get my token, but it's so much more useful as your example has shown. Thanks again. – user8114890 Jun 22 '17 at 12:23