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 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题