Why is $this->winners variable an emtpy array after ajax call

2 months ago

anakonda100000
Member | 6
+
0
-

Hi everyone!
I'm using snippets to draw a table of 6, let's say, lottery winners.
I'd like to have a button to choose the prize of a specific winner.

So…what does the code look like?
At start the user has an empty page with a button to draw the 6 winners. The button works fine.

public function handleDrawWinners()
    {
        $response = $this->getHttpRequest()->getPost()['drawWinners'];
        if ($response == 'all') {
            $this->template->winners = $this->winners = $this->getWinnersInfo($this->getWinners(true));
        }
        $this->redrawControl('raffleWinners');
    }

Afterwards I draw rows of winners where each row has a button to choose a prize manually.

{snippet raffleWinners}
<table class="table">
        <thead>
            <tr>
                <th>Name</th>
                <th>Prize</th>
                <th>Choose prize</th>
            </tr>
        </thead>
        <tbody>
        {foreach $winners as $winner}
            <tr>
                <td>{$winner['name']}</td>
                <td>{$winner['prize']}</td>
                <td>
                    <button class="btn" id="prizeManual{$winner['card_id']}" data-prize="{$winner['prizeKey']}" data-card="{$winner['card_id']}">Zvolit cenu manuálně <i class="la la-gift"></i></button>
                </td>
            </tr>
        {/foreach}
        </tbody>
</table>
{/snippet}

So I get the id after a button click which I send to php which works fine as well. The issue is that I need to work with $this->winners because I need to modify only one row but when I dump it, it's empty.

public function handleChangePrize()
    {
        $response = $this->getHttpRequest()->getPost()['changePrize'];
        bdump($response);
        bdump($this->winners);
    }

I think I could probably just send all 6 winners in JSON to the JS event listener which would then pass it to the handler but I want to know why this happens and if there isn't a better solution.

Last edited by anakonda100000 (2019-06-07 09:20)

2 months ago

David Matějka
Moderator | 5912
+
+1
-

hi, see presenter life cycle, handle is executed before render method

2 months ago

anakonda100000
Member | 6
+
0
-

Thanks David!