doulian8554
2016-08-01 19:37
浏览 9
已采纳

如何在PHP中将表单多选值发送为带前导零的字符串?

I have a situation where I am trying to take the values out of a form multiple select and send them off as a concatenated string rather that as an array. The values for the multi select box would be like this:

  • 11111
  • 222222
  • 333333

I would like the values to have a leading zero if under 6 chars (012345) which is done below: (these are two different variations)

    foreach ($_POST["book"] as $books)
        {
            $book = $book.str_pad($books, 0, 6, " ", STR_PAD_LEFT);
        }
    foreach ($_POST["book"] as $books)
        {
            $book .= str_pad(substr($books, 0), 6, " ", STR_PAD_LEFT);
        }

My goal is to have these concat together as a string like:

  • 011111222222333333

I am not sure what I am doing wrong here but I keep getting the PHP error "Invalid argument supplied for foreach()"

Any guidance is highly appreciated. I have been searching for some answers with no luck.

<select multiple name="book" id="book" size="3">
  <option value="11111" selected="selected">11111</option>
  <option value="222222">222222</option>
  <option value="333333">333333</option>
</select>

$book = '';
$param[] = $bookList->getToolkitService()->AddParameterChar('both', 2500, 'book', 'book', $book);

Not sure if this is adequate information but the total string being sent off would be 2500 chars

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

我遇到的情况是我试图从多个选择的表单中取出值并将其作为a 连接字符串而不是数组。 多选框的值如下所示:

  • 11111
  • 222222
  • 333333 < / li>

    如果低于6个字符(012345),我希望这些值具有前导零,如下所示:(这是两种不同的变体) \ n

      foreach($ _POST [“book”]作为$ books)
     {
     $ book = $ book.str_pad($ books,0,6,“”,STR_PAD_LEFT); 
      } 
     foreach($ _POST [“book”]作为$ books)
     {
     $ book。= str_pad(substr($ books,0),6,“”,STR_PAD_LEFT); 
    } 
       
     
     

    我的目标是将这些连接组合成一个字符串,如:

    • 011111222222333333 < / ul>

      我不确定我在这里做错了什么,但我不断收到PHP错误“为foreach()提供的无效参数”

      任何指导都是 非常感谢。 我一直在寻找一些没有运气的答案。

       &lt; select multiple name =“book”id =“book”size =“3”&gt; 
      &lt; option value =“11111”selected =“selected”  &gt; 11111&lt; / option&gt; 
      &lt; option value =“222222”&gt; 222222&lt; / option&gt; 
      &lt; option value =“333333”&gt; 333333&lt; / option&gt; 
      &lt; / select&gt; 
       \  n $ book =''; 
       $ param [] = $ bookList-&gt; getToolkitService() - &gt; AddParameterChar('both',2500,'book','book',$ book); 
         
       
       

      不确定这是否是足够的信息,但发送的总字符串将是2500个字符

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

1条回答 默认 最新

  • doutangu4978 2016-08-01 20:34
    已采纳

    use name in select as: <select name="book[]">

    In php:

      foreach ($_POST['book'] as $key => $book) {
        if(strlen($book)<6){
          for($i = 0; $i < 6-strlen($book); $i++){
            $_POST['book'][$key] = "0".$_POST['book'][$key];
          }
        }
      }
      $books = implode("", $_POST['book']);
    
    已采纳该答案
    打赏 评论

相关推荐 更多相似问题