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

报告相同问题?