duanph1978
2016-09-07 07:59
浏览 27
已采纳

从foreach循环创建字符串

I have an checkbox form where users can select multiple boxes. now i want to get the value of each checked checkbox into one string.

this is what i have now!

            <input onClick="return KeepCount()" type="checkbox" name="keus[]" value="A"> Test 
            <input onClick="return KeepCount()" type="checkbox" name="keus[]" value="B"> Test 
            <input onClick="return KeepCount()" type="checkbox" name="keus[]" value="C"> Test
            <input onClick="return KeepCount()" type="checkbox" name="keus[]" value="D"> Test


    $keuzen = $_POST['keus'];

        foreach($keuzen as $keus) {
          $finalOptions = $keus;
        }

    echo $finalOptions;

the $finalOptions returns only the last checked box value...

when I select A & D $finalOptions says only D if i select C & A $finalOptions says only C etc...

someone who can help me please??

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

我有一个复选框表单,用户可以在其中选择多个框。 now我想获取每个复选框的值 这是我现在拥有的!

 &lt; input onClick =“return KeepCount()”type =“ 复选框“name =”keus []“value =”A“&gt;  Test 
&lt; input onClick =“return KeepCount()”type =“checkbox”name =“keus []”value =“B”&gt;  Test 
&lt; input onClick =“return KeepCount()”type =“checkbox”name =“keus []”value =“C”&gt;  Test 
&lt; input onClick =“return KeepCount()”type =“checkbox”name =“keus []”value =“D”&gt; 测试
 
 
 $ keuzen = $ _POST ['keus']; 
 
 foreach($ keuzen as $ keus){
 $ finalOptions = $ keus; 
} 
 
 echo $ finalOptions;  
   
 
 

$ finalOptions仅返回最后一个复选框值...

当我选择A&amp; 如果我选择C&amp; D $ finalOptions只说D $ finalOptions只说C等......

有人可以帮助我吗?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • doudun6928 2016-09-07 08:04
    已采纳

    without looping it

    <?php 
        $keuzen = $_POST['keus'];
        echo implode(',',$keuzen);
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • douhaoqiao9304 2016-09-07 08:02

    Try this:

    <?php 
     $keuzen = $_POST['keus'];
     $finalOptions ="";
    
     foreach($keuzen as $keus) {
       $finalOptions .= $keus.","; // you may wanted to separate it with some delimiter
     }
    
     echo $finalOptions;
    ?>
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题