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

  • 写回答

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']);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改