1

I make a form in blade.php, Here I can select multiple checkbox, and I want to pass selected input’s value to controller in a array.But I failed, I can not send the data. Here is code from view. The selected input’s value can be 1,2 etc;

<form method="post" action="{{action('votesubmitController@votesubmit')}}" class="form">

<input type="hidden"  name="_token" value="{{ csrf_token() }}">

@foreach($candidate_list[$post_list->id] as $candidate_list[$post_list->id])

<li>
  <input type="checkbox"  name= "selected[]"  value= {{ 
$candidate_list[$post_list->id]->id }}>
  <label>
  <!-- some code here -->
  </label>
</li>

@endforeach
<button type="submit" id="login-button">Submit</button>
</form>

Here is route-

Route::post('/votesubmit','votesubmitController@votesubmit');

If I write return $input in controller I find –

{"_token":"TQIUxVz0LjzM84Z7TaUPk3Y7BLZPjmXUyhXhlQfp","selected":
  ["1","2"]}

That’s I need. I do not know how to get selected value. When I get specific route error exception happens . and says "Undefined variable: selected". Here is my Controller’s code-

class votesubmitController extends Controller
{
public function votesubmit()
{
  $input = Input::all();
 // return $input;     
  foreach ($selected as $selected) {
     echo $selected;
  }
}
}
3
  • 1
    $selected = $input['selected']; add this in your controller. Commented Apr 5, 2017 at 3:25
  • Thanks , It works... Commented Apr 5, 2017 at 3:30
  • @linktoahref . please place it in answers. And Alimur, accept that so this question can be displayed as solved Commented Apr 5, 2017 at 4:24

2 Answers 2

2
// You can try this
class votesubmitController extends Controller
{
 public function votesubmit()
 {
  //$input = Input::all();
  //$selected = $input['selected'];
  $selected = Input::get('selected');   
   foreach ($selected as $selected) 
   {
    echo $selected;
   }
 }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Either use $selected = $input['selected'] Or pass it using Ajax.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.