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

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 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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作