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

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

图片转代码服务由CSDN问答提供 功能建议

我有一个动态形成的表,其中有单选按钮可以在每行中选择通过或失败。 以下是我的代码:

 &lt; form name =“form2”method =“post”action =“/ admin / testresults / update / added”&gt; 
&lt;  ?php if(isset($ rows)){$ i = 1;  $ pass = $ fail ='';  foreach($行为$ row){(($ row-&gt; result == 1)?$ pass ='checked':$ fail ='checked'); 打印“
&lt; tr&gt; 
&lt; td&gt;”。$ i。“&lt; / td&gt; 
&lt; td&gt;”。$ row-&gt; name。“”。$ row-&gt; last_name。  “&lt; / td&gt; 
&lt; td&gt; 
&lt; input type ='radio'name ='”。$ row-&gt; userId。“'value ='1'”。$ pass。“&gt; Pass  
&lt; br /&gt; 
&lt; input type ='radio'name ='“。$ row-&gt; userId。”'value ='0'“。$ fail。”&gt; Fail 
&lt;  input type ='hidden'name ='“。$ row-&gt; userId。”'value ='“。$ row-&gt; userId。”'&lt; / td&gt; 
&lt; / tr&gt; 
“;  $ I ++;  } print“
&lt; tr&gt; 
&lt; td colspan ='3'&gt; 
&lt; input class ='ember-view btn btn-danger'type ='submit'/&gt; 
&lt; /  td&gt; 
&lt; / tr&gt;“; 我正在控制器中使用以下代码: 
 
 
 <  code> $ inputs = Input :: get(); 
foreach($ inputs as $ input){
 TestResults :: updateCandidate($ input); 
} 
   
 
  

我想传递给updateCandidate()的是学生ID和所选的单选按钮值(1/0)。 我该怎么做?

谢谢

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

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.

    已采纳该答案
    打赏 评论

相关推荐 更多相似问题