Open alilienfeld opened 10 months ago
For passing array-type data back and forth as part of a form submission I suggest using the built in methods _jsonencode and _jsondecode to serialize the array data into a JSON string that can be transferred in the hidden input.
So instead of something like this:
@foreach($deck as $card)
<input type='hidden' name='deck' value='{{$card}}'>
@end foreach
Try this:
<input type='hidden' name='deck' value='{{ json_encode($deck)}} '>
With that code in place, do a "View Source" on the page and observe how it converts the array into a JSON string object.
Then, when processing the form you'll want to translate that JSON string object back into an array:
$deck = json_decode($this->app->old('deck'));
Give that a shot and let me know if you have any follow-up questions or issues implementing it.
For P3 one aspect of the game is that cards are being drawn. I am trying to persist a deck of cards from index.php to process.php. I did get feedback from P2 here https://github.com/susanBuck/e2-fall23/issues/20 that I am trying to implement. Therefore I put all game processing in process and index only displays form and results. I have a line of code that redirects from index to process if an initial card has not been drawn to start the game:
From process I draw the first card out of the deck:
Now I have initialized the game so the next time I end up on process it will be due to form submission guessing next card being high or low. The only way I can think of to get the current value of deck back to process is via hidden form submission. Index is returning a variable for deck in blade:
When I load the page with deck as a variable I get an error that I cannot submit an array to a string. I did try to make a foreach loop from steps in https://stackoverflow.com/questions/6547209/passing-an-array-using-an-html-form-hidden-element but this only submits one value
The end goal here is to submit a variable that is an array to the form as a hidden value so that I can access what remains to the deck to draw the next card in process once a guess has been submitted via logic. I will add logic to restart the deck as it runs out of cards.
I have a link to functioning code here but it does not persist the deck across process and index: https://github.com/alilienfeld/e2/blob/main/p3/app/Controllers/AppController.php Thanks!