首页 > 解决方案 > Html Form 发送字符串而不是数组

问题描述

I can not send an array to the server through an html form with method post, when the amount of selected checkboxes is only one. 在任何其他情况下,如果元素的数量超过一个,它可以正常工作,并且表单会向我发送一个包含这些元素(字符串)的数组。所以问题是当用户只选择一个元素时,表单会发送一个字符串而不是一个字符串的数组。我该如何解决这个问题,以便向我发送一个包含一个元素(字符串)的数组?

是的,你是对的,我必须提供我的代码,顺便说一下,它在 bootstrap 4.0 中

<form method="post">
  <div class="form-group">
    <label for="myVariable">Choose :</label>
    <div class="form-check">
      <div>
        <input class="form-check-input" name="myVariable" id="idOfChoice1" type="checkbox" value="valueOfChoice1" >
        <label class="form-check-label">
          choice1
        </label>
      </div>
      <div>
        <input class="form-check-input" name="myVariable" id="idOfChoice2" type="checkbox" value="valueOfChoice2" >
        <label class="form-check-label">
          choice2
        </label>
      </div>
    </div>
  </div>
  <button type="submit" id="sendDataToServer" class="btn btn-primary">Submit</button>
</form>

因此,在上面的代码中,如果我选择两个复选框,我会在服务器端得到一个包含两个字符串的数组,顺便说一下,它在 expressjs 中。当我只选中一个复选框而不是一个数组时,我得到一个字符串而不是一个带字符串的数组。谢谢你。

标签: htmlforms

解决方案


本质上,您只需将其称为$form_input=$array[0].

如果这不是您想要的,请给我们一些代码示例并告诉我们您正在使用哪种编程语言,以便我们可以(更好地)帮助您。


推荐阅读