I am currently creating a form with 10 checkboxes in the form, once user click on submit, the form data will be posted to server, if fails it will be redirected back to the form page. At the moment after redirect, the checked checkboxes are cleared - default. I just wonder how would make the form remember the checked boxes and check them after redirect?
The Laravel version I use at the moment is 5.
Sorry if I am not so clear, please see the following -
- The page will shows 10 categories (checkboxes) at the begining
- user made selection, and hits submit button
- If user logged in, votes will be sent to the server and store in the db
- if not logged in, user select to register with fb
- redirect back - and checkboxes unchecked.. (In here I want the checkboxes to remember the checked boxes and check them)
Any help would be most appreciated!
Thanks, Michael
This is my view
SELECT 5 CATEGORIES
<div>
<form method="POST" class='ajax' >
<input type="hidden" name="_token" class="category-vote" value="{{ csrf_token() }}">
<ol>
<!-- loop through each id from the database -->
@foreach ($categories as $category)
<li>
<!-- Get category names from the translation file by using category ids from db -->
<h4><?=trans('categories.category_' . $category->id . '_name')?></h4>
<!-- Check if user has voted, if so - check the box -->
@if( in_array( $category->id, $catVotes ) )
<!-- Input checkbox for user to vote the category -->
<input type="checkbox" class='category-vote' name=<?=trans('categories.category_' . $category->id . '_input_name')?> checked> <br>
@else
<!-- Input checkbox for user to vote the category -->
<input type="checkbox" class='category-vote' name=<?=trans('categories.category_' . $category->id . '_input_name')?>> <br>
@endif
<!-- Get category image from the translation file by using cat ids from db -->
<img style='max-width: 300px;' src= <?=trans('categories.category_' . $category->id . '_img_path')?>>
</li>
<br/><br/>
@endforeach
<!-- Submit vote button -->
@if ( Auth::check() )
<button id='submit-form' class="btn btn-primary">Submit your Votes!</button>
@else
<button class="submit-btn btn btn-primary">Submit your Votes!</button>
@endif
</ol>
</form>
</div>
And this is my controller
// Get Category
$categories = Category::all();
// Check if user has already voted categories
$catVotes = [];
// Check user is logged in and get users voted categoeis if voted
if ( Auth::check() ){
for( $i = 0; $i < count($categoryVotes); $i++ ){
$catVotes[$i] = $categoryVotes[$i]->category_id;
}
}
// Phase 1
return view('vote-in-the-awards', compact('categories', 'catVotes'));
Aucun commentaire:
Enregistrer un commentaire