【问题标题】:How can I set individual variables in a PHP loop from form?如何在表单的 PHP 循环中设置单个变量?
【发布时间】:2020-11-01 05:54:52
【问题描述】:

例如:我有一个基本的 HTML 表单,如下所示:

<div class="form-group">
    <label class="control-label" for="checkboxes" name="industry">How would you classify your business?</label>

    <div class="checkbox">
        <label for="checkboxes-0">
            <input type="checkbox" name="checkboxes" id="checkboxes-0">
            Nonprofit
        </label>
    </div>
    <div class="checkbox">
        <label for="checkboxes-1">
            <input type="checkbox" name="checkboxes" id="checkboxes-1">
            Service
        </label>
    </div>

这只是一个 sn-p 它不是完整的形式

我想将多选表单解析为 PHP 中的变量。对于“Nonprofit”、“Service”等表单中的每个名称,如果用户选择“Nonprofit”,例如,我想为 nonprofit 创建一个变量并将其设置为 1,并使“service”的变量等于 0 .

我知道我必须使用这样的循环

if(isset($_POST['submit'])) {
    $industry = $_POST['checkboxes'];
}

但是我如何循环遍历标签并将用户选择的变量设置为 1 而其余的等于 0?

【问题讨论】:

    标签: php html arrays forms


    【解决方案1】:

    一种方式。首先为每个可以被复选框覆盖的隐藏输入定义一个隐藏输入,每个都需要一个namevalue

        <input type="hidden" name="Nonprofit" value="0">
        <input type="checkbox" name="Nonprofit" id="checkboxes-0" value="1">
        <input type="hidden" name="Service" value="0">
        <input type="checkbox" name="Service" id="checkboxes-1" value="1">
    

    然后您可以访问$_POST['Nonprofit'],其值为01 等...

    另一种方式,知道可以提交什么是提交一个数组:

        <input type="checkbox" name="check[Nonprofit]" id="checkboxes-0" value="1">
        <input type="checkbox" name="check[Service]" id="checkboxes-1" value="1">
    

    然后将提交的内容与0的数组合并:

    $checks = ['Nonprofit' => 0, 'Service' => 0];
    $checks = array_merge($checks, $_POST['check']);
    

    然后您可以访问$checks['Nonprofit'],其值为01 等...

    【讨论】:

      【解决方案2】:

      您可以通过更改 html 中的一些内容并制作一组用户选择的选项来实现所需的输出,我们可以通过这些选项对其进行迭代。

      HTML

      <form method="post">
      <div class="form-group">
       <label class="control-label" for="checkboxes" name="industry">How would you classify your business?</label>
      <div class="checkbox">
       <label for="checkboxes-0">
           <input type="checkbox" name="nonprofit" id="checkboxes-0" value="nonprofit">
           Nonprofit
       </label>
      </div>
      <div class="checkbox">
      <label for="checkboxes-1">
         <input type="checkbox" name="service" id="checkboxes-1" value="service">
           Service
      </label>
      </div>
         <button type="submit" name="submit">Submit</button>
      </div>
      </form>
      

      PHP

      if (isset($_POST['submit'])) {
       foreach ($_POST as $key => $value) {
        if ($value != null) {
         $result[] = $value; 
         }
        }
         foreach ($result as $value1) {
          $values[] = $value1; 
         }
       
       foreach ($values as $value2) {
        echo '$'.$value2.'= 1<br>';
       }
      }
      

      输出

      如果用户选择Nonprofit

      $nonprofit= 1
      

      如果选择了Service

      $service= 1
      

      如果都选择了

      $nonprofit= 1
      $service= 1
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-05-20
        • 1970-01-01
        • 1970-01-01
        • 2018-02-01
        • 1970-01-01
        • 2014-01-09
        • 1970-01-01
        • 2021-10-17
        相关资源
        最近更新 更多