doucuan5365
doucuan5365
2015-02-06 14:04
浏览 101
已采纳

Laravel处理单选按钮值

I have a dynamically formed table with radio buttons to choose pass or fail in each rows. Below are my codes:

<form name="form2" method="post" action="/admin/testresults/update/added">
  <?php if(isset($rows)){ $i=1; $pass=$ fail='' ; foreach ($rows as $row) { (($row->result == 1) ? $pass='checked' : $fail='checked'); print "
  <tr>
    <td>".$i."</td>
    <td>".$row->name." ".$row->last_name."</td>
    <td>
      <input type='radio' name='".$row->userId."' value='1' ".$pass.">Pass
      <br/>
      <input type='radio' name='".$row->userId."' value='0' ".$fail.">Fail
      <input type='hidden' name='".$row->userId."' value='".$row->userId."' </td>
  </tr>
  "; $i++; } print "
  <tr>
    <td colspan='3'>
      <input class='ember-view btn btn-danger' type='submit' />
    </td>
  </tr>"; } ?>
</form>

I'm using the below codes in my controller:

$inputs = Input::get();
foreach($inputs as $input){
    TestResults::updateCandidate($input);
}

What I want to pass into updateCandidate() is the student id and the selected radio button value (1/0). How can I do this?

Thanks

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doutang1946
    doutang1946 2015-02-06 14:29
    已采纳

    Firstly, I agree with @Chelius comments, look at laravel's documentation around blade templating and @if / @foreach and the use of {{ Form::input }}.

    However in answer to your question - you will need to name you inputs better to retrieve the user id and the verbose PHP version should end up looking like this:

    <td>
      <input type='radio' name='results[".$row->userId."]' value='1' ".$pass.">Pass
      <br/>
      <input type='radio' name='results[".$row->userId."]' value='0' ".$fail.">Fail
      <input type='hidden' name='".$row->userId."' value='".$row->userId."'
     </td>
    

    When processing, your code should then be:

    $inputs = Input::get();
    foreach($inputs['results'] as $userId => $result){
        TestResults::updateCandidate($userId, $result);
    }
    

    Note that you may need to update your "updateCandidate" function to accept the user id and the result.

    点赞 评论

相关推荐